blob: 57e36d9f94687158eef265a18de43bfe4886cbe9 [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//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
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 McCallbd309292010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000027
John McCall42227ed2010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000043
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000044// Common CGObjCRuntime functions, these don't belong here, but they
45// don't belong in CGObjCRuntime either so we will live with it for
46// now.
47
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000053
Daniel Dunbar7e5aba42010-04-02 22:29:40 +000054 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
55 // in here; it should never be necessary because that should be the lexical
56 // decl context for the ivar.
Daniel Dunbar031d4d42010-04-02 15:43:29 +000057
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000058 // If we know have an implementation (and the ivar is in it) then
59 // look up in the implementation layout.
Daniel Dunbar59e476b2009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000061 if (ID && ID->getClassInterface() == Container)
62 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
63 else
64 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000065
66 // Compute field index.
67 //
68 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
69 // implemented. This should be fixed to get the information from the layout
70 // directly.
71 unsigned Index = 0;
72 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
73 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
74 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
75 if (Ivar == Ivars[k])
76 break;
77 ++Index;
78 }
79 assert(Index != Ivars.size() && "Ivar is not inside container!");
80
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000109
John McCall8ccfcb52009-09-24 19:53:00 +0000110 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
111 Quals.addCVRQualifiers(CVRQualifiers);
112
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000113 if (!Ivar->isBitField())
114 return LValue::MakeAddr(V, Quals);
Daniel Dunbar961202372009-05-03 12:57:56 +0000115
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000116 // We need to compute the bit offset for the bit-field, the offset is to the
117 // byte. Note, there is a subtle invariant here: we can only call this routine
118 // on non-synthesized ivars but we may be called for synthesized ivars.
119 // However, a synthesized ivar can never be a bit-field, so this is safe.
120 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
121 uint64_t BitFieldSize =
122 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000123
Daniel Dunbardc406b82010-04-05 21:36:35 +0000124 // Allocate a new CGBitFieldInfo object to describe this access.
125 //
126 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
127 // layout object. However, this is blocked on other cleanups to the
128 // Objective-C code, so for now we just live with allocating a bunch of these
129 // objects.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000130
Daniel Dunbarb935b932010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000133 AI.FieldIndex = 0;
134 AI.FieldByteOffset = 0;
135 AI.FieldBitStart = BitOffset;
136 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
137 AI.AccessAlignment = 0;
138 AI.TargetBitOffset = 0;
139 AI.TargetBitWidth = BitFieldSize;
140
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000145 // FIXME: We need to set a very conservative alignment on this, or make sure
146 // that the runtime is doing the right thing.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000147 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000148}
149
150///
151
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000152namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000153
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000154typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000155
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156// FIXME: We should find a nicer way to make the labels for metadata, string
157// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000158
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000159class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000160protected:
161 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000162
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000163private:
164 llvm::Constant *getMessageSendFn() const {
165 // id objc_msgSend (id, SEL, ...)
166 std::vector<const llvm::Type*> Params;
167 Params.push_back(ObjectPtrTy);
168 Params.push_back(SelectorPtrTy);
169 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000170 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
171 Params, true),
172 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000173 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000174
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000175 llvm::Constant *getMessageSendStretFn() const {
176 // id objc_msgSend_stret (id, SEL, ...)
177 std::vector<const llvm::Type*> Params;
178 Params.push_back(ObjectPtrTy);
179 Params.push_back(SelectorPtrTy);
180 return
Owen Anderson41a75022009-08-13 21:57:51 +0000181 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182 Params, true),
183 "objc_msgSend_stret");
184
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000185 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000186
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000187 llvm::Constant *getMessageSendFpretFn() const {
188 // FIXME: This should be long double on x86_64?
189 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
190 std::vector<const llvm::Type*> Params;
191 Params.push_back(ObjectPtrTy);
192 Params.push_back(SelectorPtrTy);
193 return
Owen Anderson41a75022009-08-13 21:57:51 +0000194 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
195 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000196 Params,
197 true),
198 "objc_msgSend_fpret");
199
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000201
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000202 llvm::Constant *getMessageSendSuperFn() const {
203 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
204 const char *SuperName = "objc_msgSendSuper";
205 std::vector<const llvm::Type*> Params;
206 Params.push_back(SuperPtrTy);
207 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000209 Params, true),
210 SuperName);
211 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000212
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000213 llvm::Constant *getMessageSendSuperFn2() const {
214 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
215 const char *SuperName = "objc_msgSendSuper2";
216 std::vector<const llvm::Type*> Params;
217 Params.push_back(SuperPtrTy);
218 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000219 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000220 Params, true),
221 SuperName);
222 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000223
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000224 llvm::Constant *getMessageSendSuperStretFn() const {
225 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
226 // SEL op, ...)
227 std::vector<const llvm::Type*> Params;
228 Params.push_back(Int8PtrTy);
229 Params.push_back(SuperPtrTy);
230 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000231 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000232 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000233 Params, true),
234 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000235 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000236
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000237 llvm::Constant *getMessageSendSuperStretFn2() const {
238 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
239 // SEL op, ...)
240 std::vector<const llvm::Type*> Params;
241 Params.push_back(Int8PtrTy);
242 Params.push_back(SuperPtrTy);
243 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000244 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000245 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000246 Params, true),
247 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000248 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000249
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000250 llvm::Constant *getMessageSendSuperFpretFn() const {
251 // There is no objc_msgSendSuper_fpret? How can that work?
252 return getMessageSendSuperFn();
253 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000254
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000255 llvm::Constant *getMessageSendSuperFpretFn2() const {
256 // There is no objc_msgSendSuper_fpret? How can that work?
257 return getMessageSendSuperFn2();
258 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000259
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000260protected:
261 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000262
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000263public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000264 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000265 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000266
Daniel Dunbar5d715592008-08-12 05:28:47 +0000267 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
268 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000269
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000270 /// PtrObjectPtrTy - LLVM type for id *
271 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000273 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000274 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000275 /// ProtocolPtrTy - LLVM type for external protocol handles
276 /// (typeof(Protocol))
277 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000278
Daniel Dunbarc722b852008-08-30 03:02:31 +0000279 // SuperCTy - clang type for struct objc_super.
280 QualType SuperCTy;
281 // SuperPtrCTy - clang type for struct objc_super *.
282 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000283
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000284 /// SuperTy - LLVM type for struct objc_super.
285 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000286 /// SuperPtrTy - LLVM type for struct objc_super *.
287 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000288
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000289 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
290 /// in GCC parlance).
291 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000292
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000293 /// PropertyListTy - LLVM type for struct objc_property_list
294 /// (_prop_list_t in GCC parlance).
295 const llvm::StructType *PropertyListTy;
296 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
297 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000298
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000299 // MethodTy - LLVM type for struct objc_method.
300 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000301
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000302 /// CacheTy - LLVM type for struct objc_cache.
303 const llvm::Type *CacheTy;
304 /// CachePtrTy - LLVM type for struct objc_cache *.
305 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000306
Chris Lattnerce8754e2009-04-22 02:44:54 +0000307 llvm::Constant *getGetPropertyFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000311 llvm::SmallVector<CanQualType,4> Params;
312 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
313 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000314 Params.push_back(IdType);
315 Params.push_back(SelType);
316 Params.push_back(Ctx.LongTy);
317 Params.push_back(Ctx.BoolTy);
318 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000319 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000320 FunctionType::ExtInfo()),
321 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
323 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000324
Chris Lattnerce8754e2009-04-22 02:44:54 +0000325 llvm::Constant *getSetPropertyFn() {
326 CodeGen::CodeGenTypes &Types = CGM.getTypes();
327 ASTContext &Ctx = CGM.getContext();
328 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000329 llvm::SmallVector<CanQualType,6> Params;
330 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
331 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000332 Params.push_back(IdType);
333 Params.push_back(SelType);
334 Params.push_back(Ctx.LongTy);
335 Params.push_back(IdType);
336 Params.push_back(Ctx.BoolTy);
337 Params.push_back(Ctx.BoolTy);
338 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000339 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000340 FunctionType::ExtInfo()),
341 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
343 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000344
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000345
346 llvm::Constant *getCopyStructFn() {
347 CodeGen::CodeGenTypes &Types = CGM.getTypes();
348 ASTContext &Ctx = CGM.getContext();
349 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
350 llvm::SmallVector<CanQualType,5> Params;
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.VoidPtrTy);
353 Params.push_back(Ctx.LongTy);
354 Params.push_back(Ctx.BoolTy);
355 Params.push_back(Ctx.BoolTy);
356 const llvm::FunctionType *FTy =
357 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
358 FunctionType::ExtInfo()),
359 false);
360 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
361 }
362
Chris Lattnerce8754e2009-04-22 02:44:54 +0000363 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
365 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000366 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000367 llvm::SmallVector<CanQualType,1> Params;
368 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000369 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000370 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000371 FunctionType::ExtInfo()),
372 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
374 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000375
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000376 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000377 llvm::Constant *getGcReadWeakFn() {
378 // id objc_read_weak (id *)
379 std::vector<const llvm::Type*> Args;
380 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000382 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000384 }
385
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000386 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000387 llvm::Constant *getGcAssignWeakFn() {
388 // id objc_assign_weak (id, id *)
389 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
390 Args.push_back(ObjectPtrTy->getPointerTo());
391 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000392 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
394 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000395
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000396 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000397 llvm::Constant *getGcAssignGlobalFn() {
398 // id objc_assign_global(id, id *)
399 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
400 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000401 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000402 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000403 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
404 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000405
Fariborz Jahanian217af242010-07-20 20:30:03 +0000406 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
407 llvm::Constant *getGcAssignThreadLocalFn() {
408 // id objc_assign_threadlocal(id src, id * dest)
409 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
410 Args.push_back(ObjectPtrTy->getPointerTo());
411 llvm::FunctionType *FTy =
412 llvm::FunctionType::get(ObjectPtrTy, Args, false);
413 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
414 }
415
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000416 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000418 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000419 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
420 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000421 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000422 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000423 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
425 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000426
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000427 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
428 llvm::Constant *GcMemmoveCollectableFn() {
429 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
430 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
431 Args.push_back(Int8PtrTy);
432 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
435 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000436
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000437 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000438 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000439 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000440 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
441 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000442 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000443 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000444 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
445 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000446
447 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000448 llvm::Constant *getExceptionThrowFn() {
449 // void objc_exception_throw(id)
450 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
451 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000452 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000453 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
454 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000455
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000456 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
457 llvm::Constant *getExceptionRethrowFn() {
458 // void objc_exception_rethrow(void)
459 std::vector<const llvm::Type*> Args;
460 llvm::FunctionType *FTy =
461 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
462 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
463 }
464
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000465 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000466 llvm::Constant *getSyncEnterFn() {
467 // void objc_sync_enter (id)
468 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
469 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000470 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000471 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
472 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000473
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000474 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000475 llvm::Constant *getSyncExitFn() {
476 // void objc_sync_exit (id)
477 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
478 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000479 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000480 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
481 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000482
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
485 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000486
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
489 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000490
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendStretFn(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000494
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn2(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
497 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000498
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendFpretFn(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
501 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000502
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
505 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000506
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000507 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
508 ~ObjCCommonTypesHelper(){}
509};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000510
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000511/// ObjCTypesHelper - Helper class that encapsulates lazy
512/// construction of varies types used during ObjC generation.
513class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000514public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000515 /// SymtabTy - LLVM type for struct objc_symtab.
516 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000517 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
518 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000519 /// ModuleTy - LLVM type for struct objc_module.
520 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000521
Daniel Dunbarb036db82008-08-13 03:21:16 +0000522 /// ProtocolTy - LLVM type for struct objc_protocol.
523 const llvm::StructType *ProtocolTy;
524 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
525 const llvm::Type *ProtocolPtrTy;
526 /// ProtocolExtensionTy - LLVM type for struct
527 /// objc_protocol_extension.
528 const llvm::StructType *ProtocolExtensionTy;
529 /// ProtocolExtensionTy - LLVM type for struct
530 /// objc_protocol_extension *.
531 const llvm::Type *ProtocolExtensionPtrTy;
532 /// MethodDescriptionTy - LLVM type for struct
533 /// objc_method_description.
534 const llvm::StructType *MethodDescriptionTy;
535 /// MethodDescriptionListTy - LLVM type for struct
536 /// objc_method_description_list.
537 const llvm::StructType *MethodDescriptionListTy;
538 /// MethodDescriptionListPtrTy - LLVM type for struct
539 /// objc_method_description_list *.
540 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000541 /// ProtocolListTy - LLVM type for struct objc_property_list.
542 const llvm::Type *ProtocolListTy;
543 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
544 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000545 /// CategoryTy - LLVM type for struct objc_category.
546 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000547 /// ClassTy - LLVM type for struct objc_class.
548 const llvm::StructType *ClassTy;
549 /// ClassPtrTy - LLVM type for struct objc_class *.
550 const llvm::Type *ClassPtrTy;
551 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
552 const llvm::StructType *ClassExtensionTy;
553 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
554 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000555 // IvarTy - LLVM type for struct objc_ivar.
556 const llvm::StructType *IvarTy;
557 /// IvarListTy - LLVM type for struct objc_ivar_list.
558 const llvm::Type *IvarListTy;
559 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
560 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000561 /// MethodListTy - LLVM type for struct objc_method_list.
562 const llvm::Type *MethodListTy;
563 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
564 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Anders Carlsson9ff22482008-09-09 10:10:21 +0000566 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
567 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Anders Carlsson9ff22482008-09-09 10:10:21 +0000569 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionTryEnterFn() {
571 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000572 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000573 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000574 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575 Params, false),
576 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000577 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000578
579 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000580 llvm::Constant *getExceptionTryExitFn() {
581 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000582 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000583 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000584 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585 Params, false),
586 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000587 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000588
589 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000590 llvm::Constant *getExceptionExtractFn() {
591 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000592 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000594 Params, false),
595 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000596
Chris Lattnerc6406db2009-04-22 02:26:14 +0000597 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Anders Carlsson9ff22482008-09-09 10:10:21 +0000599 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000600 llvm::Constant *getExceptionMatchFn() {
601 std::vector<const llvm::Type*> Params;
602 Params.push_back(ClassPtrTy);
603 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000605 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000606 Params, false),
607 "objc_exception_match");
608
Chris Lattnerc6406db2009-04-22 02:26:14 +0000609 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000610
Anders Carlsson9ff22482008-09-09 10:10:21 +0000611 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000612 llvm::Constant *getSetJmpFn() {
613 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000614 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000615 return
Owen Anderson41a75022009-08-13 21:57:51 +0000616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000617 Params, false),
618 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Chris Lattnerc6406db2009-04-22 02:26:14 +0000620 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000622public:
623 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000624 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000625};
626
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000627/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000628/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000629class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000630public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000631
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000632 // MethodListnfABITy - LLVM for struct _method_list_t
633 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000634
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000635 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
636 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000638 // ProtocolnfABITy = LLVM for struct _protocol_t
639 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000641 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
642 const llvm::Type *ProtocolnfABIPtrTy;
643
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000644 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
645 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000647 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
648 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000650 // ClassnfABITy - LLVM for struct _class_t
651 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian71394042009-01-23 23:53:38 +0000653 // ClassnfABIPtrTy - LLVM for struct _class_t*
654 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000656 // IvarnfABITy - LLVM for struct _ivar_t
657 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000659 // IvarListnfABITy - LLVM for struct _ivar_list_t
660 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000662 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
663 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000665 // ClassRonfABITy - LLVM for struct _class_ro_t
666 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000668 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
669 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000671 // CategorynfABITy - LLVM for struct _category_t
672 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000674 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000675
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000676 // MessageRefTy - LLVM for:
677 // struct _message_ref_t {
678 // IMP messenger;
679 // SEL name;
680 // };
681 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000682 // MessageRefCTy - clang type for struct _message_ref_t
683 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000685 // MessageRefPtrTy - LLVM for struct _message_ref_t*
686 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000687 // MessageRefCPtrTy - clang type for struct _message_ref_t*
688 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000689
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000690 // MessengerTy - Type of the messenger (shown as IMP above)
691 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000692
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000693 // SuperMessageRefTy - LLVM for:
694 // struct _super_message_ref_t {
695 // SUPER_IMP messenger;
696 // SEL name;
697 // };
698 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000699
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000700 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
701 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000702
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendFixupFn() {
704 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
705 std::vector<const llvm::Type*> Params;
706 Params.push_back(ObjectPtrTy);
707 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSend_fixup");
711 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000713 llvm::Constant *getMessageSendFpretFixupFn() {
714 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
715 std::vector<const llvm::Type*> Params;
716 Params.push_back(ObjectPtrTy);
717 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000719 Params, true),
720 "objc_msgSend_fpret_fixup");
721 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000722
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000723 llvm::Constant *getMessageSendStretFixupFn() {
724 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
725 std::vector<const llvm::Type*> Params;
726 Params.push_back(ObjectPtrTy);
727 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000728 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000729 Params, true),
730 "objc_msgSend_stret_fixup");
731 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000732
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000733 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000734 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000735 // struct _super_message_ref_t*, ...)
736 std::vector<const llvm::Type*> Params;
737 Params.push_back(SuperPtrTy);
738 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000739 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000740 Params, true),
741 "objc_msgSendSuper2_fixup");
742 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000743
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000744 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000745 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000746 // struct _super_message_ref_t*, ...)
747 std::vector<const llvm::Type*> Params;
748 Params.push_back(SuperPtrTy);
749 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000750 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000751 Params, true),
752 "objc_msgSendSuper2_stret_fixup");
753 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000754
Chris Lattnera7c00b42009-04-22 02:15:23 +0000755 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000756 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000757 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000758 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000759
Chris Lattnera7c00b42009-04-22 02:15:23 +0000760 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Chris Lattnera7c00b42009-04-22 02:15:23 +0000762 llvm::Constant *getObjCBeginCatchFn() {
763 std::vector<const llvm::Type*> Params;
764 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000765 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000766 Params, false),
767 "objc_begin_catch");
768 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000769
770 const llvm::StructType *EHTypeTy;
771 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000772
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000773 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
774 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000775};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000776
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000777class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000778public:
779 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000780 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000781 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000782 unsigned ivar_bytepos;
783 unsigned ivar_size;
784 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000785 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000786
787 // Allow sorting based on byte pos.
788 bool operator<(const GC_IVAR &b) const {
789 return ivar_bytepos < b.ivar_bytepos;
790 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000791 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000792
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000793 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000794 public:
795 unsigned skip;
796 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000798 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000799 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000800
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000801protected:
802 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000803 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000804 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000805 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000806
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000807 // gc ivar layout bitmap calculation helper caches.
808 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
809 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000810
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000811 /// LazySymbols - Symbols to generate a lazy reference for. See
812 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000813 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000814
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000815 /// DefinedSymbols - External symbols which are defined by this
816 /// module. The symbols in this list and LazySymbols are used to add
817 /// special linker symbols which ensure that Objective-C modules are
818 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000819 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000820
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000821 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000822 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000823
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000824 /// MethodVarNames - uniqued method variable names.
825 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000826
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000827 /// DefinedCategoryNames - list of category names in form Class_Category.
828 llvm::SetVector<std::string> DefinedCategoryNames;
829
Daniel Dunbarb036db82008-08-13 03:21:16 +0000830 /// MethodVarTypes - uniqued method type signatures. We have to use
831 /// a StringMap here because have no other unique reference.
832 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000833
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000834 /// MethodDefinitions - map of methods which have been defined in
835 /// this translation unit.
836 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000837
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000838 /// PropertyNames - uniqued method variable names.
839 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000840
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000841 /// ClassReferences - uniqued class references.
842 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000843
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000844 /// SelectorReferences - uniqued selector references.
845 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Daniel Dunbarb036db82008-08-13 03:21:16 +0000847 /// Protocols - Protocols for which an objc_protocol structure has
848 /// been emitted. Forward declarations are handled by creating an
849 /// empty structure whose initializer is filled in when/if defined.
850 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Daniel Dunbarc475d422008-10-29 22:36:39 +0000852 /// DefinedProtocols - Protocols which have actually been
853 /// defined. We should not need this, see FIXME in GenerateProtocol.
854 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000856 /// DefinedClasses - List of defined classes.
857 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000858
859 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
860 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000861
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000862 /// DefinedCategories - List of defined categories.
863 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000864
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000865 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
866 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000867
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000868 /// GetNameForMethod - Return a name for the given method.
869 /// \param[out] NameOut - The return value.
870 void GetNameForMethod(const ObjCMethodDecl *OMD,
871 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000872 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000873
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000874 /// GetMethodVarName - Return a unique constant for the given
875 /// selector's name. The return value has type char *.
876 llvm::Constant *GetMethodVarName(Selector Sel);
877 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000878
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000879 /// GetMethodVarType - Return a unique constant for the given
880 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000882 // FIXME: This is a horrible name.
883 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000884 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000885
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000886 /// GetPropertyName - Return a unique constant for the given
887 /// name. The return value has type char *.
888 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000889
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000890 // FIXME: This can be dropped once string functions are unified.
891 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
892 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000894 /// GetClassName - Return a unique constant for the given selector's
895 /// name. The return value has type char *.
896 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000897
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000898 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
899
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000900 /// BuildIvarLayout - Builds ivar layout bitmap for the class
901 /// implementation for the __strong or __weak case.
902 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000903 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
904 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000905
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000906 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000908 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000909 unsigned int BytePos, bool ForStrongLayout,
910 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000911 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000912 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000913 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000914 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000915 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000916 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000917
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000918 /// GetIvarLayoutName - Returns a unique constant for the given
919 /// ivar layout bitmap.
920 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
921 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000922
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000923 /// EmitPropertyList - Emit the given property list. The return
924 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000925 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000926 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000927 const ObjCContainerDecl *OCD,
928 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000929
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000930 /// PushProtocolProperties - Push protocol's property on the input stack.
931 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
932 std::vector<llvm::Constant*> &Properties,
933 const Decl *Container,
934 const ObjCProtocolDecl *PROTO,
935 const ObjCCommonTypesHelper &ObjCTypes);
936
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000937 /// GetProtocolRef - Return a reference to the internal protocol
938 /// description, creating an empty one if it has not been
939 /// defined. The return value has type ProtocolPtrTy.
940 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000941
Daniel Dunbar30c65362009-03-09 20:09:19 +0000942 /// CreateMetadataVar - Create a global variable with internal
943 /// linkage for use by the Objective-C runtime.
944 ///
945 /// This is a convenience wrapper which not only creates the
946 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000947 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000948 ///
949 /// \param Name - The variable name.
950 /// \param Init - The variable initializer; this is also used to
951 /// define the type of the variable.
952 /// \param Section - The section the variable should go into, or 0.
953 /// \param Align - The alignment for the variable, or 0.
954 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000955 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000956 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000957 llvm::Constant *Init,
958 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000959 unsigned Align,
960 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000961
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000962 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000963 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000964 QualType ResultType,
965 llvm::Value *Sel,
966 llvm::Value *Arg0,
967 QualType Arg0Ty,
968 bool IsSuper,
969 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000970 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000971 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000972
Daniel Dunbar5e639272010-04-25 20:39:01 +0000973 /// EmitImageInfo - Emit the image info marker used to encode some module
974 /// level information.
975 void EmitImageInfo();
976
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000977public:
Owen Andersonae86c192009-07-13 04:10:07 +0000978 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000979 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000980
David Chisnall481e3a82010-01-23 02:40:42 +0000981 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000982
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000983 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
984 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000985
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000986 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000987
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000988 /// GetOrEmitProtocol - Get the protocol object for the given
989 /// declaration, emitting it if necessary. The return value has type
990 /// ProtocolPtrTy.
991 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000992
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000993 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
994 /// object for the given declaration, emitting it if needed. These
995 /// forward references will be filled in with empty bodies if no
996 /// definition is seen. The return value has type ProtocolPtrTy.
997 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000998 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
999 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1000
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001001};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001002
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001003class CGObjCMac : public CGObjCCommonMac {
1004private:
1005 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001006
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001007 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001008 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001009 void EmitModuleInfo();
1010
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001011 /// EmitModuleSymols - Emit module symbols, the list of defined
1012 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001013 llvm::Constant *EmitModuleSymbols();
1014
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001015 /// FinishModule - Write out global data structures at the end of
1016 /// processing a translation unit.
1017 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001018
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001019 /// EmitClassExtension - Generate the class extension structure used
1020 /// to store the weak ivar layout and properties. The return value
1021 /// has type ClassExtensionPtrTy.
1022 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1023
1024 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1025 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001026 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001027 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001028
1029 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1030 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001031
1032 /// EmitIvarList - Emit the ivar list for the given
1033 /// implementation. If ForClass is true the list of class ivars
1034 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1035 /// interface ivars will be emitted. The return value has type
1036 /// IvarListPtrTy.
1037 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001038 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001039
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001040 /// EmitMetaClass - Emit a forward reference to the class structure
1041 /// for the metaclass of the given interface. The return value has
1042 /// type ClassPtrTy.
1043 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1044
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001045 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001046 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001047 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1048 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001049 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001050
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001051 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001052
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001053 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001054
1055 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001056 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001057 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001058 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001059 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001060
1061 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001062 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001063 /// - TypeName: The name for the type containing the methods.
1064 /// - IsProtocol: True iff these methods are for a protocol.
1065 /// - ClassMethds: True iff these are class methods.
1066 /// - Required: When true, only "required" methods are
1067 /// listed. Similarly, when false only "optional" methods are
1068 /// listed. For classes this should always be true.
1069 /// - begin, end: The method list to output.
1070 ///
1071 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001072 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001073 const char *Section,
1074 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001075
Daniel Dunbarc475d422008-10-29 22:36:39 +00001076 /// GetOrEmitProtocol - Get the protocol object for the given
1077 /// declaration, emitting it if necessary. The return value has type
1078 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001079 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001080
1081 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1082 /// object for the given declaration, emitting it if needed. These
1083 /// forward references will be filled in with empty bodies if no
1084 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001085 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001086
Daniel Dunbarb036db82008-08-13 03:21:16 +00001087 /// EmitProtocolExtension - Generate the protocol extension
1088 /// structure used to store optional instance and class methods, and
1089 /// protocol properties. The return value has type
1090 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001091 llvm::Constant *
1092 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1093 const ConstantVector &OptInstanceMethods,
1094 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001095
1096 /// EmitProtocolList - Generate the list of referenced
1097 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001098 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001099 ObjCProtocolDecl::protocol_iterator begin,
1100 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001101
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001102 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1103 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001104 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1105 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001106
1107public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001108 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001109
Fariborz Jahanian71394042009-01-23 23:53:38 +00001110 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001111
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001112 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001113 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001114 QualType ResultType,
1115 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001116 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001117 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001118 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001119 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001120
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001121 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001122 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001123 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001124 QualType ResultType,
1125 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001126 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001127 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001128 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001129 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001130 const CallArgList &CallArgs,
1131 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001132
Daniel Dunbarcb463852008-11-01 01:53:16 +00001133 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001134 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001135
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001136 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1137 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001138
1139 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1140 /// untyped one.
1141 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1142 const ObjCMethodDecl *Method);
1143
John McCall2ca705e2010-07-24 00:37:23 +00001144 virtual llvm::Constant *GetEHType(QualType T);
1145
Daniel Dunbar92992502008-08-15 22:20:32 +00001146 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001147
Daniel Dunbar92992502008-08-15 22:20:32 +00001148 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001149
Daniel Dunbarcb463852008-11-01 01:53:16 +00001150 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001151 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001152
Chris Lattnerd4808922009-03-22 21:03:39 +00001153 virtual llvm::Constant *GetPropertyGetFunction();
1154 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001155 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001156 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001157
John McCallbd309292010-07-06 01:34:17 +00001158 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1159 const ObjCAtTryStmt &S);
1160 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1161 const ObjCAtSynchronizedStmt &S);
1162 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001163 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1164 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001165 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001166 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001167 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001168 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001169 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001170 llvm::Value *src, llvm::Value *dest,
1171 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001172 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001173 llvm::Value *src, llvm::Value *dest,
1174 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001175 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1176 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001177 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1178 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001179 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001180
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001181 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1182 QualType ObjectTy,
1183 llvm::Value *BaseValue,
1184 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001185 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001186 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001187 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001188 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001189};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001190
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001191class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001192private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001193 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001194 llvm::GlobalVariable* ObjCEmptyCacheVar;
1195 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001197 /// SuperClassReferences - uniqued super class references.
1198 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001200 /// MetaClassReferences - uniqued meta class references.
1201 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001202
1203 /// EHTypeReferences - uniqued class ehtype references.
1204 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001205
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001206 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001207 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001208 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209
Fariborz Jahanian67260552009-11-17 21:37:35 +00001210 /// DefinedMetaClasses - List of defined meta-classes.
1211 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1212
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001213 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001214 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001215 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001216
Fariborz Jahanian71394042009-01-23 23:53:38 +00001217 /// FinishNonFragileABIModule - Write out global data structures at the end of
1218 /// processing a translation unit.
1219 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001220
Daniel Dunbar19573e72009-05-15 21:48:48 +00001221 /// AddModuleClassList - Add the given list of class pointers to the
1222 /// module with the provided symbol and section names.
1223 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1224 const char *SymbolName,
1225 const char *SectionName);
1226
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001227 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1228 unsigned InstanceStart,
1229 unsigned InstanceSize,
1230 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001231 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001232 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001233 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001234 llvm::Constant *ClassRoGV,
1235 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001236
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001237 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001238
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001239 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001240
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001241 /// EmitMethodList - Emit the method list for the given
1242 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001243 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001244 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001245 const ConstantVector &Methods);
1246 /// EmitIvarList - Emit the ivar list for the given
1247 /// implementation. If ForClass is true the list of class ivars
1248 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1249 /// interface ivars will be emitted. The return value has type
1250 /// IvarListnfABIPtrTy.
1251 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001252
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001253 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001254 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001255 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001257 /// GetOrEmitProtocol - Get the protocol object for the given
1258 /// declaration, emitting it if necessary. The return value has type
1259 /// ProtocolPtrTy.
1260 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001261
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001262 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1263 /// object for the given declaration, emitting it if needed. These
1264 /// forward references will be filled in with empty bodies if no
1265 /// definition is seen. The return value has type ProtocolPtrTy.
1266 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001268 /// EmitProtocolList - Generate the list of referenced
1269 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001270 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001271 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001272 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001274 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001275 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001276 QualType ResultType,
1277 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001278 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001279 QualType Arg0Ty,
1280 bool IsSuper,
1281 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001282
1283 /// GetClassGlobal - Return the global variable for the Objective-C
1284 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001285 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001287 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001288 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001290 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001291
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001292 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1293 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001294 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1295 const ObjCInterfaceDecl *ID);
1296
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001297 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1298 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001300 const ObjCInterfaceDecl *ID);
1301
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001302 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1303 /// the given ivar.
1304 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001305 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306 const ObjCInterfaceDecl *ID,
1307 const ObjCIvarDecl *Ivar);
1308
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001309 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1310 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001311 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1312 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001313
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001314 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001315 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001316 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001317 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001318
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001319 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001320 return "OBJC_METACLASS_$_";
1321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322
Daniel Dunbar15894b72009-04-07 05:48:37 +00001323 const char *getClassSymbolPrefix() const {
1324 return "OBJC_CLASS_$_";
1325 }
1326
Daniel Dunbar961202372009-05-03 12:57:56 +00001327 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001328 uint32_t &InstanceStart,
1329 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001330
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001331 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001332 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001333 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1334 return CGM.getContext().Selectors.getSelector(0, &II);
1335 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001336
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001337 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001338 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1339 return CGM.getContext().Selectors.getSelector(1, &II);
1340 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001341
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001342 /// ImplementationIsNonLazy - Check whether the given category or
1343 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001344 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001345
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001346public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001347 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001348 // FIXME. All stubs for now!
1349 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001350
Fariborz Jahanian71394042009-01-23 23:53:38 +00001351 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001352 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001353 QualType ResultType,
1354 Selector Sel,
1355 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001356 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001357 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001358 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001359
1360 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001361 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001362 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001363 QualType ResultType,
1364 Selector Sel,
1365 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001366 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001367 llvm::Value *Receiver,
1368 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001369 const CallArgList &CallArgs,
1370 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001371
Fariborz Jahanian71394042009-01-23 23:53:38 +00001372 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001373 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001374
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001375 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1376 bool lvalue = false)
1377 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001378
1379 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1380 /// untyped one.
1381 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1382 const ObjCMethodDecl *Method)
1383 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001384
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001385 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001386
Fariborz Jahanian71394042009-01-23 23:53:38 +00001387 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001388 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001389 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001390
John McCall2ca705e2010-07-24 00:37:23 +00001391 virtual llvm::Constant *GetEHType(QualType T);
1392
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001394 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001395 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001396 virtual llvm::Constant *GetPropertySetFunction() {
1397 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001398 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001399
1400 virtual llvm::Constant *GetCopyStructFunction() {
1401 return ObjCTypes.getCopyStructFn();
1402 }
1403
Chris Lattnerd4808922009-03-22 21:03:39 +00001404 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001405 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001406 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001407
John McCallbd309292010-07-06 01:34:17 +00001408 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1409 const ObjCAtTryStmt &S);
1410 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1411 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001412 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001413 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001414 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001415 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001417 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001418 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001419 llvm::Value *src, llvm::Value *dest,
1420 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001421 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001422 llvm::Value *src, llvm::Value *dest,
1423 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001424 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001425 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001426 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1427 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001428 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001429 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1430 QualType ObjectTy,
1431 llvm::Value *BaseValue,
1432 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001433 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001434 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001435 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001436 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001437};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001438
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001439} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001440
1441/* *** Helper Functions *** */
1442
1443/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001444static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001445 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001446 unsigned idx0,
1447 unsigned idx1) {
1448 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001449 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1450 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001451 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001452 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001453}
1454
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001455/// hasObjCExceptionAttribute - Return true if this class or any super
1456/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001457static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001458 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001459 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001460 return true;
1461 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001462 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001463 return false;
1464}
1465
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001466/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001467
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001468CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001469 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001470 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001471 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001472}
1473
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001474/// GetClass - Return a reference to the class for the given interface
1475/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001476llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001477 const ObjCInterfaceDecl *ID) {
1478 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001479}
1480
1481/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001482llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1483 bool lval) {
1484 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001485}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001486llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001487 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001488 return EmitSelector(Builder, Method->getSelector());
1489}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001490
John McCall2ca705e2010-07-24 00:37:23 +00001491llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1492 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1493 return 0;
1494}
1495
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001496/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001497/*
1498 struct __builtin_CFString {
1499 const int *isa; // point to __CFConstantStringClassReference
1500 int flags;
1501 const char *str;
1502 long length;
1503 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001504*/
1505
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001506/// or Generate a constant NSString object.
1507/*
1508 struct __builtin_NSString {
1509 const int *isa; // point to __NSConstantStringClassReference
1510 const char *str;
1511 unsigned int length;
1512 };
1513*/
1514
Fariborz Jahanian71394042009-01-23 23:53:38 +00001515llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001516 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001517 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1518 CGM.GetAddrOfConstantCFString(SL) :
1519 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001520}
1521
1522/// Generates a message send where the super is the receiver. This is
1523/// a message send to self with special delivery semantics indicating
1524/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001525CodeGen::RValue
1526CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001527 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001528 QualType ResultType,
1529 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001530 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001531 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001532 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001533 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001534 const CodeGen::CallArgList &CallArgs,
1535 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001536 // Create and init a super structure; this is a (receiver, class)
1537 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001538 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001539 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001540 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001541 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001542 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001543 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001544
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001545 // If this is a class message the metaclass is passed as the target.
1546 llvm::Value *Target;
1547 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001548 if (isCategoryImpl) {
1549 // Message sent to 'super' in a class method defined in a category
1550 // implementation requires an odd treatment.
1551 // If we are in a class method, we must retrieve the
1552 // _metaclass_ for the current class, pointed at by
1553 // the class's "isa" pointer. The following assumes that
1554 // isa" is the first ivar in a class (which it must be).
1555 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1556 Target = CGF.Builder.CreateStructGEP(Target, 0);
1557 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001558 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001559 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1560 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1561 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1562 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001563 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001564 }
1565 else if (isCategoryImpl)
1566 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1567 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001568 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1569 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1570 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001571 }
Mike Stump18bb9282009-05-16 07:57:57 +00001572 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1573 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001574 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001575 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001576 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001577 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001578 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001579 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001580 EmitSelector(CGF.Builder, Sel),
1581 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001582 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001583}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001584
1585/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001586CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001587 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001588 QualType ResultType,
1589 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001590 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001591 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001592 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001593 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001594 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001595 EmitSelector(CGF.Builder, Sel),
1596 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001597 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001598}
1599
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001600CodeGen::RValue
1601CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001602 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001603 QualType ResultType,
1604 llvm::Value *Sel,
1605 llvm::Value *Arg0,
1606 QualType Arg0Ty,
1607 bool IsSuper,
1608 const CallArgList &CallArgs,
1609 const ObjCMethodDecl *Method,
1610 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001611 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001612 if (!IsSuper)
1613 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001614 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001615 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001616 CGF.getContext().getObjCSelType()));
1617 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001618
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001619 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001620 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001621 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001622 const llvm::FunctionType *FTy =
1623 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001624
Anders Carlsson280e61f12010-06-21 20:59:55 +00001625 if (Method)
1626 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1627 CGM.getContext().getCanonicalType(ResultType) &&
1628 "Result type mismatch!");
1629
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001630 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001631 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001632 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001633 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001634 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1635 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1636 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001637 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001638 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001639 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001640 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001641 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001642 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001643}
1644
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001645static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1646 if (FQT.isObjCGCStrong())
1647 return Qualifiers::Strong;
1648
1649 if (FQT.isObjCGCWeak())
1650 return Qualifiers::Weak;
1651
1652 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1653 return Qualifiers::Strong;
1654
1655 if (const PointerType *PT = FQT->getAs<PointerType>())
1656 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1657
1658 return Qualifiers::GCNone;
1659}
1660
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001661llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001662 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1663 llvm::Constant *NullPtr =
1664 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian89f37212010-08-05 15:52:12 +00001665 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
1666 DeclRefs.empty())
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001667 return NullPtr;
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001668 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001669 SkipIvars.clear();
1670 IvarsInfo.clear();
1671 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1672 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1673
1674 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1675 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1676 const ValueDecl *VD = BDRE->getDecl();
1677 CharUnits Offset = CGF.BlockDecls[VD];
1678 uint64_t FieldOffset = Offset.getQuantity();
1679 QualType Ty = VD->getType();
1680 assert(!Ty->isArrayType() &&
1681 "Array block variable should have been caught");
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001682 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001683 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1684 FieldOffset,
1685 true,
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001686 hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001687 continue;
1688 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001689
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001690 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1691 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1692 // __block variables are passed by their descriptior address. So, size
1693 // must reflect this.
1694 if (BDRE->isByRef())
1695 FieldSize = WordSizeInBits;
1696 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1697 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1698 FieldSize / WordSizeInBits));
1699 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1700 SkipIvars.push_back(GC_IVAR(FieldOffset,
1701 FieldSize / ByteSizeInBits));
1702 }
1703
1704 if (IvarsInfo.empty())
1705 return NullPtr;
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001706 // Sort on byte position in case we encounterred a union nested in
1707 // block variable type's aggregate type.
1708 if (hasUnion && !IvarsInfo.empty())
1709 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1710 if (hasUnion && !SkipIvars.empty())
1711 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001712
1713 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001714 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001715 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1716 printf("\n block variable layout for block: ");
1717 const unsigned char *s = (unsigned char*)BitMap.c_str();
1718 for (unsigned i = 0; i < BitMap.size(); i++)
1719 if (!(s[i] & 0xf0))
1720 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1721 else
1722 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1723 printf("\n");
1724 }
1725
1726 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001727}
1728
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001729llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001730 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001731 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001732 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001733 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1734
Owen Andersonade90fd2009-07-29 18:54:39 +00001735 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001736 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001737}
1738
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001739void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001740 // FIXME: We shouldn't need this, the protocol decl should contain enough
1741 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001742 DefinedProtocols.insert(PD->getIdentifier());
1743
1744 // If we have generated a forward reference to this protocol, emit
1745 // it now. Otherwise do nothing, the protocol objects are lazily
1746 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001747 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001748 GetOrEmitProtocol(PD);
1749}
1750
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001751llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 if (DefinedProtocols.count(PD->getIdentifier()))
1753 return GetOrEmitProtocol(PD);
1754 return GetOrEmitProtocolRef(PD);
1755}
1756
Daniel Dunbarb036db82008-08-13 03:21:16 +00001757/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001758// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1759struct _objc_protocol {
1760struct _objc_protocol_extension *isa;
1761char *protocol_name;
1762struct _objc_protocol_list *protocol_list;
1763struct _objc__method_prototype_list *instance_methods;
1764struct _objc__method_prototype_list *class_methods
1765};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001766
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001767See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001768*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001769llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1770 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1771
1772 // Early exit if a defining object has already been generated.
1773 if (Entry && Entry->hasInitializer())
1774 return Entry;
1775
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001776 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001777 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001778 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1779
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001780 // Construct method lists.
1781 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1782 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001783 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001784 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001785 ObjCMethodDecl *MD = *i;
1786 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1787 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1788 OptInstanceMethods.push_back(C);
1789 } else {
1790 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001791 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001792 }
1793
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001794 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001795 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001796 ObjCMethodDecl *MD = *i;
1797 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1798 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1799 OptClassMethods.push_back(C);
1800 } else {
1801 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001802 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001803 }
1804
Daniel Dunbarb036db82008-08-13 03:21:16 +00001805 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001806 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001807 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001808 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001809 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001810 PD->protocol_begin(),
1811 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001813 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001814 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1815 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001816 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001817 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001818 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1819 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001820 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001821 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001822
Daniel Dunbarb036db82008-08-13 03:21:16 +00001823 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001824 // Already created, fix the linkage and update the initializer.
1825 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001826 Entry->setInitializer(Init);
1827 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001828 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001829 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001830 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001831 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001832 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001833 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001834 // FIXME: Is this necessary? Why only for protocol?
1835 Entry->setAlignment(4);
1836 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001837 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001838
1839 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001840}
1841
Daniel Dunbarc475d422008-10-29 22:36:39 +00001842llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1844
1845 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001846 // We use the initializer as a marker of whether this is a forward
1847 // reference or not. At module finalization we add the empty
1848 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001849 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001850 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001851 llvm::GlobalValue::ExternalLinkage,
1852 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001853 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001855 // FIXME: Is this necessary? Why only for protocol?
1856 Entry->setAlignment(4);
1857 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001858
Daniel Dunbarb036db82008-08-13 03:21:16 +00001859 return Entry;
1860}
1861
1862/*
1863 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001864 uint32_t size;
1865 struct objc_method_description_list *optional_instance_methods;
1866 struct objc_method_description_list *optional_class_methods;
1867 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868 };
1869*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001870llvm::Constant *
1871CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1872 const ConstantVector &OptInstanceMethods,
1873 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001874 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001875 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001876 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001877 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001878 Values[1] =
1879 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001880 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001881 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1882 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001883 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001884 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001885 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1886 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001887 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001888 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001889
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001890 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001891 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001892 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001893 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001894
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001895 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001896 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001897
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001898 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001899 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001900 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001901 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001902}
1903
1904/*
1905 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001906 struct objc_protocol_list *next;
1907 long count;
1908 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001909 };
1910*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001911llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001912CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001913 ObjCProtocolDecl::protocol_iterator begin,
1914 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001915 std::vector<llvm::Constant*> ProtocolRefs;
1916
Daniel Dunbardec75f82008-08-21 21:57:41 +00001917 for (; begin != end; ++begin)
1918 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001919
1920 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001921 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001922 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001924 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001925 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001926
1927 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001928 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001929 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001930 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 ProtocolRefs.size() - 1);
1932 Values[2] =
1933 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1934 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001935 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001936
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001937 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001938 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001939 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001940 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001941 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001942}
1943
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001944void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1945 std::vector<llvm::Constant*> &Properties,
1946 const Decl *Container,
1947 const ObjCProtocolDecl *PROTO,
1948 const ObjCCommonTypesHelper &ObjCTypes) {
1949 std::vector<llvm::Constant*> Prop(2);
1950 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1951 E = PROTO->protocol_end(); P != E; ++P)
1952 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1953 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1954 E = PROTO->prop_end(); I != E; ++I) {
1955 const ObjCPropertyDecl *PD = *I;
1956 if (!PropertySet.insert(PD->getIdentifier()))
1957 continue;
1958 Prop[0] = GetPropertyName(PD->getIdentifier());
1959 Prop[1] = GetPropertyTypeString(PD, Container);
1960 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1961 }
1962}
1963
Daniel Dunbarb036db82008-08-13 03:21:16 +00001964/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001965 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001966 const char * const name;
1967 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001968 };
1969
1970 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001971 uint32_t entsize; // sizeof (struct _objc_property)
1972 uint32_t prop_count;
1973 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001974 };
1975*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001976llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001977 const Decl *Container,
1978 const ObjCContainerDecl *OCD,
1979 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001980 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001981 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001982 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1983 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001984 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001985 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001986 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001987 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001988 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001989 Prop));
1990 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001991 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001992 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1993 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001994 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1995 ObjCTypes);
1996 }
1997 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1998 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1999 E = CD->protocol_end(); P != E; ++P)
2000 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2001 ObjCTypes);
2002 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002003
2004 // Return null for empty list.
2005 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002006 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002007
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002008 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002009 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002010 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002011 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2012 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002013 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002014 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002015 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002016 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002017
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002018 llvm::GlobalVariable *GV =
2019 CreateMetadataVar(Name, Init,
2020 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002021 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002022 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002023 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002024 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002025}
2026
2027/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002028 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002029 int count;
2030 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002031 };
2032*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002033llvm::Constant *
2034CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2035 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002036 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002037 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2038 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002039 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002040 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002041 Desc);
2042}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002043
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002044llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002045 const char *Section,
2046 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002047 // Return null for empty list.
2048 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002049 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002050
2051 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002052 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002053 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002054 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002055 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002056 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002057
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002058 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002059 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002060 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002061}
2062
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002063/*
2064 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002065 char *category_name;
2066 char *class_name;
2067 struct _objc_method_list *instance_methods;
2068 struct _objc_method_list *class_methods;
2069 struct _objc_protocol_list *protocols;
2070 uint32_t size; // <rdar://4585769>
2071 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002072 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002073*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002074void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002075 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002076
Mike Stump18bb9282009-05-16 07:57:57 +00002077 // FIXME: This is poor design, the OCD should have a pointer to the category
2078 // decl. Additionally, note that Category can be null for the @implementation
2079 // w/o an @interface case. Sema should just create one for us as it does for
2080 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002081 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002082 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002083 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002084
2085 llvm::SmallString<256> ExtName;
2086 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2087 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002088
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002089 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002090 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002091 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002092 // Instance methods should always be defined.
2093 InstanceMethods.push_back(GetMethodConstant(*i));
2094 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002095 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002096 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002097 // Class methods should always be defined.
2098 ClassMethods.push_back(GetMethodConstant(*i));
2099 }
2100
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002101 std::vector<llvm::Constant*> Values(7);
2102 Values[0] = GetClassName(OCD->getIdentifier());
2103 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002104 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002105 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002106 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002107 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002108 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002109 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002110 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002111 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002112 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002113 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002114 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002115 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002116 Category->protocol_begin(),
2117 Category->protocol_end());
2118 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002119 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002120 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002121 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002122
2123 // If there is no category @interface then there can be no properties.
2124 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002125 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002126 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002127 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002128 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002129 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130
Owen Anderson0e0189d2009-07-27 22:29:56 +00002131 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002132 Values);
2133
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002134 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002135 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002136 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002137 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002138 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002139 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002140}
2141
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002142// FIXME: Get from somewhere?
2143enum ClassFlags {
2144 eClassFlags_Factory = 0x00001,
2145 eClassFlags_Meta = 0x00002,
2146 // <rdr://5142207>
2147 eClassFlags_HasCXXStructors = 0x02000,
2148 eClassFlags_Hidden = 0x20000,
2149 eClassFlags_ABI2_Hidden = 0x00010,
2150 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2151};
2152
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002153/*
2154 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002155 Class isa;
2156 Class super_class;
2157 const char *name;
2158 long version;
2159 long info;
2160 long instance_size;
2161 struct _objc_ivar_list *ivars;
2162 struct _objc_method_list *methods;
2163 struct _objc_cache *cache;
2164 struct _objc_protocol_list *protocols;
2165 // Objective-C 1.0 extensions (<rdr://4585769>)
2166 const char *ivar_layout;
2167 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002168 };
2169
2170 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002171*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002172void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002173 DefinedSymbols.insert(ID->getIdentifier());
2174
Chris Lattner86d7d912008-11-24 03:54:41 +00002175 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002176 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002177 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002178 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002179 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002180 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002181 Interface->protocol_begin(),
2182 Interface->protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002183 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002184 if (ID->getNumIvarInitializers())
2185 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002186 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002187 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002188
2189 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002190 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002191 Flags |= eClassFlags_Hidden;
2192
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002193 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002194 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002195 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002196 // Instance methods should always be defined.
2197 InstanceMethods.push_back(GetMethodConstant(*i));
2198 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002199 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002200 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002201 // Class methods should always be defined.
2202 ClassMethods.push_back(GetMethodConstant(*i));
2203 }
2204
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002205 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002206 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002207 ObjCPropertyImplDecl *PID = *i;
2208
2209 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2210 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2211
2212 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2213 if (llvm::Constant *C = GetMethodConstant(MD))
2214 InstanceMethods.push_back(C);
2215 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2216 if (llvm::Constant *C = GetMethodConstant(MD))
2217 InstanceMethods.push_back(C);
2218 }
2219 }
2220
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002221 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002222 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002223 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002224 // Record a reference to the super class.
2225 LazySymbols.insert(Super->getIdentifier());
2226
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002227 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002228 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002229 ObjCTypes.ClassPtrTy);
2230 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002231 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002232 }
2233 Values[ 2] = GetClassName(ID->getIdentifier());
2234 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002235 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2236 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2237 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002238 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002239 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002240 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002241 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002242 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002243 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002244 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002245 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002246 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002247 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002248 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002249 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002250 std::string Name("\01L_OBJC_CLASS_");
2251 Name += ClassName;
2252 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2253 // Check for a forward reference.
2254 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2255 if (GV) {
2256 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2257 "Forward metaclass reference has incorrect type.");
2258 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2259 GV->setInitializer(Init);
2260 GV->setSection(Section);
2261 GV->setAlignment(4);
2262 CGM.AddUsedGlobal(GV);
2263 }
2264 else
2265 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002266 DefinedClasses.push_back(GV);
2267}
2268
2269llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2270 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002271 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002272 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002273 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002274
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002275 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002276 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002277
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002278 std::vector<llvm::Constant*> Values(12);
2279 // The isa for the metaclass is the root of the hierarchy.
2280 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2281 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2282 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002283 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002284 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002285 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002286 // The super class for the metaclass is emitted as the name of the
2287 // super class. The runtime fixes this up to point to the
2288 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002289 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002290 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002291 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002292 ObjCTypes.ClassPtrTy);
2293 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002294 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002295 }
2296 Values[ 2] = GetClassName(ID->getIdentifier());
2297 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002298 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2299 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2300 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002301 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002302 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002303 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002304 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002305 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002306 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002307 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 Values[ 9] = Protocols;
2309 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002310 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002311 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002312 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002313 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314 Values);
2315
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002316 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002317 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002318
2319 // Check for a forward reference.
2320 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2321 if (GV) {
2322 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2323 "Forward metaclass reference has incorrect type.");
2324 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2325 GV->setInitializer(Init);
2326 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002327 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002328 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002329 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002330 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002331 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002332 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002333 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002334
2335 return GV;
2336}
2337
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002338llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002339 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002340
Mike Stump18bb9282009-05-16 07:57:57 +00002341 // FIXME: Should we look these up somewhere other than the module. Its a bit
2342 // silly since we only generate these while processing an implementation, so
2343 // exactly one pointer would work if know when we entered/exitted an
2344 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002345
2346 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002347 // Previously, metaclass with internal linkage may have been defined.
2348 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002349 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2350 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002351 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2352 "Forward metaclass reference has incorrect type.");
2353 return GV;
2354 } else {
2355 // Generate as an external reference to keep a consistent
2356 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002357 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002358 llvm::GlobalValue::ExternalLinkage,
2359 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002360 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002361 }
2362}
2363
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002364llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2365 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2366
2367 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2368 true)) {
2369 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2370 "Forward class metadata reference has incorrect type.");
2371 return GV;
2372 } else {
2373 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2374 llvm::GlobalValue::ExternalLinkage,
2375 0,
2376 Name);
2377 }
2378}
2379
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002380/*
2381 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002382 uint32_t size;
2383 const char *weak_ivar_layout;
2384 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002385 };
2386*/
2387llvm::Constant *
2388CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002389 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002390 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002391
2392 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002393 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002394 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002395 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002396 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002397
2398 // Return null if no extension bits are used.
2399 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002400 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002401
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002402 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002403 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002404 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002405 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002406 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002407}
2408
2409/*
2410 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002411 char *ivar_name;
2412 char *ivar_type;
2413 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002414 };
2415
2416 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002417 int ivar_count;
2418 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002419 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002420*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002422 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002423 std::vector<llvm::Constant*> Ivars, Ivar(3);
2424
2425 // When emitting the root class GCC emits ivar entries for the
2426 // actual class structure. It is not clear if we need to follow this
2427 // behavior; for now lets try and get away with not doing it. If so,
2428 // the cleanest solution would be to make up an ObjCInterfaceDecl
2429 // for the class.
2430 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002431 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002432
2433 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002434 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002435
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002436 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002437 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002438
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002439 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2440 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002441 // Ignore unnamed bit-fields.
2442 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002443 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002444 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2445 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002446 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002447 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002448 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 }
2450
2451 // Return null for empty list.
2452 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002453 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002454
2455 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002456 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002457 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002458 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002459 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002460 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002462 llvm::GlobalVariable *GV;
2463 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002464 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002465 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002466 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002467 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002468 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002469 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002470 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002471 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002472}
2473
2474/*
2475 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002476 SEL method_name;
2477 char *method_types;
2478 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002479 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002480
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002481 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002482 struct objc_method_list *obsolete;
2483 int count;
2484 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485 };
2486*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002487
2488/// GetMethodConstant - Return a struct objc_method constant for the
2489/// given method if it has been defined. The result is null if the
2490/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002491llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002492 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002493 if (!Fn)
2494 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002495
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002496 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002497 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002498 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002499 ObjCTypes.SelectorPtrTy);
2500 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002501 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002502 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002503}
2504
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002505llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002506 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002507 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002508 // Return null for empty list.
2509 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002510 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002511
2512 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002513 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002514 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002515 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002516 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002517 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002518 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002519
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002520 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002521 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002522 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002523}
2524
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002525llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002526 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002527 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002528 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002529
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002530 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002531 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002532 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002533 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002534 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002535 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002536 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002537 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002538 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002539
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002540 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002541}
2542
Daniel Dunbar30c65362009-03-09 20:09:19 +00002543llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002544CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002545 llvm::Constant *Init,
2546 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002547 unsigned Align,
2548 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002549 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002550 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002551 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002552 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002553 if (Section)
2554 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002555 if (Align)
2556 GV->setAlignment(Align);
2557 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002558 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002559 return GV;
2560}
2561
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002562llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002563 // Abuse this interface function as a place to finalize.
2564 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002565 return NULL;
2566}
2567
Chris Lattnerd4808922009-03-22 21:03:39 +00002568llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002569 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002570}
2571
Chris Lattnerd4808922009-03-22 21:03:39 +00002572llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002573 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002574}
2575
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002576llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2577 return ObjCTypes.getCopyStructFn();
2578}
2579
Chris Lattnerd4808922009-03-22 21:03:39 +00002580llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002581 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002582}
2583
John McCallbd309292010-07-06 01:34:17 +00002584void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2585 return EmitTryOrSynchronizedStmt(CGF, S);
2586}
2587
2588void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2589 const ObjCAtSynchronizedStmt &S) {
2590 return EmitTryOrSynchronizedStmt(CGF, S);
2591}
2592
John McCall65bea082010-07-21 06:59:36 +00002593namespace {
John McCallcda666c2010-07-21 07:22:38 +00002594 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002595 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002596 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002597 llvm::Value *CallTryExitVar;
2598 llvm::Value *ExceptionData;
2599 ObjCTypesHelper &ObjCTypes;
2600 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002601 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002602 llvm::Value *CallTryExitVar,
2603 llvm::Value *ExceptionData,
2604 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002605 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002606 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2607
2608 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2609 // Check whether we need to call objc_exception_try_exit.
2610 // In optimized code, this branch will always be folded.
2611 llvm::BasicBlock *FinallyCallExit =
2612 CGF.createBasicBlock("finally.call_exit");
2613 llvm::BasicBlock *FinallyNoCallExit =
2614 CGF.createBasicBlock("finally.no_call_exit");
2615 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2616 FinallyCallExit, FinallyNoCallExit);
2617
2618 CGF.EmitBlock(FinallyCallExit);
2619 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2620 ->setDoesNotThrow();
2621
2622 CGF.EmitBlock(FinallyNoCallExit);
2623
2624 if (isa<ObjCAtTryStmt>(S)) {
2625 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002626 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2627 // Save the current cleanup destination in case there's
2628 // control flow inside the finally statement.
2629 llvm::Value *CurCleanupDest =
2630 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2631
John McCall65bea082010-07-21 06:59:36 +00002632 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2633
John McCallcebe0ca2010-08-11 00:16:14 +00002634 if (CGF.HaveInsertPoint()) {
2635 CGF.Builder.CreateStore(CurCleanupDest,
2636 CGF.getNormalCleanupDestSlot());
2637 } else {
2638 // Currently, the end of the cleanup must always exist.
2639 CGF.EnsureInsertPoint();
2640 }
2641 }
John McCall65bea082010-07-21 06:59:36 +00002642 } else {
2643 // Emit objc_sync_exit(expr); as finally's sole statement for
2644 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002645 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002646 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2647 ->setDoesNotThrow();
2648 }
2649 }
2650 };
John McCall42227ed2010-07-31 23:20:56 +00002651
2652 class FragileHazards {
2653 CodeGenFunction &CGF;
2654 llvm::SmallVector<llvm::Value*, 20> Locals;
2655 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2656
2657 llvm::InlineAsm *ReadHazard;
2658 llvm::InlineAsm *WriteHazard;
2659
2660 llvm::FunctionType *GetAsmFnType();
2661
2662 void collectLocals();
2663 void emitReadHazard(CGBuilderTy &Builder);
2664
2665 public:
2666 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002667
John McCall42227ed2010-07-31 23:20:56 +00002668 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002669 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002670 };
2671}
2672
2673/// Create the fragile-ABI read and write hazards based on the current
2674/// state of the function, which is presumed to be immediately prior
2675/// to a @try block. These hazards are used to maintain correct
2676/// semantics in the face of optimization and the fragile ABI's
2677/// cavalier use of setjmp/longjmp.
2678FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2679 collectLocals();
2680
2681 if (Locals.empty()) return;
2682
2683 // Collect all the blocks in the function.
2684 for (llvm::Function::iterator
2685 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2686 BlocksBeforeTry.insert(&*I);
2687
2688 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2689
2690 // Create a read hazard for the allocas. This inhibits dead-store
2691 // optimizations and forces the values to memory. This hazard is
2692 // inserted before any 'throwing' calls in the protected scope to
2693 // reflect the possibility that the variables might be read from the
2694 // catch block if the call throws.
2695 {
2696 std::string Constraint;
2697 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2698 if (I) Constraint += ',';
2699 Constraint += "*m";
2700 }
2701
2702 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2703 }
2704
2705 // Create a write hazard for the allocas. This inhibits folding
2706 // loads across the hazard. This hazard is inserted at the
2707 // beginning of the catch path to reflect the possibility that the
2708 // variables might have been written within the protected scope.
2709 {
2710 std::string Constraint;
2711 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2712 if (I) Constraint += ',';
2713 Constraint += "=*m";
2714 }
2715
2716 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2717 }
2718}
2719
2720/// Emit a write hazard at the current location.
2721void FragileHazards::emitWriteHazard() {
2722 if (Locals.empty()) return;
2723
2724 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2725 ->setDoesNotThrow();
2726}
2727
John McCall42227ed2010-07-31 23:20:56 +00002728void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2729 assert(!Locals.empty());
2730 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2731 ->setDoesNotThrow();
2732}
2733
2734/// Emit read hazards in all the protected blocks, i.e. all the blocks
2735/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002736void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002737 if (Locals.empty()) return;
2738
2739 CGBuilderTy Builder(CGF.getLLVMContext());
2740
2741 // Iterate through all blocks, skipping those prior to the try.
2742 for (llvm::Function::iterator
2743 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2744 llvm::BasicBlock &BB = *FI;
2745 if (BlocksBeforeTry.count(&BB)) continue;
2746
2747 // Walk through all the calls in the block.
2748 for (llvm::BasicBlock::iterator
2749 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2750 llvm::Instruction &I = *BI;
2751
2752 // Ignore instructions that aren't non-intrinsic calls.
2753 // These are the only calls that can possibly call longjmp.
2754 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2755 if (isa<llvm::IntrinsicInst>(I))
2756 continue;
2757
2758 // Ignore call sites marked nounwind. This may be questionable,
2759 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2760 llvm::CallSite CS(&I);
2761 if (CS.doesNotThrow()) continue;
2762
John McCall2dd7d442010-08-04 05:59:32 +00002763 // Insert a read hazard before the call. This will ensure that
2764 // any writes to the locals are performed before making the
2765 // call. If the call throws, then this is sufficient to
2766 // guarantee correctness as long as it doesn't also write to any
2767 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002768 Builder.SetInsertPoint(&BB, BI);
2769 emitReadHazard(Builder);
2770 }
2771 }
2772}
2773
2774static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2775 if (V) S.insert(V);
2776}
2777
2778void FragileHazards::collectLocals() {
2779 // Compute a set of allocas to ignore.
2780 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2781 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2782 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2783 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2784
2785 // Collect all the allocas currently in the function. This is
2786 // probably way too aggressive.
2787 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2788 for (llvm::BasicBlock::iterator
2789 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2790 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2791 Locals.push_back(&*I);
2792}
2793
2794llvm::FunctionType *FragileHazards::GetAsmFnType() {
2795 std::vector<const llvm::Type *> Tys(Locals.size());
2796 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2797 Tys[I] = Locals[I]->getType();
2798 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002799}
2800
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002801/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002802
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002803 Objective-C setjmp-longjmp (sjlj) Exception Handling
2804 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002805
John McCallbd309292010-07-06 01:34:17 +00002806 A catch buffer is a setjmp buffer plus:
2807 - a pointer to the exception that was caught
2808 - a pointer to the previous exception data buffer
2809 - two pointers of reserved storage
2810 Therefore catch buffers form a stack, with a pointer to the top
2811 of the stack kept in thread-local storage.
2812
2813 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2814 objc_exception_try_exit pops the given catch buffer, which is
2815 required to be the top of the EH stack.
2816 objc_exception_throw pops the top of the EH stack, writes the
2817 thrown exception into the appropriate field, and longjmps
2818 to the setjmp buffer. It crashes the process (with a printf
2819 and an abort()) if there are no catch buffers on the stack.
2820 objc_exception_extract just reads the exception pointer out of the
2821 catch buffer.
2822
2823 There's no reason an implementation couldn't use a light-weight
2824 setjmp here --- something like __builtin_setjmp, but API-compatible
2825 with the heavyweight setjmp. This will be more important if we ever
2826 want to implement correct ObjC/C++ exception interactions for the
2827 fragile ABI.
2828
2829 Note that for this use of setjmp/longjmp to be correct, we may need
2830 to mark some local variables volatile: if a non-volatile local
2831 variable is modified between the setjmp and the longjmp, it has
2832 indeterminate value. For the purposes of LLVM IR, it may be
2833 sufficient to make loads and stores within the @try (to variables
2834 declared outside the @try) volatile. This is necessary for
2835 optimized correctness, but is not currently being done; this is
2836 being tracked as rdar://problem/8160285
2837
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002838 The basic framework for a @try-catch-finally is as follows:
2839 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002840 objc_exception_data d;
2841 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002842 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002843
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002844 objc_exception_try_enter(&d);
2845 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002846 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002847 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002848 // exception path
2849 id _caught = objc_exception_extract(&d);
2850
2851 // enter new try scope for handlers
2852 if (!setjmp(d.jmp_buf)) {
2853 ... match exception and execute catch blocks ...
2854
2855 // fell off end, rethrow.
2856 _rethrow = _caught;
2857 ... jump-through-finally to finally_rethrow ...
2858 } else {
2859 // exception in catch block
2860 _rethrow = objc_exception_extract(&d);
2861 _call_try_exit = false;
2862 ... jump-through-finally to finally_rethrow ...
2863 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002864 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002865 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002866
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002867 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002868 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002870
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002871 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002872 ... dispatch to finally destination ...
2873
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002874 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002875 objc_exception_throw(_rethrow);
2876
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002877 finally_end:
2878 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002879
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002880 This framework differs slightly from the one gcc uses, in that gcc
2881 uses _rethrow to determine if objc_exception_try_exit should be called
2882 and if the object should be rethrown. This breaks in the face of
2883 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002884
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002885 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002886
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002887 - If there are no catch blocks, then we avoid emitting the second
2888 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002889
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2891 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002892
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002893 - FIXME: If there is no @finally block we can do a few more
2894 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002895
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002896 Rethrows and Jumps-Through-Finally
2897 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002898
John McCallbd309292010-07-06 01:34:17 +00002899 '@throw;' is supported by pushing the currently-caught exception
2900 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002901
John McCallbd309292010-07-06 01:34:17 +00002902 Branches through the @finally block are handled with an ordinary
2903 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2904 exceptions are not compatible with C++ exceptions, and this is
2905 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002906
John McCallbd309292010-07-06 01:34:17 +00002907 @synchronized(expr) { stmt; } is emitted as if it were:
2908 id synch_value = expr;
2909 objc_sync_enter(synch_value);
2910 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002911*/
2912
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002913void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2914 const Stmt &S) {
2915 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002916
2917 // A destination for the fall-through edges of the catch handlers to
2918 // jump to.
2919 CodeGenFunction::JumpDest FinallyEnd =
2920 CGF.getJumpDestInCurrentScope("finally.end");
2921
2922 // A destination for the rethrow edge of the catch handlers to jump
2923 // to.
2924 CodeGenFunction::JumpDest FinallyRethrow =
2925 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002926
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002927 // For @synchronized, call objc_sync_enter(sync.expr). The
2928 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002929 // @synchronized. We can't avoid a temp here because we need the
2930 // value to be preserved. If the backend ever does liveness
2931 // correctly after setjmp, this will be unnecessary.
2932 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002933 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002934 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002935 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2936 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002937 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2938 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002939
2940 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2941 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002942 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002943
John McCall2dd7d442010-08-04 05:59:32 +00002944 // Allocate memory for the setjmp buffer. This needs to be kept
2945 // live throughout the try and catch blocks.
2946 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2947 "exceptiondata.ptr");
2948
John McCall42227ed2010-07-31 23:20:56 +00002949 // Create the fragile hazards. Note that this will not capture any
2950 // of the allocas required for exception processing, but will
2951 // capture the current basic block (which extends all the way to the
2952 // setjmp call) as "before the @try".
2953 FragileHazards Hazards(CGF);
2954
John McCallbd309292010-07-06 01:34:17 +00002955 // Create a flag indicating whether the cleanup needs to call
2956 // objc_exception_try_exit. This is true except when
2957 // - no catches match and we're branching through the cleanup
2958 // just to rethrow the exception, or
2959 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002960 // The setjmp-safety rule here is that we should always store to this
2961 // variable in a place that dominates the branch through the cleanup
2962 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002963 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002964 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002965
John McCallbd309292010-07-06 01:34:17 +00002966 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002967 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002968 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002969 CallTryExitVar,
2970 ExceptionData,
2971 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002972
2973 // Enter a try block:
2974 // - Call objc_exception_try_enter to push ExceptionData on top of
2975 // the EH stack.
2976 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2977 ->setDoesNotThrow();
2978
2979 // - Call setjmp on the exception data buffer.
2980 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2981 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2982 llvm::Value *SetJmpBuffer =
2983 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2984 llvm::CallInst *SetJmpResult =
2985 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2986 SetJmpResult->setDoesNotThrow();
2987
2988 // If setjmp returned 0, enter the protected block; otherwise,
2989 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002990 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2991 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002992 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002993 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2994 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002995
John McCallbd309292010-07-06 01:34:17 +00002996 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002997 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002998 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002999 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003000 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003001
3002 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003003
John McCallbd309292010-07-06 01:34:17 +00003004 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003005 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003006
John McCall42227ed2010-07-31 23:20:56 +00003007 // Don't optimize loads of the in-scope locals across this point.
3008 Hazards.emitWriteHazard();
3009
John McCallbd309292010-07-06 01:34:17 +00003010 // For a @synchronized (or a @try with no catches), just branch
3011 // through the cleanup to the rethrow block.
3012 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3013 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003014 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003015 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003016
3017 // Otherwise, we have to match against the caught exceptions.
3018 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003019 // Retrieve the exception object. We may emit multiple blocks but
3020 // nothing can cross this so the value is already in SSA form.
3021 llvm::CallInst *Caught =
3022 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3023 ExceptionData, "caught");
3024 Caught->setDoesNotThrow();
3025
John McCallbd309292010-07-06 01:34:17 +00003026 // Push the exception to rethrow onto the EH value stack for the
3027 // benefit of any @throws in the handlers.
3028 CGF.ObjCEHValueStack.push_back(Caught);
3029
Douglas Gregor96c79492010-04-23 22:50:49 +00003030 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003031
John McCall2dd7d442010-08-04 05:59:32 +00003032 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003033
John McCall2dd7d442010-08-04 05:59:32 +00003034 llvm::BasicBlock *CatchBlock = 0;
3035 llvm::BasicBlock *CatchHandler = 0;
3036 if (HasFinally) {
3037 // Enter a new exception try block (in case a @catch block
3038 // throws an exception).
3039 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3040 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003041
John McCall2dd7d442010-08-04 05:59:32 +00003042 llvm::CallInst *SetJmpResult =
3043 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3044 "setjmp.result");
3045 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003046
John McCall2dd7d442010-08-04 05:59:32 +00003047 llvm::Value *Threw =
3048 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3049
3050 CatchBlock = CGF.createBasicBlock("catch");
3051 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3052 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3053
3054 CGF.EmitBlock(CatchBlock);
3055 }
3056
3057 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003058
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003059 // Handle catch list. As a special case we check if everything is
3060 // matched and avoid generating code for falling off the end if
3061 // so.
3062 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003063 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3064 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003065
Douglas Gregor46a572b2010-04-26 16:46:50 +00003066 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003067 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003068
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003069 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003070 if (!CatchParam) {
3071 AllMatched = true;
3072 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003073 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003074
John McCallbd309292010-07-06 01:34:17 +00003075 // catch(id e) always matches under this ABI, since only
3076 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003077 // FIXME: For the time being we also match id<X>; this should
3078 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003079 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003080 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003081 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003082
John McCallbd309292010-07-06 01:34:17 +00003083 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003084 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003085 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3086
Anders Carlsson9396a892008-09-11 09:15:33 +00003087 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00003088 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003089 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003090
3091 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003092 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003093 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003094
Anders Carlsson9396a892008-09-11 09:15:33 +00003095 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003096
3097 // The scope of the catch variable ends right here.
3098 CatchVarCleanups.ForceCleanup();
3099
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003100 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003101 break;
3102 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003103
Steve Naroff7cae42b2009-07-10 23:34:53 +00003104 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003105 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003106
3107 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003108 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3109 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003110
3111 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003112 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003113
John McCallbd309292010-07-06 01:34:17 +00003114 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003115 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3116 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003117 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003118
John McCallbd309292010-07-06 01:34:17 +00003119 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3120 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003121
3122 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003123 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003124
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003125 // Emit the @catch block.
3126 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003127
3128 // Collect any cleanups for the catch variable. The scope lasts until
3129 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003130 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003131
Steve Naroff371b8fb2009-03-03 19:52:17 +00003132 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003133 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003134
John McCallbd309292010-07-06 01:34:17 +00003135 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003136 llvm::Value *Tmp =
3137 CGF.Builder.CreateBitCast(Caught,
3138 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003139 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003140 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003141
Anders Carlsson9396a892008-09-11 09:15:33 +00003142 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003143
3144 // We're done with the catch variable.
3145 CatchVarCleanups.ForceCleanup();
3146
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003147 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003148
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003149 CGF.EmitBlock(NextCatchBlock);
3150 }
3151
John McCallbd309292010-07-06 01:34:17 +00003152 CGF.ObjCEHValueStack.pop_back();
3153
John McCall2dd7d442010-08-04 05:59:32 +00003154 // If nothing wanted anything to do with the caught exception,
3155 // kill the extract call.
3156 if (Caught->use_empty())
3157 Caught->eraseFromParent();
3158
3159 if (!AllMatched)
3160 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3161
3162 if (HasFinally) {
3163 // Emit the exception handler for the @catch blocks.
3164 CGF.EmitBlock(CatchHandler);
3165
3166 // In theory we might now need a write hazard, but actually it's
3167 // unnecessary because there's no local-accessing code between
3168 // the try's write hazard and here.
3169 //Hazards.emitWriteHazard();
3170
3171 // Don't pop the catch handler; the throw already did.
3172 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003173 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003174 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003175 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003176
John McCall42227ed2010-07-31 23:20:56 +00003177 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003178 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003179
John McCallbd309292010-07-06 01:34:17 +00003180 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003181 CGF.Builder.restoreIP(TryFallthroughIP);
3182 if (CGF.HaveInsertPoint())
3183 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003184 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003185 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003186
John McCallbd309292010-07-06 01:34:17 +00003187 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003188 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003189 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003190 if (CGF.HaveInsertPoint()) {
John McCall2dd7d442010-08-04 05:59:32 +00003191 // Just look in the buffer for the exception to throw.
3192 llvm::CallInst *Caught =
3193 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3194 ExceptionData);
3195 Caught->setDoesNotThrow();
3196
3197 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallbd309292010-07-06 01:34:17 +00003198 ->setDoesNotThrow();
3199 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003200 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003201
John McCall42227ed2010-07-31 23:20:56 +00003202 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003203}
3204
3205void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003206 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003207 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003208
Anders Carlssone005aa12008-09-09 16:16:55 +00003209 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3210 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003211 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003212 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3213 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003215 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003216 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003217 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003218
John McCallbd309292010-07-06 01:34:17 +00003219 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3220 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003221 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003222
3223 // Clear the insertion point to indicate we are in unreachable code.
3224 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003225}
3226
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003227/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003228/// object: objc_read_weak (id *src)
3229///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003230llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003231 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003232 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003233 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3234 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3235 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003236 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003237 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003238 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003239 return read_weak;
3240}
3241
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003242/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3243/// objc_assign_weak (id src, id *dst)
3244///
3245void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003246 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003247 const llvm::Type * SrcTy = src->getType();
3248 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003249 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003250 assert(Size <= 8 && "does not support size > 8");
3251 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003252 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003253 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3254 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003255 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3256 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003257 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003258 src, dst, "weakassign");
3259 return;
3260}
3261
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003262/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3263/// objc_assign_global (id src, id *dst)
3264///
3265void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003266 llvm::Value *src, llvm::Value *dst,
3267 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003268 const llvm::Type * SrcTy = src->getType();
3269 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003270 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003271 assert(Size <= 8 && "does not support size > 8");
3272 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003273 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003274 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3275 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003276 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3277 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003278 if (!threadlocal)
3279 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3280 src, dst, "globalassign");
3281 else
3282 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3283 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003284 return;
3285}
3286
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003287/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003288/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003289///
3290void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003291 llvm::Value *src, llvm::Value *dst,
3292 llvm::Value *ivarOffset) {
3293 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003294 const llvm::Type * SrcTy = src->getType();
3295 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003296 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003297 assert(Size <= 8 && "does not support size > 8");
3298 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003299 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003300 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3301 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003302 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3303 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003304 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3305 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003306 return;
3307}
3308
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003309/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3310/// objc_assign_strongCast (id src, id *dst)
3311///
3312void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003313 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003314 const llvm::Type * SrcTy = src->getType();
3315 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003316 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003317 assert(Size <= 8 && "does not support size > 8");
3318 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003319 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003320 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3321 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003322 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3323 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003324 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003325 src, dst, "weakassign");
3326 return;
3327}
3328
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003329void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003330 llvm::Value *DestPtr,
3331 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003332 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003333 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3334 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003335 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003336 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003337 return;
3338}
3339
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003340/// EmitObjCValueForIvar - Code Gen for ivar reference.
3341///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003342LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3343 QualType ObjectTy,
3344 llvm::Value *BaseValue,
3345 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003346 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003347 const ObjCInterfaceDecl *ID =
3348 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003349 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3350 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003351}
3352
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003353llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003354 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003355 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003356 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003357 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003358 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3359 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003360}
3361
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003362/* *** Private Interface *** */
3363
3364/// EmitImageInfo - Emit the image info marker used to encode some module
3365/// level information.
3366///
3367/// See: <rdr://4810609&4810587&4810587>
3368/// struct IMAGE_INFO {
3369/// unsigned version;
3370/// unsigned flags;
3371/// };
3372enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003373 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003374 eImageInfo_GarbageCollected = (1 << 1),
3375 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003376 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3377
Daniel Dunbar5e639272010-04-25 20:39:01 +00003378 // A flag indicating that the module has no instances of a @synthesize of a
3379 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003380 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003381};
3382
Daniel Dunbar5e639272010-04-25 20:39:01 +00003383void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003384 unsigned version = 0; // Version is unused?
3385 unsigned flags = 0;
3386
3387 // FIXME: Fix and continue?
3388 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3389 flags |= eImageInfo_GarbageCollected;
3390 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3391 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003392
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003393 // We never allow @synthesize of a superclass property.
3394 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003395
Chris Lattner5e016ae2010-06-27 07:15:29 +00003396 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3397
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003398 // Emitted as int[2];
3399 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003400 llvm::ConstantInt::get(Int32Ty, version),
3401 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003402 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003403 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003404
3405 const char *Section;
3406 if (ObjCABI == 1)
3407 Section = "__OBJC, __image_info,regular";
3408 else
3409 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003410 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003411 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003412 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003413 Section,
3414 0,
3415 true);
3416 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003417}
3418
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003419
3420// struct objc_module {
3421// unsigned long version;
3422// unsigned long size;
3423// const char *name;
3424// Symtab symtab;
3425// };
3426
3427// FIXME: Get from somewhere
3428static const int ModuleVersion = 7;
3429
3430void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003431 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003432
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003433 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003434 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3435 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003436 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003437 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003438 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003439 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003440 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003441 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003442 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003443}
3444
3445llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003446 unsigned NumClasses = DefinedClasses.size();
3447 unsigned NumCategories = DefinedCategories.size();
3448
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003449 // Return null if no symbols were defined.
3450 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003451 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003452
3453 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003454 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003455 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003456 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3457 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003458
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003459 // The runtime expects exactly the list of defined classes followed
3460 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003461 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003462 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003463 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003464 ObjCTypes.Int8PtrTy);
3465 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003466 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003467 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003468 ObjCTypes.Int8PtrTy);
3469
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003470 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003471 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003472 NumClasses + NumCategories),
3473 Symbols);
3474
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003475 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003476
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003477 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003478 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3479 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003480 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003481 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003482}
3483
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003484llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003485 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003486 LazySymbols.insert(ID->getIdentifier());
3487
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003488 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003490 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003491 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003492 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003493 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003494 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003495 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3496 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003497 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003498 }
3499
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003500 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003501}
3502
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003503llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3504 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003505 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003506
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003507 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003508 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003509 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003510 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003511 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003512 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3513 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003514 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003515 }
3516
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003517 if (lvalue)
3518 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003519 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003520}
3521
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003522llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003523 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003524
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003525 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003526 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003527 llvm::ConstantArray::get(VMContext,
3528 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003529 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003530 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003531
Owen Anderson170229f2009-07-14 23:10:40 +00003532 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003533}
3534
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003535llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3536 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3537 I = MethodDefinitions.find(MD);
3538 if (I != MethodDefinitions.end())
3539 return I->second;
3540
3541 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3542 // MD isn't emitted yet because it comes from PCH.
3543 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3544 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3545 return MethodDefinitions[MD];
3546 }
3547
3548 return NULL;
3549}
3550
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003551/// GetIvarLayoutName - Returns a unique constant for the given
3552/// ivar layout bitmap.
3553llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003554 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003555 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003556}
3557
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003558void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003560 bool ForStrongLayout,
3561 bool &HasUnion) {
3562 const RecordDecl *RD = RT->getDecl();
3563 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003564 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003565 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003566 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003567 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003568
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003569 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3570 ForStrongLayout, HasUnion);
3571}
3572
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003573void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003574 const llvm::StructLayout *Layout,
3575 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003576 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003577 unsigned int BytePos, bool ForStrongLayout,
3578 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003579 bool IsUnion = (RD && RD->isUnion());
3580 uint64_t MaxUnionIvarSize = 0;
3581 uint64_t MaxSkippedUnionIvarSize = 0;
3582 FieldDecl *MaxField = 0;
3583 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003584 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003585 uint64_t MaxFieldOffset = 0;
3586 uint64_t MaxSkippedFieldOffset = 0;
3587 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003588
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003589 if (RecFields.empty())
3590 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003591 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3592 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3593
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003594 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003595 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003596 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003597 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003598 // Note that 'i' here is actually the field index inside RD of Field,
3599 // although this dependency is hidden.
3600 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3601 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003602 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003603 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003604
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003605 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003606 if (!Field->getIdentifier() || Field->isBitField()) {
3607 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003608 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003609 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003610 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003611
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003612 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003613 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003614 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003615 if (FQT->isUnionType())
3616 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003617
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003618 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003619 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003620 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003621 continue;
3622 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003623
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003624 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003625 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003626 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003627 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003628 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003629 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003630 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3631 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003632 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003633 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003634 FQT = CArray->getElementType();
3635 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003636
3637 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003638 "layout for array of unions not supported");
3639 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003640 int OldIndex = IvarsInfo.size() - 1;
3641 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003642
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003643 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003644 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003645 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003646
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003647 // Replicate layout information for each array element. Note that
3648 // one element is already done.
3649 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003650 for (int FirstIndex = IvarsInfo.size() - 1,
3651 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003652 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003653 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3654 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3655 IvarsInfo[i].ivar_size));
3656 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3657 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3658 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003659 }
3660 continue;
3661 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003662 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003663 // At this point, we are done with Record/Union and array there of.
3664 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003665 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003666
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003667 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003668 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3669 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003670 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003671 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003672 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003673 MaxUnionIvarSize = UnionIvarSize;
3674 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003675 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003676 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003677 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003678 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003679 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003680 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003681 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003682 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3683 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003684 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003685 // FIXME: Why the asymmetry? We divide by word size in bits on other
3686 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003687 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003688 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003689 MaxSkippedUnionIvarSize = UnionIvarSize;
3690 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003691 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003692 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003693 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003694 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003695 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003696 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003697 }
3698 }
3699 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003700
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003701 if (LastFieldBitfield) {
3702 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003703 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3704 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003705 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003706 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003707 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003708 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3709 + ((BitFieldSize % ByteSizeInBits) != 0);
3710 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003711 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003712
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003713 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003714 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003715 MaxUnionIvarSize));
3716 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003717 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003718 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003719}
3720
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003721/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3722/// the computations and returning the layout bitmap (for ivar or blocks) in
3723/// the given argument BitMap string container. Routine reads
3724/// two containers, IvarsInfo and SkipIvars which are assumed to be
3725/// filled already by the caller.
3726llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003727 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003728 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003729
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003730 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003731 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003732 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003733 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003734 if (IvarsInfo[0].ivar_bytepos == 0) {
3735 WordsToSkip = 0;
3736 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003737 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003738 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3739 WordsToScan = IvarsInfo[0].ivar_size;
3740 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003741 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003742 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003743 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003744 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003745 // consecutive 'scanned' object pointers.
3746 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003747 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003748 // Skip over 'gc'able object pointer which lay over each other.
3749 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3750 continue;
3751 // Must skip over 1 or more words. We save current skip/scan values
3752 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003753 SKIP_SCAN SkScan;
3754 SkScan.skip = WordsToSkip;
3755 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003756 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003757
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003758 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003759 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3760 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003761 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003762 WordsToSkip = 0;
3763 WordsToScan = IvarsInfo[i].ivar_size;
3764 }
3765 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003766 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003767 SKIP_SCAN SkScan;
3768 SkScan.skip = WordsToSkip;
3769 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003770 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003771 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003772
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003773 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003774 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003775 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003776 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003777 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003778 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003779 IvarsInfo[LastIndex].ivar_bytepos +
3780 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003781 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003782 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003783 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003784 SKIP_SCAN SkScan;
3785 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3786 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003787 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003788 }
3789 }
3790 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3791 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003792 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003793 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003794 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3795 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3796 // 0xM0 followed by 0x0N detected.
3797 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3798 for (int j = i+1; j < SkipScan; j++)
3799 SkipScanIvars[j] = SkipScanIvars[j+1];
3800 --SkipScan;
3801 }
3802 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003803
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003804 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003805 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003806 unsigned char byte;
3807 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3808 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3809 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3810 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003811
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003812 // first skip big.
3813 for (unsigned int ix = 0; ix < skip_big; ix++)
3814 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003815
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003816 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003817 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003818 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003819 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003820 byte |= 0xf;
3821 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003822 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003823 byte |= scan_small;
3824 scan_small = 0;
3825 }
3826 BitMap += byte;
3827 }
3828 // next scan big
3829 for (unsigned int ix = 0; ix < scan_big; ix++)
3830 BitMap += (unsigned char)(0x0f);
3831 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003832 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003833 byte = scan_small;
3834 BitMap += byte;
3835 }
3836 }
3837 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003838 unsigned char zero = 0;
3839 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003840
3841 llvm::GlobalVariable * Entry =
3842 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3843 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3844 "__TEXT,__cstring,cstring_literals",
3845 1, true);
3846 return getConstantGEP(VMContext, Entry, 0, 0);
3847}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003848
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003849/// BuildIvarLayout - Builds ivar layout bitmap for the class
3850/// implementation for the __strong or __weak case.
3851/// The layout map displays which words in ivar list must be skipped
3852/// and which must be scanned by GC (see below). String is built of bytes.
3853/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3854/// of words to skip and right nibble is count of words to scan. So, each
3855/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3856/// represented by a 0x00 byte which also ends the string.
3857/// 1. when ForStrongLayout is true, following ivars are scanned:
3858/// - id, Class
3859/// - object *
3860/// - __strong anything
3861///
3862/// 2. When ForStrongLayout is false, following ivars are scanned:
3863/// - __weak anything
3864///
3865llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3866 const ObjCImplementationDecl *OMD,
3867 bool ForStrongLayout) {
3868 bool hasUnion = false;
3869
3870 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3871 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3872 return llvm::Constant::getNullValue(PtrTy);
3873
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003874 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003875 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003876 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003877
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003878 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003879 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3880 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3881
3882 if (RecFields.empty())
3883 return llvm::Constant::getNullValue(PtrTy);
3884
3885 SkipIvars.clear();
3886 IvarsInfo.clear();
3887
3888 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3889 if (IvarsInfo.empty())
3890 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003891 // Sort on byte position in case we encounterred a union nested in
3892 // the ivar list.
3893 if (hasUnion && !IvarsInfo.empty())
3894 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3895 if (hasUnion && !SkipIvars.empty())
3896 std::sort(SkipIvars.begin(), SkipIvars.end());
3897
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003898 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003899 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003900
3901 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003902 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003903 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003904 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003905 const unsigned char *s = (unsigned char*)BitMap.c_str();
3906 for (unsigned i = 0; i < BitMap.size(); i++)
3907 if (!(s[i] & 0xf0))
3908 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3909 else
3910 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3911 printf("\n");
3912 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003913 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003914}
3915
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003916llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003917 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3918
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003919 // FIXME: Avoid std::string copying.
3920 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003921 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003922 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003923 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003924 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003925
Owen Anderson170229f2009-07-14 23:10:40 +00003926 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003927}
3928
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003929// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003930llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003931 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3932}
3933
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003934llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003935 std::string TypeStr;
3936 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3937
3938 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003939
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003940 if (!Entry)
3941 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003942 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003943 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003944 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003945
Owen Anderson170229f2009-07-14 23:10:40 +00003946 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003947}
3948
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003949llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003950 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003951 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3952 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003953
3954 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3955
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003956 if (!Entry)
3957 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003958 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003959 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003960 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003961
Owen Anderson170229f2009-07-14 23:10:40 +00003962 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003963}
3964
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003965// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003966llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003967 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003968
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003969 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003970 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003971 llvm::ConstantArray::get(VMContext,
3972 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003973 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003974 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003975
Owen Anderson170229f2009-07-14 23:10:40 +00003976 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003977}
3978
3979// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003980// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003981llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003982CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3983 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003984 std::string TypeStr;
3985 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003986 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3987}
3988
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003989void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003990 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003991 llvm::SmallVectorImpl<char> &Name) {
3992 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003993 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003994 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3995 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003996 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003997 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003998 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003999 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004000}
4001
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004002void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004003 EmitModuleInfo();
4004
Daniel Dunbarc475d422008-10-29 22:36:39 +00004005 // Emit the dummy bodies for any protocols which were referenced but
4006 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004007 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004008 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4009 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004010 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004011
Daniel Dunbarc475d422008-10-29 22:36:39 +00004012 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004013 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004014 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004015 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004016 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004017 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004018 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004019 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004020 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004021 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004022 }
4023
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004024 // Add assembler directives to add lazy undefined symbol references
4025 // for classes which are referenced but not defined. This is
4026 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004027 //
4028 // FIXME: It would be nice if we had an LLVM construct for this.
4029 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4030 llvm::SmallString<256> Asm;
4031 Asm += CGM.getModule().getModuleInlineAsm();
4032 if (!Asm.empty() && Asm.back() != '\n')
4033 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004034
Daniel Dunbard027a922009-09-07 00:20:42 +00004035 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004036 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4037 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004038 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4039 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004040 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004041 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004042 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004043 }
4044
4045 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4046 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4047 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4048 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004049
Daniel Dunbard027a922009-09-07 00:20:42 +00004050 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004051 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004052}
4053
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004054CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004055 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004056 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004057 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004058 ObjCABI = 2;
4059}
4060
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004061/* *** */
4062
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004063ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004064 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004065 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4066 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004067
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004068 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004069 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004070 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004071 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004072 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004073
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004074 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004075 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004076 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004077
Mike Stump18bb9282009-05-16 07:57:57 +00004078 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4079 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004080 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004081 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004082
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004083 // I'm not sure I like this. The implicit coordination is a bit
4084 // gross. We should solve this in a reasonable fashion because this
4085 // is a pretty common task (match some runtime data structure with
4086 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004087
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004088 // FIXME: This is leaked.
4089 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004090
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004091 // struct _objc_super {
4092 // id self;
4093 // Class cls;
4094 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004095 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004096 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004097 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004098 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004099 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004100 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004101 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004102 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004103 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004104
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004105 SuperCTy = Ctx.getTagDeclType(RD);
4106 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004107
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004108 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004109 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4110
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004111 // struct _prop_t {
4112 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004113 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004114 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004115 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004116 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004117 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004118
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004119 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004120 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004121 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004122 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004123 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004124 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004125 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004126 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004127 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004128 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004129 PropertyListTy);
4130 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004131 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004132
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004133 // struct _objc_method {
4134 // SEL _cmd;
4135 // char *method_type;
4136 // char *_imp;
4137 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004138 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004139 Int8PtrTy,
4140 Int8PtrTy,
4141 NULL);
4142 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004143
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004144 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004145 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004146 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004147 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004148}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004149
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004150ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004151 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004152 // struct _objc_method_description {
4153 // SEL name;
4154 // char *types;
4155 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004157 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004158 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004159 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004160 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004161 MethodDescriptionTy);
4162
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004163 // struct _objc_method_description_list {
4164 // int count;
4165 // struct _objc_method_description[1];
4166 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004167 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004168 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004169 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004170 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004171 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004172 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004173
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004174 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004175 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004176 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004177
Daniel Dunbarb036db82008-08-13 03:21:16 +00004178 // Protocol description structures
4179
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004180 // struct _objc_protocol_extension {
4181 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4182 // struct _objc_method_description_list *optional_instance_methods;
4183 // struct _objc_method_description_list *optional_class_methods;
4184 // struct _objc_property_list *instance_properties;
4185 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004186 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004187 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004188 MethodDescriptionListPtrTy,
4189 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004190 PropertyListPtrTy,
4191 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004192 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004193 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004194
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004195 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004196 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004197
Daniel Dunbarc475d422008-10-29 22:36:39 +00004198 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004199
Owen Andersonc36edfe2009-08-13 23:27:53 +00004200 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4201 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004202
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004203 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004204 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004205 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004206 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004207 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004208 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004209 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4210
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004211 // struct _objc_protocol {
4212 // struct _objc_protocol_extension *isa;
4213 // char *protocol_name;
4214 // struct _objc_protocol **_objc_protocol_list;
4215 // struct _objc_method_description_list *instance_methods;
4216 // struct _objc_method_description_list *class_methods;
4217 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004218 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004219 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004220 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004221 MethodDescriptionListPtrTy,
4222 MethodDescriptionListPtrTy,
4223 NULL);
4224 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4225
4226 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004227 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004228 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004229 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004230 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004231
4232 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004233 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004234 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004235
4236 // Class description structures
4237
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004238 // struct _objc_ivar {
4239 // char *ivar_name;
4240 // char *ivar_type;
4241 // int ivar_offset;
4242 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004243 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004244 Int8PtrTy,
4245 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004246 NULL);
4247 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4248
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004249 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004250 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004251 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004252 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004253
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004254 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004255 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004256 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004257 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004258
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004259 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004260 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004261 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004262 Int8PtrTy,
4263 PropertyListPtrTy,
4264 NULL);
4265 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004266 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004267
Owen Andersonc36edfe2009-08-13 23:27:53 +00004268 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004269
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004270 // struct _objc_class {
4271 // Class isa;
4272 // Class super_class;
4273 // char *name;
4274 // long version;
4275 // long info;
4276 // long instance_size;
4277 // struct _objc_ivar_list *ivars;
4278 // struct _objc_method_list *methods;
4279 // struct _objc_cache *cache;
4280 // struct _objc_protocol_list *protocols;
4281 // char *ivar_layout;
4282 // struct _objc_class_ext *ext;
4283 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004284 T = llvm::StructType::get(VMContext,
4285 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004286 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004287 Int8PtrTy,
4288 LongTy,
4289 LongTy,
4290 LongTy,
4291 IvarListPtrTy,
4292 MethodListPtrTy,
4293 CachePtrTy,
4294 ProtocolListPtrTy,
4295 Int8PtrTy,
4296 ClassExtensionPtrTy,
4297 NULL);
4298 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004300 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4301 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004302 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004303
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004304 // struct _objc_category {
4305 // char *category_name;
4306 // char *class_name;
4307 // struct _objc_method_list *instance_method;
4308 // struct _objc_method_list *class_method;
4309 // uint32_t size; // sizeof(struct _objc_category)
4310 // struct _objc_property_list *instance_properties;// category's @property
4311 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004312 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004313 Int8PtrTy,
4314 MethodListPtrTy,
4315 MethodListPtrTy,
4316 ProtocolListPtrTy,
4317 IntTy,
4318 PropertyListPtrTy,
4319 NULL);
4320 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4321
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004322 // Global metadata structures
4323
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004324 // struct _objc_symtab {
4325 // long sel_ref_cnt;
4326 // SEL *refs;
4327 // short cls_def_cnt;
4328 // short cat_def_cnt;
4329 // char *defs[cls_def_cnt + cat_def_cnt];
4330 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004331 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004332 SelectorPtrTy,
4333 ShortTy,
4334 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004335 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336 NULL);
4337 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004338 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004339
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004340 // struct _objc_module {
4341 // long version;
4342 // long size; // sizeof(struct _objc_module)
4343 // char *name;
4344 // struct _objc_symtab* symtab;
4345 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004346 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004347 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004348 LongTy,
4349 Int8PtrTy,
4350 SymtabPtrTy,
4351 NULL);
4352 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004353
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004354
Mike Stump18bb9282009-05-16 07:57:57 +00004355 // FIXME: This is the size of the setjmp buffer and should be target
4356 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004357 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004358
Anders Carlsson9ff22482008-09-09 10:10:21 +00004359 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004360 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004361 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004362
4363 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004364 llvm::StructType::get(VMContext,
4365 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4366 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004367 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004368 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004369 ExceptionDataTy);
4370
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004371}
4372
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004373ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004374 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004375 // struct _method_list_t {
4376 // uint32_t entsize; // sizeof(struct _objc_method)
4377 // uint32_t method_count;
4378 // struct _objc_method method_list[method_count];
4379 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004380 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004381 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004382 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004383 NULL);
4384 CGM.getModule().addTypeName("struct.__method_list_t",
4385 MethodListnfABITy);
4386 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004387 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004389 // struct _protocol_t {
4390 // id isa; // NULL
4391 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004392 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004393 // const struct method_list_t * const instance_methods;
4394 // const struct method_list_t * const class_methods;
4395 // const struct method_list_t *optionalInstanceMethods;
4396 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004397 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004398 // const uint32_t size; // sizeof(struct _protocol_t)
4399 // const uint32_t flags; // = 0
4400 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004401
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004402 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004403 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004404
Owen Anderson758428f2009-08-05 23:18:46 +00004405 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004406 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004407 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004408 ProtocolListTyHolder),
4409 MethodListnfABIPtrTy,
4410 MethodListnfABIPtrTy,
4411 MethodListnfABIPtrTy,
4412 MethodListnfABIPtrTy,
4413 PropertyListPtrTy,
4414 IntTy,
4415 IntTy,
4416 NULL);
4417 CGM.getModule().addTypeName("struct._protocol_t",
4418 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004419
4420 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004421 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004422
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004423 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004424 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004425 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004426 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004427 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004428 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004429 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004430 NULL);
4431 CGM.getModule().addTypeName("struct._objc_protocol_list",
4432 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004433 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004434 ProtocolListnfABITy);
4435
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004436 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004437 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004438
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004439 // struct _ivar_t {
4440 // unsigned long int *offset; // pointer to ivar offset location
4441 // char *name;
4442 // char *type;
4443 // uint32_t alignment;
4444 // uint32_t size;
4445 // }
Mike Stump11289f42009-09-09 15:08:12 +00004446 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004447 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004448 Int8PtrTy,
4449 Int8PtrTy,
4450 IntTy,
4451 IntTy,
4452 NULL);
4453 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004454
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004455 // struct _ivar_list_t {
4456 // uint32 entsize; // sizeof(struct _ivar_t)
4457 // uint32 count;
4458 // struct _iver_t list[count];
4459 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004460 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004461 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004462 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004463 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004464 NULL);
4465 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004466
Owen Anderson9793f0e2009-07-29 22:16:19 +00004467 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004468
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004469 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004470 // uint32_t const flags;
4471 // uint32_t const instanceStart;
4472 // uint32_t const instanceSize;
4473 // uint32_t const reserved; // only when building for 64bit targets
4474 // const uint8_t * const ivarLayout;
4475 // const char *const name;
4476 // const struct _method_list_t * const baseMethods;
4477 // const struct _objc_protocol_list *const baseProtocols;
4478 // const struct _ivar_list_t *const ivars;
4479 // const uint8_t * const weakIvarLayout;
4480 // const struct _prop_list_t * const properties;
4481 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004482
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004483 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004484 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004485 IntTy,
4486 IntTy,
4487 Int8PtrTy,
4488 Int8PtrTy,
4489 MethodListnfABIPtrTy,
4490 ProtocolListnfABIPtrTy,
4491 IvarListnfABIPtrTy,
4492 Int8PtrTy,
4493 PropertyListPtrTy,
4494 NULL);
4495 CGM.getModule().addTypeName("struct._class_ro_t",
4496 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004497
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004498 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4499 std::vector<const llvm::Type*> Params;
4500 Params.push_back(ObjectPtrTy);
4501 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004502 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004503 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4504
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004505 // struct _class_t {
4506 // struct _class_t *isa;
4507 // struct _class_t * const superclass;
4508 // void *cache;
4509 // IMP *vtable;
4510 // struct class_ro_t *ro;
4511 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004512
Owen Andersonc36edfe2009-08-13 23:27:53 +00004513 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004514 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004515 llvm::StructType::get(VMContext,
4516 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517 llvm::PointerType::getUnqual(ClassTyHolder),
4518 CachePtrTy,
4519 llvm::PointerType::getUnqual(ImpnfABITy),
4520 llvm::PointerType::getUnqual(ClassRonfABITy),
4521 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004522 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4523
4524 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525 ClassnfABITy);
4526
Fariborz Jahanian71394042009-01-23 23:53:38 +00004527 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004528 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004530 // struct _category_t {
4531 // const char * const name;
4532 // struct _class_t *const cls;
4533 // const struct _method_list_t * const instance_methods;
4534 // const struct _method_list_t * const class_methods;
4535 // const struct _protocol_list_t * const protocols;
4536 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004537 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004538 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004539 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004540 MethodListnfABIPtrTy,
4541 MethodListnfABIPtrTy,
4542 ProtocolListnfABIPtrTy,
4543 PropertyListPtrTy,
4544 NULL);
4545 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004546
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004547 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004548 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4549 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004550
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004551 // MessageRefTy - LLVM for:
4552 // struct _message_ref_t {
4553 // IMP messenger;
4554 // SEL name;
4555 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004556
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004557 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004558 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004559 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004560 SourceLocation(),
4561 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004562 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004563 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004564 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004565 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004566 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004567
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004568 MessageRefCTy = Ctx.getTagDeclType(RD);
4569 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4570 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004571
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004572 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004573 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004574
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004575 // SuperMessageRefTy - LLVM for:
4576 // struct _super_message_ref_t {
4577 // SUPER_IMP messenger;
4578 // SEL name;
4579 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004580 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004581 SelectorPtrTy,
4582 NULL);
4583 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004584
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004585 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004586 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4587
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004588
4589 // struct objc_typeinfo {
4590 // const void** vtable; // objc_ehtype_vtable + 2
4591 // const char* name; // c++ typeinfo string
4592 // Class cls;
4593 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004594 EHTypeTy = llvm::StructType::get(VMContext,
4595 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004596 Int8PtrTy,
4597 ClassnfABIPtrTy,
4598 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004599 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004600 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004601}
4602
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004604 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004605
Fariborz Jahanian71394042009-01-23 23:53:38 +00004606 return NULL;
4607}
4608
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004609void CGObjCNonFragileABIMac::AddModuleClassList(const
4610 std::vector<llvm::GlobalValue*>
4611 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004612 const char *SymbolName,
4613 const char *SectionName) {
4614 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615
Daniel Dunbar19573e72009-05-15 21:48:48 +00004616 if (!NumClasses)
4617 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Daniel Dunbar19573e72009-05-15 21:48:48 +00004619 std::vector<llvm::Constant*> Symbols(NumClasses);
4620 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004621 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004622 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004623 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004624 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 NumClasses),
4626 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004627
Daniel Dunbar19573e72009-05-15 21:48:48 +00004628 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004629 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004630 llvm::GlobalValue::InternalLinkage,
4631 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004632 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004633 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004634 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004635 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004636}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004637
Fariborz Jahanian71394042009-01-23 23:53:38 +00004638void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4639 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004640
Daniel Dunbar19573e72009-05-15 21:48:48 +00004641 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004642 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004643 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004644 "\01L_OBJC_LABEL_CLASS_$",
4645 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004646
Fariborz Jahanian67260552009-11-17 21:37:35 +00004647 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4648 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4649 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4650 continue;
4651 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004652 }
4653
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004654 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4655 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4656 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4657 continue;
4658 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4659 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004660
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004661 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004662 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4663 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004664
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004665 // Build list of all implemented category addresses in array
4666 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004667 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004668 "\01L_OBJC_LABEL_CATEGORY_$",
4669 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004670 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004671 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4672 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004673
Daniel Dunbar5e639272010-04-25 20:39:01 +00004674 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004675}
4676
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004677/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004678/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004679/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004680/// message dispatch call for all the rest.
4681///
4682bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004683 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4684 default:
4685 assert(0 && "Invalid dispatch method!");
4686 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004687 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004688 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004689 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004690 case CodeGenOptions::Mixed:
4691 break;
4692 }
4693
4694 // If so, see whether this selector is in the white-list of things which must
4695 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004696 if (NonLegacyDispatchMethods.empty()) {
4697 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4698 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4699 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4700 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4701 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4702 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4703 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4704 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4705 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4706 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004707
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004708 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4709 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4710 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4711 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4712 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4713 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4714 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4715 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004716 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004717 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004718 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4719 &CGM.getContext().Idents.get("objects"),
4720 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004721 };
4722 NonLegacyDispatchMethods.insert(
4723 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004724 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004725
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004726 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004727}
4728
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004729// Metadata flags
4730enum MetaDataDlags {
4731 CLS = 0x0,
4732 CLS_META = 0x1,
4733 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004734 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004735 CLS_EXCEPTION = 0x20
4736};
4737/// BuildClassRoTInitializer - generate meta-data for:
4738/// struct _class_ro_t {
4739/// uint32_t const flags;
4740/// uint32_t const instanceStart;
4741/// uint32_t const instanceSize;
4742/// uint32_t const reserved; // only when building for 64bit targets
4743/// const uint8_t * const ivarLayout;
4744/// const char *const name;
4745/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004746/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004747/// const struct _ivar_list_t *const ivars;
4748/// const uint8_t * const weakIvarLayout;
4749/// const struct _prop_list_t * const properties;
4750/// }
4751///
4752llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004753 unsigned flags,
4754 unsigned InstanceStart,
4755 unsigned InstanceSize,
4756 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004757 std::string ClassName = ID->getNameAsString();
4758 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004759 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4760 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4761 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004762 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004763 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4764 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004765 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004766 // const struct _method_list_t * const baseMethods;
4767 std::vector<llvm::Constant*> Methods;
4768 std::string MethodListName("\01l_OBJC_$_");
4769 if (flags & CLS_META) {
4770 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004771 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004772 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004773 // Class methods should always be defined.
4774 Methods.push_back(GetMethodConstant(*i));
4775 }
4776 } else {
4777 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004778 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004779 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004780 // Instance methods should always be defined.
4781 Methods.push_back(GetMethodConstant(*i));
4782 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004783 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004784 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004785 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004786
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004787 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4788 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004789
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004790 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4791 if (llvm::Constant *C = GetMethodConstant(MD))
4792 Methods.push_back(C);
4793 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4794 if (llvm::Constant *C = GetMethodConstant(MD))
4795 Methods.push_back(C);
4796 }
4797 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004798 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799 Values[ 5] = EmitMethodList(MethodListName,
4800 "__DATA, __objc_const", Methods);
4801
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004802 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4803 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004804 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004805 + OID->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004806 OID->protocol_begin(),
4807 OID->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004808
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004809 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004810 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004811 else
4812 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004813 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4814 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004815 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004816 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004817 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004818 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4819 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004820 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004821 Values);
4822 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004823 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4824 llvm::GlobalValue::InternalLinkage,
4825 Init,
4826 (flags & CLS_META) ?
4827 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4828 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004829 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004830 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004831 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004832 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004833
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004834}
4835
4836/// BuildClassMetaData - This routine defines that to-level meta-data
4837/// for the given ClassName for:
4838/// struct _class_t {
4839/// struct _class_t *isa;
4840/// struct _class_t * const superclass;
4841/// void *cache;
4842/// IMP *vtable;
4843/// struct class_ro_t *ro;
4844/// }
4845///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004846llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004847 std::string &ClassName,
4848 llvm::Constant *IsAGV,
4849 llvm::Constant *SuperClassGV,
4850 llvm::Constant *ClassRoGV,
4851 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004852 std::vector<llvm::Constant*> Values(5);
4853 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004854 Values[1] = SuperClassGV;
4855 if (!Values[1])
4856 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004857 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4858 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4859 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004860 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004861 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004862 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4863 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004864 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004865 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004866 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004867 if (HiddenVisibility)
4868 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004869 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004870}
4871
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004872bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004873CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004874 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004875}
4876
Daniel Dunbar961202372009-05-03 12:57:56 +00004877void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004878 uint32_t &InstanceStart,
4879 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004880 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004881 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004882
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004883 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004884 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004885
4886 // If there are no fields, the start is the same as the end.
4887 if (!RL.getFieldCount())
4888 InstanceStart = InstanceSize;
4889 else
4890 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004891}
4892
Fariborz Jahanian71394042009-01-23 23:53:38 +00004893void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4894 std::string ClassName = ID->getNameAsString();
4895 if (!ObjCEmptyCacheVar) {
4896 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004897 CGM.getModule(),
4898 ObjCTypes.CacheTy,
4899 false,
4900 llvm::GlobalValue::ExternalLinkage,
4901 0,
4902 "_objc_empty_cache");
4903
Fariborz Jahanian71394042009-01-23 23:53:38 +00004904 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004905 CGM.getModule(),
4906 ObjCTypes.ImpnfABITy,
4907 false,
4908 llvm::GlobalValue::ExternalLinkage,
4909 0,
4910 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004911 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004912 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004913 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004914 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004915 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004916 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004917 uint32_t InstanceSize = InstanceStart;
4918 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004919 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4920 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004921
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004922 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004923
4924 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004925 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004926 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004927 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004928 if (ID->getNumIvarInitializers())
4929 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004930 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004931 // class is root
4932 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004933 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004934 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004935 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004936 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004937 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4938 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4939 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004940 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004941 if (Root->hasAttr<WeakImportAttr>())
4942 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004943 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004944 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004945 ObjCMetaClassName +
4946 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004947 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004948 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4949 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004950 }
4951 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4952 InstanceStart,
4953 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004954 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004955 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004956 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4957 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004958 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004959
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004960 // Metadata for the class
4961 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004962 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004963 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004964 if (ID->getNumIvarInitializers())
4965 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004966
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004967 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004968 flags |= CLS_EXCEPTION;
4969
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004970 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004971 flags |= CLS_ROOT;
4972 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004973 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004974 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004975 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004976 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004977 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004978 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4979 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004980 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004981 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004982 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004983 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004984 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004985 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004986
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004987 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004988 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004989 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4990 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004991 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004992
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004993 // Determine if this class is also "non-lazy".
4994 if (ImplementationIsNonLazy(ID))
4995 DefinedNonLazyClasses.push_back(ClassMD);
4996
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004997 // Force the definition of the EHType if necessary.
4998 if (flags & CLS_EXCEPTION)
4999 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005000}
5001
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005002/// GenerateProtocolRef - This routine is called to generate code for
5003/// a protocol reference expression; as in:
5004/// @code
5005/// @protocol(Proto1);
5006/// @endcode
5007/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5008/// which will hold address of the protocol meta-data.
5009///
5010llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005011 const ObjCProtocolDecl *PD) {
5012
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005013 // This routine is called for @protocol only. So, we must build definition
5014 // of protocol's meta-data (not a reference to it!)
5015 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016 llvm::Constant *Init =
5017 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5018 ObjCTypes.ExternalProtocolPtrTy);
5019
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005020 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005021 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005022
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005023 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5024 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005025 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005026 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005027 CGM.getModule(),
5028 Init->getType(), false,
5029 llvm::GlobalValue::WeakAnyLinkage,
5030 Init,
5031 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005032 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5033 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005034 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005035 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005036}
5037
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005038/// GenerateCategory - Build metadata for a category implementation.
5039/// struct _category_t {
5040/// const char * const name;
5041/// struct _class_t *const cls;
5042/// const struct _method_list_t * const instance_methods;
5043/// const struct _method_list_t * const class_methods;
5044/// const struct _protocol_list_t * const protocols;
5045/// const struct _prop_list_t * const properties;
5046/// }
5047///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005048void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005049 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005050 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005051 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5052 "_$_" + OCD->getNameAsString());
5053 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005054 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005056 std::vector<llvm::Constant*> Values(6);
5057 Values[0] = GetClassName(OCD->getIdentifier());
5058 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005059 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005060 if (Interface->hasAttr<WeakImportAttr>())
5061 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5062
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005063 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005064 std::vector<llvm::Constant*> Methods;
5065 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005066 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005067 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005068
5069 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005070 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005071 // Instance methods should always be defined.
5072 Methods.push_back(GetMethodConstant(*i));
5073 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005074
5075 Values[2] = EmitMethodList(MethodListName,
5076 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005077 Methods);
5078
5079 MethodListName = Prefix;
5080 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5081 OCD->getNameAsString();
5082 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005083 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005084 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005085 // Class methods should always be defined.
5086 Methods.push_back(GetMethodConstant(*i));
5087 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088
5089 Values[3] = EmitMethodList(MethodListName,
5090 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005091 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005092 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005093 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005094 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005095 llvm::SmallString<256> ExtName;
5096 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5097 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005098 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005099 + Interface->getName() + "_$_"
5100 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005101 Category->protocol_begin(),
5102 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005103 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5104 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005105 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005106 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5107 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005108 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005109
5110 llvm::Constant *Init =
5111 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005112 Values);
5113 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005114 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005115 false,
5116 llvm::GlobalValue::InternalLinkage,
5117 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005118 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005119 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005120 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005121 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005122 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005123 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005124
5125 // Determine if this category is also "non-lazy".
5126 if (ImplementationIsNonLazy(OCD))
5127 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005128}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005129
5130/// GetMethodConstant - Return a struct objc_method constant for the
5131/// given method if it has been defined. The result is null if the
5132/// method has not been defined. The return value has type MethodPtrTy.
5133llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005135 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005136 if (!Fn)
5137 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005138
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005139 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005140 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005141 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005143 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005144 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005145 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005146}
5147
5148/// EmitMethodList - Build meta-data for method declarations
5149/// struct _method_list_t {
5150/// uint32_t entsize; // sizeof(struct _objc_method)
5151/// uint32_t method_count;
5152/// struct _objc_method method_list[method_count];
5153/// }
5154///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005155llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5156 const char *Section,
5157 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005158 // Return null for empty list.
5159 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005160 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005161
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005162 std::vector<llvm::Constant*> Values(3);
5163 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005164 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005165 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005166 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005167 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005168 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005169 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005170 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005171 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005172
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005173 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005174 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005175 llvm::GlobalValue::InternalLinkage,
5176 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005177 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005178 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005179 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005180 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005181 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005182 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005183 ObjCTypes.MethodListnfABIPtrTy);
5184}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005185
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005186/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5187/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005188llvm::GlobalVariable *
5189CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5190 const ObjCIvarDecl *Ivar) {
5191 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005192 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005193 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005194 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005195 CGM.getModule().getGlobalVariable(Name);
5196 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005197 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005198 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005199 false,
5200 llvm::GlobalValue::ExternalLinkage,
5201 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005202 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005203 return IvarOffsetGV;
5204}
5205
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005206llvm::Constant *
5207CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5208 const ObjCIvarDecl *Ivar,
5209 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005210 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005212 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005213 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005214 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005215
Mike Stump18bb9282009-05-16 07:57:57 +00005216 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5217 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005218 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5219 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5220 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005221 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005222 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005223 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005224 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005225 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005226}
5227
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005228/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005229/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005230/// IvarListnfABIPtrTy.
5231/// struct _ivar_t {
5232/// unsigned long int *offset; // pointer to ivar offset location
5233/// char *name;
5234/// char *type;
5235/// uint32_t alignment;
5236/// uint32_t size;
5237/// }
5238/// struct _ivar_list_t {
5239/// uint32 entsize; // sizeof(struct _ivar_t)
5240/// uint32 count;
5241/// struct _iver_t list[count];
5242/// }
5243///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005244
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005245llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005246 const ObjCImplementationDecl *ID) {
5247
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005248 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005249
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005250 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5251 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005252
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005253 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254
Daniel Dunbarae032262009-04-20 00:33:43 +00005255 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005256 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005257 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005258
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005259 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5260 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005261 // Ignore unnamed bit-fields.
5262 if (!IVD->getDeclName())
5263 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005264 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005265 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005266 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5267 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005268 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005269 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005270 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005271 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005272 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005273 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005274 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005275 // NOTE. Size of a bitfield does not match gcc's, because of the
5276 // way bitfields are treated special in each. But I am told that
5277 // 'size' for bitfield ivars is ignored by the runtime so it does
5278 // not matter. If it matters, there is enough info to get the
5279 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005280 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005281 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005282 }
5283 // Return null for empty list.
5284 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005285 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005286 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005287 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005288 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5289 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005290 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005291 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005292 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005293 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005294 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5295 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005296 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005297 llvm::GlobalValue::InternalLinkage,
5298 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005299 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005300 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005301 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005302 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303
Chris Lattnerf56501c2009-07-17 23:57:13 +00005304 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005305 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005306}
5307
5308llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005309 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005310 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005311
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005312 if (!Entry) {
5313 // We use the initializer as a marker of whether this is a forward
5314 // reference or not. At module finalization we add the empty
5315 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005316 Entry =
5317 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5318 llvm::GlobalValue::ExternalLinkage,
5319 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005320 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005321 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005322 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005324 return Entry;
5325}
5326
5327/// GetOrEmitProtocol - Generate the protocol meta-data:
5328/// @code
5329/// struct _protocol_t {
5330/// id isa; // NULL
5331/// const char * const protocol_name;
5332/// const struct _protocol_list_t * protocol_list; // super protocols
5333/// const struct method_list_t * const instance_methods;
5334/// const struct method_list_t * const class_methods;
5335/// const struct method_list_t *optionalInstanceMethods;
5336/// const struct method_list_t *optionalClassMethods;
5337/// const struct _prop_list_t * properties;
5338/// const uint32_t size; // sizeof(struct _protocol_t)
5339/// const uint32_t flags; // = 0
5340/// }
5341/// @endcode
5342///
5343
5344llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005345 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005346 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005347
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005348 // Early exit if a defining object has already been generated.
5349 if (Entry && Entry->hasInitializer())
5350 return Entry;
5351
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005352 // Construct method lists.
5353 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5354 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005355 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005356 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005357 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005358 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005359 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5360 OptInstanceMethods.push_back(C);
5361 } else {
5362 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005363 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005365
5366 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005367 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005368 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005369 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005370 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5371 OptClassMethods.push_back(C);
5372 } else {
5373 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005374 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005375 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005376
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005377 std::vector<llvm::Constant*> Values(10);
5378 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005379 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005380 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005381 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5382 PD->protocol_begin(),
5383 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005384
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005385 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005386 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005387 "__DATA, __objc_const",
5388 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005389 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005390 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 "__DATA, __objc_const",
5392 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005393 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005394 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005395 "__DATA, __objc_const",
5396 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005397 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005398 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005399 "__DATA, __objc_const",
5400 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005401 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005404 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005405 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005406 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005407 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005408 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005409
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005410 if (Entry) {
5411 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005412 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005413 Entry->setInitializer(Init);
5414 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005415 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005416 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5417 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5418 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005419 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005420 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005421 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005422 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005423 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005424 CGM.AddUsedGlobal(Entry);
5425
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005426 // Use this protocol meta-data to build protocol list table in section
5427 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005428 llvm::GlobalVariable *PTGV =
5429 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5430 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5431 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005432 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005433 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005434 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005435 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005436 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005437 return Entry;
5438}
5439
5440/// EmitProtocolList - Generate protocol list meta-data:
5441/// @code
5442/// struct _protocol_list_t {
5443/// long protocol_count; // Note, this is 32/64 bit
5444/// struct _protocol_t[protocol_count];
5445/// }
5446/// @endcode
5447///
5448llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005449CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5450 ObjCProtocolDecl::protocol_iterator begin,
5451 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005452 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005453
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005454 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005455 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005456 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005458 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005459 llvm::SmallString<256> TmpName;
5460 Name.toVector(TmpName);
5461 llvm::GlobalVariable *GV =
5462 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005463 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005464 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005465
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005466 for (; begin != end; ++begin)
5467 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5468
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005469 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005470 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471 ObjCTypes.ProtocolnfABIPtrTy));
5472
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005473 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005474 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005475 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005476 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005477 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005478 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005479 ProtocolRefs.size()),
5480 ProtocolRefs);
5481
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005482 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005483 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005484 llvm::GlobalValue::InternalLinkage,
5485 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005486 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005487 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005488 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005489 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005490 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005491 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005492 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005493}
5494
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005495/// GetMethodDescriptionConstant - This routine build following meta-data:
5496/// struct _objc_method {
5497/// SEL _cmd;
5498/// char *method_type;
5499/// char *_imp;
5500/// }
5501
5502llvm::Constant *
5503CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5504 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005505 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005506 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5507 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005508 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005509 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005510 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005511 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005512}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005513
5514/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5515/// This code gen. amounts to generating code for:
5516/// @code
5517/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5518/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005519///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005520LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005521 CodeGen::CodeGenFunction &CGF,
5522 QualType ObjectTy,
5523 llvm::Value *BaseValue,
5524 const ObjCIvarDecl *Ivar,
5525 unsigned CVRQualifiers) {
5526 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005527 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5528 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005529}
5530
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005531llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005532 CodeGen::CodeGenFunction &CGF,
5533 const ObjCInterfaceDecl *Interface,
5534 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005535 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005536}
5537
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005538CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005539 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005540 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005541 QualType ResultType,
5542 Selector Sel,
5543 llvm::Value *Receiver,
5544 QualType Arg0Ty,
5545 bool IsSuper,
5546 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005547 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5548 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005549 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005550 llvm::Value *Arg0 = Receiver;
5551 if (!IsSuper)
5552 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005553
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005554 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005555 // FIXME. This is too much work to get the ABI-specific result type needed to
5556 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005557 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005558 = Types.getFunctionInfo(ResultType, CallArgList(),
5559 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005560 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005561 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005562 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner396639d2010-08-18 16:09:06 +00005563 if (IsSuper) {
5564 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5565 Name += "objc_msgSendSuper2_stret_fixup";
5566 } else {
5567 Fn = ObjCTypes.getMessageSendStretFixupFn();
5568 Name += "objc_msgSend_stret_fixup";
5569 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005570 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5571 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5572 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005573 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005574 if (IsSuper) {
5575 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5576 Name += "objc_msgSendSuper2_fixup";
5577 } else {
5578 Fn = ObjCTypes.getMessageSendFixupFn();
5579 Name += "objc_msgSend_fixup";
5580 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005581 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005582 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005583 Name += '_';
5584 std::string SelName(Sel.getAsString());
5585 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005586 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005587 if (SelName[i] == ':')
5588 SelName[i] = '_';
5589 Name += SelName;
5590 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5591 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005592 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005593 std::vector<llvm::Constant*> Values(2);
5594 Values[0] = Fn;
5595 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005596 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005597 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005598 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005599 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005600 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005601 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005602 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005603 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005604 }
5605 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005606
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005607 CallArgList ActualArgs;
5608 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005609 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005610 ObjCTypes.MessageRefCPtrTy));
5611 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005612 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005613 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005614 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5615 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005616 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005617 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005618 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005619 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005620}
5621
5622/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005623CodeGen::RValue
5624CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005625 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005626 QualType ResultType,
5627 Selector Sel,
5628 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005629 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005630 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005631 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005632 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005633 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5634 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005636 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005637 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005638 Receiver, CGF.getContext().getObjCIdType(),
5639 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005640}
5641
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005642llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005643CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005644 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5645
Daniel Dunbara6468342009-03-02 05:18:14 +00005646 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005647 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005648 false, llvm::GlobalValue::ExternalLinkage,
5649 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005650 }
5651
5652 return GV;
5653}
5654
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005655llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5656 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005657 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005658
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005659 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005660 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005661 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005662 Entry =
5663 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005664 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005665 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005666 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005667 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005668 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005669 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005670 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005671 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005672 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005673
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005674 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005675}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005676
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005677llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005678CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005679 const ObjCInterfaceDecl *ID) {
5680 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005681
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005682 if (!Entry) {
5683 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5684 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005685 Entry =
5686 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005687 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005688 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005689 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005690 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005691 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005692 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005693 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005694 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005695 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005696
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005697 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005698}
5699
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005700/// EmitMetaClassRef - Return a Value * of the address of _class_t
5701/// meta-data
5702///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5704 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005705 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5706 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005707 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005708
Daniel Dunbar15894b72009-04-07 05:48:37 +00005709 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005710 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005711 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005712 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005713 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005714 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005715 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005716 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005717 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 ObjCTypes.ClassnfABIPtrTy));
5719
Daniel Dunbare60aa052009-04-15 19:03:14 +00005720 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005721 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005722
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005723 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005724}
5725
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005726/// GetClass - Return a reference to the class for the given interface
5727/// decl.
5728llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5729 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005730 if (ID->hasAttr<WeakImportAttr>()) {
5731 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5732 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5733 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5734 }
5735
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005736 return EmitClassRef(Builder, ID);
5737}
5738
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005739/// Generates a message send where the super is the receiver. This is
5740/// a message send to self with special delivery semantics indicating
5741/// which class's method should be called.
5742CodeGen::RValue
5743CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005744 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005745 QualType ResultType,
5746 Selector Sel,
5747 const ObjCInterfaceDecl *Class,
5748 bool isCategoryImpl,
5749 llvm::Value *Receiver,
5750 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005751 const CodeGen::CallArgList &CallArgs,
5752 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005753 // ...
5754 // Create and init a super structure; this is a (receiver, class)
5755 // pair we will pass to objc_msgSendSuper.
5756 llvm::Value *ObjCSuper =
5757 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005758
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005759 llvm::Value *ReceiverAsObject =
5760 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5761 CGF.Builder.CreateStore(ReceiverAsObject,
5762 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005763
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005764 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005765 llvm::Value *Target;
5766 if (IsClassMessage) {
5767 if (isCategoryImpl) {
5768 // Message sent to "super' in a class method defined in
5769 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005770 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005771 Target = CGF.Builder.CreateStructGEP(Target, 0);
5772 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005773 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005774 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005775 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005776 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005777
Mike Stump18bb9282009-05-16 07:57:57 +00005778 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5779 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005780 const llvm::Type *ClassTy =
5781 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5782 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5783 CGF.Builder.CreateStore(Target,
5784 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005785
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005786 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005787 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5788 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005789 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005790 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005791 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005792 ObjCSuper, ObjCTypes.SuperPtrCTy,
5793 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005794}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005795
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005796llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005797 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005798 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005799
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005800 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005801 llvm::Constant *Casted =
5802 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5803 ObjCTypes.SelectorPtrTy);
5804 Entry =
5805 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5806 llvm::GlobalValue::InternalLinkage,
5807 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005808 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005809 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005810 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005811
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005812 if (lval)
5813 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005814 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005815}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005816/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005817/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005818///
5819void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005820 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005821 llvm::Value *dst,
5822 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005823 const llvm::Type * SrcTy = src->getType();
5824 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005825 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005826 assert(Size <= 8 && "does not support size > 8");
5827 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5828 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005829 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5830 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005831 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5832 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005833 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5834 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005835 return;
5836}
5837
5838/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5839/// objc_assign_strongCast (id src, id *dst)
5840///
5841void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005842 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005843 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005844 const llvm::Type * SrcTy = src->getType();
5845 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005846 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005847 assert(Size <= 8 && "does not support size > 8");
5848 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005849 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005850 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5851 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005852 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5853 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005854 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005855 src, dst, "weakassign");
5856 return;
5857}
5858
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005859void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005860 CodeGen::CodeGenFunction &CGF,
5861 llvm::Value *DestPtr,
5862 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005863 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005864 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5865 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005866 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005867 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005868 return;
5869}
5870
Fariborz Jahanian06292952009-02-16 22:52:32 +00005871/// EmitObjCWeakRead - Code gen for loading value of a __weak
5872/// object: objc_read_weak (id *src)
5873///
5874llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005876 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005877 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005878 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5879 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005880 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005881 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005882 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005883 return read_weak;
5884}
5885
5886/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5887/// objc_assign_weak (id src, id *dst)
5888///
5889void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005890 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005891 const llvm::Type * SrcTy = src->getType();
5892 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005893 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005894 assert(Size <= 8 && "does not support size > 8");
5895 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5896 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005897 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5898 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005899 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5900 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005901 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005902 src, dst, "weakassign");
5903 return;
5904}
5905
5906/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5907/// objc_assign_global (id src, id *dst)
5908///
5909void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005910 llvm::Value *src, llvm::Value *dst,
5911 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005912 const llvm::Type * SrcTy = src->getType();
5913 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005914 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005915 assert(Size <= 8 && "does not support size > 8");
5916 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5917 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005918 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5919 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005920 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5921 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005922 if (!threadlocal)
5923 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5924 src, dst, "globalassign");
5925 else
5926 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5927 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005928 return;
5929}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005930
John McCallc20acd32010-07-21 00:41:47 +00005931namespace {
John McCallcda666c2010-07-21 07:22:38 +00005932 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00005933 llvm::Value *SyncExitFn;
5934 llvm::Value *SyncArg;
5935 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5936 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5937
5938 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5939 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5940 }
5941 };
5942}
5943
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005944void
John McCallbd309292010-07-06 01:34:17 +00005945CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5946 const ObjCAtSynchronizedStmt &S) {
5947 // Evaluate the lock operand. This should dominate the cleanup.
5948 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005949
John McCallbd309292010-07-06 01:34:17 +00005950 // Acquire the lock.
5951 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5952 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5953 ->setDoesNotThrow();
5954
5955 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00005956 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5957 ObjCTypes.getSyncExitFn(),
5958 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005959
John McCallbd309292010-07-06 01:34:17 +00005960 // Emit the body of the statement.
5961 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005962
John McCallbd309292010-07-06 01:34:17 +00005963 // Pop the lock-release cleanup.
5964 CGF.PopCleanupBlock();
5965}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005966
John McCallbd309292010-07-06 01:34:17 +00005967namespace {
5968 struct CatchHandler {
5969 const VarDecl *Variable;
5970 const Stmt *Body;
5971 llvm::BasicBlock *Block;
5972 llvm::Value *TypeInfo;
5973 };
John McCall5c08ab92010-07-13 22:12:14 +00005974
John McCallcda666c2010-07-21 07:22:38 +00005975 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00005976 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5977 MightThrow(MightThrow), Fn(Fn) {}
5978 bool MightThrow;
5979 llvm::Value *Fn;
5980
5981 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5982 if (!MightThrow) {
5983 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5984 return;
5985 }
5986
5987 CGF.EmitCallOrInvoke(Fn, 0, 0);
5988 }
5989 };
John McCallbd309292010-07-06 01:34:17 +00005990}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005991
John McCall2ca705e2010-07-24 00:37:23 +00005992llvm::Constant *
5993CGObjCNonFragileABIMac::GetEHType(QualType T) {
5994 // There's a particular fixed type info for 'id'.
5995 if (T->isObjCIdType() ||
5996 T->isObjCQualifiedIdType()) {
5997 llvm::Constant *IDEHType =
5998 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5999 if (!IDEHType)
6000 IDEHType =
6001 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6002 false,
6003 llvm::GlobalValue::ExternalLinkage,
6004 0, "OBJC_EHTYPE_id");
6005 return IDEHType;
6006 }
6007
6008 // All other types should be Objective-C interface pointer types.
6009 const ObjCObjectPointerType *PT =
6010 T->getAs<ObjCObjectPointerType>();
6011 assert(PT && "Invalid @catch type.");
6012 const ObjCInterfaceType *IT = PT->getInterfaceType();
6013 assert(IT && "Invalid @catch type.");
6014 return GetInterfaceEHType(IT->getDecl(), false);
6015}
6016
John McCallbd309292010-07-06 01:34:17 +00006017void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6018 const ObjCAtTryStmt &S) {
6019 // Jump destination for falling out of catch bodies.
6020 CodeGenFunction::JumpDest Cont;
6021 if (S.getNumCatchStmts())
6022 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006023
John McCallbd309292010-07-06 01:34:17 +00006024 CodeGenFunction::FinallyInfo FinallyInfo;
6025 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6026 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6027 ObjCTypes.getObjCBeginCatchFn(),
6028 ObjCTypes.getObjCEndCatchFn(),
6029 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006030
John McCallbd309292010-07-06 01:34:17 +00006031 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006032
John McCallbd309292010-07-06 01:34:17 +00006033 // Enter the catch, if there is one.
6034 if (S.getNumCatchStmts()) {
6035 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6036 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006037 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006038
John McCallbd309292010-07-06 01:34:17 +00006039 Handlers.push_back(CatchHandler());
6040 CatchHandler &Handler = Handlers.back();
6041 Handler.Variable = CatchDecl;
6042 Handler.Body = CatchStmt->getCatchBody();
6043 Handler.Block = CGF.createBasicBlock("catch");
6044
6045 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006046 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006047 Handler.TypeInfo = 0; // catch-all
6048 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006049 break;
6050 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006051
John McCall2ca705e2010-07-24 00:37:23 +00006052 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006053 }
John McCallbd309292010-07-06 01:34:17 +00006054
6055 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6056 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6057 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006058 }
John McCallbd309292010-07-06 01:34:17 +00006059
6060 // Emit the try body.
6061 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006062
John McCallbd309292010-07-06 01:34:17 +00006063 // Leave the try.
6064 if (S.getNumCatchStmts())
6065 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006066
John McCallbd309292010-07-06 01:34:17 +00006067 // Remember where we were.
6068 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006069
John McCallbd309292010-07-06 01:34:17 +00006070 // Emit the handlers.
6071 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6072 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006073
John McCallbd309292010-07-06 01:34:17 +00006074 CGF.EmitBlock(Handler.Block);
6075 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006076
John McCallbd309292010-07-06 01:34:17 +00006077 // Enter the catch.
6078 llvm::CallInst *Exn =
6079 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6080 "exn.adjusted");
6081 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006082
John McCallbd309292010-07-06 01:34:17 +00006083 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006084 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006085 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6086 EndCatchMightThrow,
6087 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006088
John McCallbd309292010-07-06 01:34:17 +00006089 // Bind the catch parameter if it exists.
6090 if (const VarDecl *CatchParam = Handler.Variable) {
6091 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6092 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006093
John McCallbd309292010-07-06 01:34:17 +00006094 CGF.EmitLocalBlockVarDecl(*CatchParam);
6095 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006096 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006097
John McCallbd309292010-07-06 01:34:17 +00006098 CGF.ObjCEHValueStack.push_back(Exn);
6099 CGF.EmitStmt(Handler.Body);
6100 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006101
John McCallbd309292010-07-06 01:34:17 +00006102 // Leave the earlier cleanup.
6103 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006104
John McCallbd309292010-07-06 01:34:17 +00006105 CGF.EmitBranchThroughCleanup(Cont);
6106 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006107
John McCallbd309292010-07-06 01:34:17 +00006108 // Go back to the try-statement fallthrough.
6109 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006110
John McCallbd309292010-07-06 01:34:17 +00006111 // Pop out of the normal cleanup on the finally.
6112 if (S.getFinallyStmt())
6113 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006114
John McCallad5d61e2010-07-23 21:56:41 +00006115 if (Cont.isValid())
6116 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006117}
6118
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006119/// EmitThrowStmt - Generate code for a throw statement.
6120void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6121 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006122 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006123 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006124 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006125 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006126 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006127 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006128 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006129 "Unexpected rethrow outside @catch block.");
6130 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006131 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006132 }
6133
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006134 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006135 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6136 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6137 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006138 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00006139 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006140 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00006141 } else {
6142 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6143 ->setDoesNotReturn();
6144 CGF.Builder.CreateUnreachable();
6145 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006146
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006147 // Clear the insertion point to indicate we are in unreachable code.
6148 CGF.Builder.ClearInsertionPoint();
6149}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006150
John McCall2ca705e2010-07-24 00:37:23 +00006151llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006152CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006153 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006154 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006155
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006156 // If we don't need a definition, return the entry if found or check
6157 // if we use an external reference.
6158 if (!ForDefinition) {
6159 if (Entry)
6160 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006161
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006162 // If this type (or a super class) has the __objc_exception__
6163 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006164 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006165 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006166 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006167 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006168 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006169 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006170 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006171 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006172
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006173 // Otherwise we need to either make a new entry or fill in the
6174 // initializer.
6175 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006176 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006177 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006178 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006179 CGM.getModule().getGlobalVariable(VTableName);
6180 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006181 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6182 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006183 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006184 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006185
Chris Lattner5e016ae2010-06-27 07:15:29 +00006186 llvm::Value *VTableIdx =
6187 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006188
6189 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006190 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006191 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006192 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006193 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006194 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006195
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006196 if (Entry) {
6197 Entry->setInitializer(Init);
6198 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006199 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006200 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006201 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006202 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006203 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006204 }
6205
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006206 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006207 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006208 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6209 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006210
6211 if (ForDefinition) {
6212 Entry->setSection("__DATA,__objc_const");
6213 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6214 } else {
6215 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6216 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006217
6218 return Entry;
6219}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006220
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006221/* *** */
6222
Daniel Dunbarb036db82008-08-13 03:21:16 +00006223CodeGen::CGObjCRuntime *
6224CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006225 return new CGObjCMac(CGM);
6226}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006227
6228CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006229CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006230 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006231}