blob: b3921a4816541500759a9ecbecb00aa09c5a2a98 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallf1549f62010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000027
John McCall87bb5822010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000043
Daniel Dunbar97776872009-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 Dunbar1d7e5392009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000053
Daniel Dunbar61ac1d22010-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 Dunbar3a2c80f2010-04-02 15:43:29 +000057
Daniel Dunbar532d4da2009-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 Dunbar6bff2512009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-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 Dunbare83be122010-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 Dunbar532d4da2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-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 Dunbar97776872009-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 Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000109
John McCall0953e762009-09-24 19:53:00 +0000110 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
111 Quals.addCVRQualifiers(CVRQualifiers);
112
Daniel Dunbar56229f52010-04-05 16:20:33 +0000113 if (!Ivar->isBitField())
114 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000115
Daniel Dunbar56229f52010-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 Dunbar97776872009-04-22 07:32:20 +0000123
Daniel Dunbarf0fe5bc2010-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 Dunbarf0fe5bc2010-04-05 21:36:35 +0000130
Daniel Dunbarab970f92010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-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 Dunbar2df25692010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbar56229f52010-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 Dunbarefbf4872010-04-06 01:07:44 +0000147 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000148}
149
150///
151
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000152namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000153
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000154typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000155
Daniel Dunbar6bff2512009-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 Dunbar6efc0c52008-08-13 03:21:16 +0000158
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000159class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000160protected:
161 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000170 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
171 Params, true),
172 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000173 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174
Fariborz Jahaniand0f8a8d2009-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 Anderson0032b272009-08-13 21:57:51 +0000181 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182 Params, true),
183 "objc_msgSend_stret");
184
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000186
Fariborz Jahaniand0f8a8d2009-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 Anderson0032b272009-08-13 21:57:51 +0000194 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
195 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000196 Params,
197 true),
198 "objc_msgSend_fpret");
199
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000201
Fariborz Jahaniand0f8a8d2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000209 Params, true),
210 SuperName);
211 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Fariborz Jahaniand0f8a8d2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000219 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000220 Params, true),
221 SuperName);
222 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000223
Fariborz Jahaniand0f8a8d2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000231 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000232 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000233 Params, true),
234 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000235 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000236
Fariborz Jahaniand0f8a8d2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000244 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000245 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000246 Params, true),
247 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000248 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000249
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000254
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000259
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260protected:
261 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000262
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000263public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000264 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000265 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000267 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
268 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000270 /// PtrObjectPtrTy - LLVM type for id *
271 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000273 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000274 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000275 /// ProtocolPtrTy - LLVM type for external protocol handles
276 /// (typeof(Protocol))
277 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000278
Daniel Dunbar19cd87e2008-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 Dunbar6bff2512009-08-03 17:06:42 +0000283
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000284 /// SuperTy - LLVM type for struct objc_super.
285 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000286 /// SuperPtrTy - LLVM type for struct objc_super *.
287 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000288
Fariborz Jahanian30bc5712009-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 Dunbar6bff2512009-08-03 17:06:42 +0000292
Fariborz Jahanian30bc5712009-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 Dunbar6bff2512009-08-03 17:06:42 +0000298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000299 // MethodTy - LLVM type for struct objc_method.
300 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000301
Fariborz Jahaniand55b6fc2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000306
Chris Lattner72db6c32009-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 McCallead608a2010-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 Lattner72db6c32009-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 McCall04a67a62010-02-05 21:31:56 +0000319 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000320 FunctionType::ExtInfo()),
321 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000324
Chris Lattner72db6c32009-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 McCallead608a2010-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 Lattner72db6c32009-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 McCall04a67a62010-02-05 21:31:56 +0000339 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000340 FunctionType::ExtInfo()),
341 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000344
Fariborz Jahanian6cc59062010-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 Lattner72db6c32009-04-22 02:44:54 +0000363 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
365 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000366 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000367 llvm::SmallVector<CanQualType,1> Params;
368 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000369 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000370 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000371 FunctionType::ExtInfo()),
372 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
374 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000375
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000376 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-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 Dunbar6bff2512009-08-03 17:06:42 +0000381 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000382 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000384 }
385
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000386 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-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 Anderson96e0fc72009-07-29 22:16:19 +0000392 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000395
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000396 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-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 Andersona1cf15f2009-07-14 23:10:40 +0000401 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000402 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000403 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000405
Fariborz Jahanian021a7a62010-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 Jahaniandb286862009-01-22 00:37:21 +0000416 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000418 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000419 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
420 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000421 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000422 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000423 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
425 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000426
Fariborz Jahanian082b02e2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
435 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000436
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000437 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000438 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000439 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000440 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
441 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000443 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
445 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000446
447 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-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 Anderson0032b272009-08-13 21:57:51 +0000452 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000453 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000455
Fariborz Jahanian69677ea2010-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 Dunbar1c566672009-02-24 01:43:46 +0000465 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-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 Anderson0032b272009-08-13 21:57:51 +0000470 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000471 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000473
Daniel Dunbar1c566672009-02-24 01:43:46 +0000474 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-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 Anderson0032b272009-08-13 21:57:51 +0000479 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000480 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
481 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000482
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000486
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
489 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000490
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendStretFn(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
493 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000494
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn2(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
497 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000498
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendFpretFn(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
501 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
505 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000506
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000507 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
508 ~ObjCCommonTypesHelper(){}
509};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000510
Fariborz Jahanianee0af742009-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 Jahanianee0af742009-01-21 22:04:16 +0000514public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000515 /// SymtabTy - LLVM type for struct objc_symtab.
516 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000517 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
518 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000519 /// ModuleTy - LLVM type for struct objc_module.
520 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000521
Daniel Dunbar6efc0c52008-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 Dunbar6efc0c52008-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 Dunbar86e253a2008-08-22 20:34:54 +0000545 /// CategoryTy - LLVM type for struct objc_category.
546 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar6bff2512009-08-03 17:06:42 +0000565
Anders Carlsson124526b2008-09-09 10:10:21 +0000566 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
567 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Anders Carlsson124526b2008-09-09 10:10:21 +0000569 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionTryEnterFn() {
571 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000572 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000573 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000574 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575 Params, false),
576 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000578
579 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000580 llvm::Constant *getExceptionTryExitFn() {
581 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000582 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000583 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000584 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585 Params, false),
586 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000588
589 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000590 llvm::Constant *getExceptionExtractFn() {
591 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000592 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000594 Params, false),
595 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000596
Chris Lattner34b02a12009-04-22 02:26:14 +0000597 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Anders Carlsson124526b2008-09-09 10:10:21 +0000599 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-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 Dunbar6bff2512009-08-03 17:06:42 +0000604 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000605 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606 Params, false),
607 "objc_exception_match");
608
Chris Lattner34b02a12009-04-22 02:26:14 +0000609 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610
Anders Carlsson124526b2008-09-09 10:10:21 +0000611 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000612 llvm::Constant *getSetJmpFn() {
613 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000614 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000615 return
Owen Anderson0032b272009-08-13 21:57:51 +0000616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000617 Params, false),
618 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Chris Lattner34b02a12009-04-22 02:26:14 +0000620 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000622public:
623 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000624 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000625};
626
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000627/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000628/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000629class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000630public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // MethodListnfABITy - LLVM for struct _method_list_t
633 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
636 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // ProtocolnfABITy = LLVM for struct _protocol_t
639 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Daniel Dunbar948e2582009-02-15 07:36:20 +0000641 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
642 const llvm::Type *ProtocolnfABIPtrTy;
643
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000644 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
645 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000647 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
648 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000650 // ClassnfABITy - LLVM for struct _class_t
651 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000652
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000653 // ClassnfABIPtrTy - LLVM for struct _class_t*
654 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000656 // IvarnfABITy - LLVM for struct _ivar_t
657 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000659 // IvarListnfABITy - LLVM for struct _ivar_list_t
660 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000662 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
663 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000665 // ClassRonfABITy - LLVM for struct _class_ro_t
666 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000668 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
669 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000671 // CategorynfABITy - LLVM for struct _category_t
672 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000673
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000674 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000675
Fariborz Jahanian2e4672b2009-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 Jahanian83a8a752009-02-04 20:42:28 +0000682 // MessageRefCTy - clang type for struct _message_ref_t
683 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000685 // MessageRefPtrTy - LLVM for struct _message_ref_t*
686 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000687 // MessageRefCPtrTy - clang type for struct _message_ref_t*
688 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Fariborz Jahanianef163782009-02-05 01:13:09 +0000690 // MessengerTy - Type of the messenger (shown as IMP above)
691 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000692
Fariborz Jahanian2e4672b2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000699
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000700 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
701 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSend_fixup");
711 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000719 Params, true),
720 "objc_msgSend_fpret_fixup");
721 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000722
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000728 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000729 Params, true),
730 "objc_msgSend_stret_fixup");
731 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Chris Lattner1c02f862009-04-22 02:53:24 +0000733 llvm::Constant *getMessageSendIdFixupFn() {
734 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
735 std::vector<const llvm::Type*> Params;
736 Params.push_back(ObjectPtrTy);
737 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000738 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000739 Params, true),
740 "objc_msgSendId_fixup");
741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742
Chris Lattner1c02f862009-04-22 02:53:24 +0000743 llvm::Constant *getMessageSendIdStretFixupFn() {
744 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
745 std::vector<const llvm::Type*> Params;
746 Params.push_back(ObjectPtrTy);
747 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000748 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000749 Params, true),
750 "objc_msgSendId_stret_fixup");
751 }
752 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000754 // struct _super_message_ref_t*, ...)
755 std::vector<const llvm::Type*> Params;
756 Params.push_back(SuperPtrTy);
757 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000758 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000759 Params, true),
760 "objc_msgSendSuper2_fixup");
761 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Chris Lattner1c02f862009-04-22 02:53:24 +0000763 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000765 // struct _super_message_ref_t*, ...)
766 std::vector<const llvm::Type*> Params;
767 Params.push_back(SuperPtrTy);
768 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000770 Params, true),
771 "objc_msgSendSuper2_stret_fixup");
772 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773
774
775
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000776 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
777 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000778 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000780 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000781 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000783 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000784 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000785
Chris Lattner8a569112009-04-22 02:15:23 +0000786 llvm::Constant *getUnwindResumeOrRethrowFn() {
787 std::vector<const llvm::Type*> Params;
788 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000789 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000790 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000792 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
793 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000794 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Chris Lattner8a569112009-04-22 02:15:23 +0000796 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000797 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000798 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000799 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000800
Chris Lattner8a569112009-04-22 02:15:23 +0000801 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Chris Lattner8a569112009-04-22 02:15:23 +0000803 llvm::Constant *getObjCBeginCatchFn() {
804 std::vector<const llvm::Type*> Params;
805 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000806 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000807 Params, false),
808 "objc_begin_catch");
809 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000810
811 const llvm::StructType *EHTypeTy;
812 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000813
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000814 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
815 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000816};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000817
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000818class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000819public:
820 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000821 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000822 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000823 unsigned ivar_bytepos;
824 unsigned ivar_size;
825 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000826 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000827
828 // Allow sorting based on byte pos.
829 bool operator<(const GC_IVAR &b) const {
830 return ivar_bytepos < b.ivar_bytepos;
831 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000832 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000833
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000834 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000835 public:
836 unsigned skip;
837 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000839 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000840 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000842protected:
843 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000844 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000845 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000846 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000848 // gc ivar layout bitmap calculation helper caches.
849 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
850 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000852 /// LazySymbols - Symbols to generate a lazy reference for. See
853 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000854 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000855
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000856 /// DefinedSymbols - External symbols which are defined by this
857 /// module. The symbols in this list and LazySymbols are used to add
858 /// special linker symbols which ensure that Objective-C modules are
859 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000860 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000861
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000862 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000863 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000864
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000865 /// MethodVarNames - uniqued method variable names.
866 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000867
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000868 /// DefinedCategoryNames - list of category names in form Class_Category.
869 llvm::SetVector<std::string> DefinedCategoryNames;
870
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000871 /// MethodVarTypes - uniqued method type signatures. We have to use
872 /// a StringMap here because have no other unique reference.
873 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000874
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000875 /// MethodDefinitions - map of methods which have been defined in
876 /// this translation unit.
877 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000878
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000879 /// PropertyNames - uniqued method variable names.
880 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 /// ClassReferences - uniqued class references.
883 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000885 /// SelectorReferences - uniqued selector references.
886 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000888 /// Protocols - Protocols for which an objc_protocol structure has
889 /// been emitted. Forward declarations are handled by creating an
890 /// empty structure whose initializer is filled in when/if defined.
891 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000893 /// DefinedProtocols - Protocols which have actually been
894 /// defined. We should not need this, see FIXME in GenerateProtocol.
895 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000896
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000897 /// DefinedClasses - List of defined classes.
898 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000899
900 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
901 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000903 /// DefinedCategories - List of defined categories.
904 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000905
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000906 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
907 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000909 /// GetNameForMethod - Return a name for the given method.
910 /// \param[out] NameOut - The return value.
911 void GetNameForMethod(const ObjCMethodDecl *OMD,
912 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000913 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000915 /// GetMethodVarName - Return a unique constant for the given
916 /// selector's name. The return value has type char *.
917 llvm::Constant *GetMethodVarName(Selector Sel);
918 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
919 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000921 /// GetMethodVarType - Return a unique constant for the given
922 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000924 // FIXME: This is a horrible name.
925 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000926 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000928 /// GetPropertyName - Return a unique constant for the given
929 /// name. The return value has type char *.
930 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000932 // FIXME: This can be dropped once string functions are unified.
933 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
934 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000935
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000936 /// GetClassName - Return a unique constant for the given selector's
937 /// name. The return value has type char *.
938 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000939
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000940 /// BuildIvarLayout - Builds ivar layout bitmap for the class
941 /// implementation for the __strong or __weak case.
942 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000943 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
944 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000945
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000946 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000947
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000948 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000949 unsigned int BytePos, bool ForStrongLayout,
950 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000951 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000952 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000953 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000954 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000955 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000956 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000957
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000958 /// GetIvarLayoutName - Returns a unique constant for the given
959 /// ivar layout bitmap.
960 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
961 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000962
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000963 /// EmitPropertyList - Emit the given property list. The return
964 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000965 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000966 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000967 const ObjCContainerDecl *OCD,
968 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000969
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000970 /// PushProtocolProperties - Push protocol's property on the input stack.
971 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
972 std::vector<llvm::Constant*> &Properties,
973 const Decl *Container,
974 const ObjCProtocolDecl *PROTO,
975 const ObjCCommonTypesHelper &ObjCTypes);
976
Fariborz Jahanianda320092009-01-29 19:24:30 +0000977 /// GetProtocolRef - Return a reference to the internal protocol
978 /// description, creating an empty one if it has not been
979 /// defined. The return value has type ProtocolPtrTy.
980 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000981
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000982 /// CreateMetadataVar - Create a global variable with internal
983 /// linkage for use by the Objective-C runtime.
984 ///
985 /// This is a convenience wrapper which not only creates the
986 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000987 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000988 ///
989 /// \param Name - The variable name.
990 /// \param Init - The variable initializer; this is also used to
991 /// define the type of the variable.
992 /// \param Section - The section the variable should go into, or 0.
993 /// \param Align - The alignment for the variable, or 0.
994 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000995 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000996 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000997 llvm::Constant *Init,
998 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000999 unsigned Align,
1000 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001001
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001002 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001003 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001004 QualType ResultType,
1005 llvm::Value *Sel,
1006 llvm::Value *Arg0,
1007 QualType Arg0Ty,
1008 bool IsSuper,
1009 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001010 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001011 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001012
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001013 /// EmitImageInfo - Emit the image info marker used to encode some module
1014 /// level information.
1015 void EmitImageInfo();
1016
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001017public:
Owen Anderson69243822009-07-13 04:10:07 +00001018 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001019 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001020
David Chisnall0d13f6f2010-01-23 02:40:42 +00001021 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001022
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001023 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1024 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001025
Fariborz Jahanianda320092009-01-29 19:24:30 +00001026 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027
Fariborz Jahanianda320092009-01-29 19:24:30 +00001028 /// GetOrEmitProtocol - Get the protocol object for the given
1029 /// declaration, emitting it if necessary. The return value has type
1030 /// ProtocolPtrTy.
1031 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001032
Fariborz Jahanianda320092009-01-29 19:24:30 +00001033 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1034 /// object for the given declaration, emitting it if needed. These
1035 /// forward references will be filled in with empty bodies if no
1036 /// definition is seen. The return value has type ProtocolPtrTy.
1037 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001038 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1039 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1040
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001041};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001042
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001043class CGObjCMac : public CGObjCCommonMac {
1044private:
1045 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001046
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001047 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001048 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001049 void EmitModuleInfo();
1050
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001051 /// EmitModuleSymols - Emit module symbols, the list of defined
1052 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001053 llvm::Constant *EmitModuleSymbols();
1054
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001055 /// FinishModule - Write out global data structures at the end of
1056 /// processing a translation unit.
1057 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001058
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001059 /// EmitClassExtension - Generate the class extension structure used
1060 /// to store the weak ivar layout and properties. The return value
1061 /// has type ClassExtensionPtrTy.
1062 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1063
1064 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1065 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001066 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001067 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001068
1069 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1070 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001071
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001072 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001073 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001074 QualType ResultType,
1075 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001076 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001077 QualType Arg0Ty,
1078 bool IsSuper,
1079 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001080
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001081 /// EmitIvarList - Emit the ivar list for the given
1082 /// implementation. If ForClass is true the list of class ivars
1083 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1084 /// interface ivars will be emitted. The return value has type
1085 /// IvarListPtrTy.
1086 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001087 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001088
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 /// EmitMetaClass - Emit a forward reference to the class structure
1090 /// for the metaclass of the given interface. The return value has
1091 /// type ClassPtrTy.
1092 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1093
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001094 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001095 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001096 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1097 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001098 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001099
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001101
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001102 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001103
1104 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001105 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001106 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001107 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001108 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001109
1110 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001111 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001112 /// - TypeName: The name for the type containing the methods.
1113 /// - IsProtocol: True iff these methods are for a protocol.
1114 /// - ClassMethds: True iff these are class methods.
1115 /// - Required: When true, only "required" methods are
1116 /// listed. Similarly, when false only "optional" methods are
1117 /// listed. For classes this should always be true.
1118 /// - begin, end: The method list to output.
1119 ///
1120 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001121 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001122 const char *Section,
1123 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001124
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001125 /// GetOrEmitProtocol - Get the protocol object for the given
1126 /// declaration, emitting it if necessary. The return value has type
1127 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001128 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001129
1130 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1131 /// object for the given declaration, emitting it if needed. These
1132 /// forward references will be filled in with empty bodies if no
1133 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001134 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001135
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001136 /// EmitProtocolExtension - Generate the protocol extension
1137 /// structure used to store optional instance and class methods, and
1138 /// protocol properties. The return value has type
1139 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001140 llvm::Constant *
1141 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1142 const ConstantVector &OptInstanceMethods,
1143 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001144
1145 /// EmitProtocolList - Generate the list of referenced
1146 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001147 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001148 ObjCProtocolDecl::protocol_iterator begin,
1149 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001150
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001151 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1152 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001153 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1154 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001155
1156public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001157 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001158
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001159 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001160
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001161 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001162 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001163 QualType ResultType,
1164 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001165 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001166 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001167 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001168 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001169
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001170 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001171 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001172 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001173 QualType ResultType,
1174 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001175 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001176 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001177 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001178 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001179 const CallArgList &CallArgs,
1180 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001182 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001183 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001184
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001185 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1186 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001187
1188 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1189 /// untyped one.
1190 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1191 const ObjCMethodDecl *Method);
1192
John McCall5a180392010-07-24 00:37:23 +00001193 virtual llvm::Constant *GetEHType(QualType T);
1194
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001195 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001196
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001197 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001198
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001199 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001200 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001201
Chris Lattner74391b42009-03-22 21:03:39 +00001202 virtual llvm::Constant *GetPropertyGetFunction();
1203 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001204 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001205 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001206
John McCallf1549f62010-07-06 01:34:17 +00001207 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1208 const ObjCAtTryStmt &S);
1209 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1210 const ObjCAtSynchronizedStmt &S);
1211 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001212 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1213 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001214 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001215 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001216 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001217 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001218 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001219 llvm::Value *src, llvm::Value *dest,
1220 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001221 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001222 llvm::Value *src, llvm::Value *dest,
1223 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001224 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1225 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001226 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1227 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001228 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001229
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001230 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1231 QualType ObjectTy,
1232 llvm::Value *BaseValue,
1233 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001234 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001235 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001236 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001237 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001238};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001239
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001240class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001241private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001242 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001243 llvm::GlobalVariable* ObjCEmptyCacheVar;
1244 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245
Daniel Dunbar11394522009-04-18 08:51:00 +00001246 /// SuperClassReferences - uniqued super class references.
1247 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001248
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001249 /// MetaClassReferences - uniqued meta class references.
1250 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001251
1252 /// EHTypeReferences - uniqued class ehtype references.
1253 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001254
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001255 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001256 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001257 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001258
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001259 /// DefinedMetaClasses - List of defined meta-classes.
1260 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1261
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001262 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001263 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001264 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001265
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001266 /// FinishNonFragileABIModule - Write out global data structures at the end of
1267 /// processing a translation unit.
1268 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001269
Daniel Dunbar463b8762009-05-15 21:48:48 +00001270 /// AddModuleClassList - Add the given list of class pointers to the
1271 /// module with the provided symbol and section names.
1272 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1273 const char *SymbolName,
1274 const char *SectionName);
1275
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001276 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1277 unsigned InstanceStart,
1278 unsigned InstanceSize,
1279 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001280 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001282 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001283 llvm::Constant *ClassRoGV,
1284 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001285
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001286 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001288 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001289
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001290 /// EmitMethodList - Emit the method list for the given
1291 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001292 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001293 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001294 const ConstantVector &Methods);
1295 /// EmitIvarList - Emit the ivar list for the given
1296 /// implementation. If ForClass is true the list of class ivars
1297 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1298 /// interface ivars will be emitted. The return value has type
1299 /// IvarListnfABIPtrTy.
1300 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001302 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001303 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001304 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001305
Fariborz Jahanianda320092009-01-29 19:24:30 +00001306 /// GetOrEmitProtocol - Get the protocol object for the given
1307 /// declaration, emitting it if necessary. The return value has type
1308 /// ProtocolPtrTy.
1309 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Fariborz Jahanianda320092009-01-29 19:24:30 +00001311 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1312 /// object for the given declaration, emitting it if needed. These
1313 /// forward references will be filled in with empty bodies if no
1314 /// definition is seen. The return value has type ProtocolPtrTy.
1315 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001316
Fariborz Jahanianda320092009-01-29 19:24:30 +00001317 /// EmitProtocolList - Generate the list of referenced
1318 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001319 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001320 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001321 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001322
Fariborz Jahanian46551122009-02-04 00:22:57 +00001323 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001324 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001325 QualType ResultType,
1326 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001327 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001328 QualType Arg0Ty,
1329 bool IsSuper,
1330 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001331
1332 /// GetClassGlobal - Return the global variable for the Objective-C
1333 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001334 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001335
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001336 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001337 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001338 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001339 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001340
Daniel Dunbar11394522009-04-18 08:51:00 +00001341 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1342 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001343 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1344 const ObjCInterfaceDecl *ID);
1345
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001346 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1347 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001348 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001349 const ObjCInterfaceDecl *ID);
1350
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001351 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1352 /// the given ivar.
1353 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001354 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001355 const ObjCInterfaceDecl *ID,
1356 const ObjCIvarDecl *Ivar);
1357
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001358 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1359 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001360 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1361 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001362
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001363 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001364 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001365 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001366 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001367
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001368 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001369 return "OBJC_METACLASS_$_";
1370 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001371
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001372 const char *getClassSymbolPrefix() const {
1373 return "OBJC_CLASS_$_";
1374 }
1375
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001376 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001377 uint32_t &InstanceStart,
1378 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001380 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001381 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001382 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1383 return CGM.getContext().Selectors.getSelector(0, &II);
1384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001385
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001386 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001387 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1388 return CGM.getContext().Selectors.getSelector(1, &II);
1389 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001390
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001391 /// ImplementationIsNonLazy - Check whether the given category or
1392 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001393 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001394
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001395public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001396 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001397 // FIXME. All stubs for now!
1398 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001399
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001400 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001401 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001402 QualType ResultType,
1403 Selector Sel,
1404 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001405 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001406 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001407 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
1409 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001410 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001411 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001412 QualType ResultType,
1413 Selector Sel,
1414 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001415 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001416 llvm::Value *Receiver,
1417 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001418 const CallArgList &CallArgs,
1419 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001420
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001421 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001422 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001423
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001424 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1425 bool lvalue = false)
1426 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001427
1428 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1429 /// untyped one.
1430 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1431 const ObjCMethodDecl *Method)
1432 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001433
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001434 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001435
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001436 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001437 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001438 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001439
John McCall5a180392010-07-24 00:37:23 +00001440 virtual llvm::Constant *GetEHType(QualType T);
1441
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001442 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001443 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001444 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001445 virtual llvm::Constant *GetPropertySetFunction() {
1446 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001447 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001448
1449 virtual llvm::Constant *GetCopyStructFunction() {
1450 return ObjCTypes.getCopyStructFn();
1451 }
1452
Chris Lattner74391b42009-03-22 21:03:39 +00001453 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001454 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001456
John McCallf1549f62010-07-06 01:34:17 +00001457 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1458 const ObjCAtTryStmt &S);
1459 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1460 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001461 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001462 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001463 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001464 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001465 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001466 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001467 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001468 llvm::Value *src, llvm::Value *dest,
1469 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001470 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001471 llvm::Value *src, llvm::Value *dest,
1472 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001473 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001474 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001475 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1476 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001477 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001478 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1479 QualType ObjectTy,
1480 llvm::Value *BaseValue,
1481 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001482 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001483 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001484 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001485 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001486};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001487
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001488} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001489
1490/* *** Helper Functions *** */
1491
1492/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001493static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001494 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001495 unsigned idx0,
1496 unsigned idx1) {
1497 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001498 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1499 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001500 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001501 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001502}
1503
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001504/// hasObjCExceptionAttribute - Return true if this class or any super
1505/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001506static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001507 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001508 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001509 return true;
1510 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001511 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001512 return false;
1513}
1514
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001515/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001516
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001517CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001518 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001519 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001520 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001521}
1522
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001523/// GetClass - Return a reference to the class for the given interface
1524/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001525llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001526 const ObjCInterfaceDecl *ID) {
1527 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001528}
1529
1530/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001531llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1532 bool lval) {
1533 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001534}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001535llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001536 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001537 return EmitSelector(Builder, Method->getSelector());
1538}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001539
John McCall5a180392010-07-24 00:37:23 +00001540llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1541 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1542 return 0;
1543}
1544
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001545/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001546/*
1547 struct __builtin_CFString {
1548 const int *isa; // point to __CFConstantStringClassReference
1549 int flags;
1550 const char *str;
1551 long length;
1552 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001553*/
1554
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001555/// or Generate a constant NSString object.
1556/*
1557 struct __builtin_NSString {
1558 const int *isa; // point to __NSConstantStringClassReference
1559 const char *str;
1560 unsigned int length;
1561 };
1562*/
1563
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001564llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001565 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001566 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1567 CGM.GetAddrOfConstantCFString(SL) :
1568 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001569}
1570
1571/// Generates a message send where the super is the receiver. This is
1572/// a message send to self with special delivery semantics indicating
1573/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001574CodeGen::RValue
1575CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001576 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001577 QualType ResultType,
1578 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001579 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001580 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001581 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001582 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001583 const CodeGen::CallArgList &CallArgs,
1584 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001585 // Create and init a super structure; this is a (receiver, class)
1586 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001587 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001588 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001589 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001590 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001591 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001592 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001593
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001594 // If this is a class message the metaclass is passed as the target.
1595 llvm::Value *Target;
1596 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001597 if (isCategoryImpl) {
1598 // Message sent to 'super' in a class method defined in a category
1599 // implementation requires an odd treatment.
1600 // If we are in a class method, we must retrieve the
1601 // _metaclass_ for the current class, pointed at by
1602 // the class's "isa" pointer. The following assumes that
1603 // isa" is the first ivar in a class (which it must be).
1604 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1605 Target = CGF.Builder.CreateStructGEP(Target, 0);
1606 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001607 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001608 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1609 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1610 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1611 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001612 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001613 }
1614 else if (isCategoryImpl)
1615 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1616 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001617 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1618 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1619 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001620 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001621 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1622 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001623 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001624 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001625 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001626 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001627 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001628 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001629 EmitSelector(CGF.Builder, Sel),
1630 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001631 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001632}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001633
1634/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001635CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001636 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001637 QualType ResultType,
1638 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001639 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001640 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001641 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001642 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001643 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001644 EmitSelector(CGF.Builder, Sel),
1645 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001646 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001647}
1648
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001649CodeGen::RValue
1650CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001651 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001652 QualType ResultType,
1653 llvm::Value *Sel,
1654 llvm::Value *Arg0,
1655 QualType Arg0Ty,
1656 bool IsSuper,
1657 const CallArgList &CallArgs,
1658 const ObjCMethodDecl *Method,
1659 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001660 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001661 if (!IsSuper)
1662 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001663 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001664 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001665 CGF.getContext().getObjCSelType()));
1666 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001667
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001668 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001669 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001670 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001671 const llvm::FunctionType *FTy =
1672 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001673
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001674 if (Method)
1675 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1676 CGM.getContext().getCanonicalType(ResultType) &&
1677 "Result type mismatch!");
1678
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001679 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001680 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001681 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001682 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001683 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1684 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1685 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001686 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001687 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001688 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001689 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001690 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001691 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001692}
1693
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001694static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1695 if (FQT.isObjCGCStrong())
1696 return Qualifiers::Strong;
1697
1698 if (FQT.isObjCGCWeak())
1699 return Qualifiers::Weak;
1700
1701 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1702 return Qualifiers::Strong;
1703
1704 if (const PointerType *PT = FQT->getAs<PointerType>())
1705 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1706
1707 return Qualifiers::GCNone;
1708}
1709
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001710llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001711 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1712 llvm::Constant *NullPtr =
1713 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian5af12352010-08-05 15:52:12 +00001714 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
1715 DeclRefs.empty())
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001716 return NullPtr;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001717 SkipIvars.clear();
1718 IvarsInfo.clear();
1719 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1720 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1721
1722 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1723 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1724 const ValueDecl *VD = BDRE->getDecl();
1725 CharUnits Offset = CGF.BlockDecls[VD];
1726 uint64_t FieldOffset = Offset.getQuantity();
1727 QualType Ty = VD->getType();
1728 assert(!Ty->isArrayType() &&
1729 "Array block variable should have been caught");
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001730 if (Ty->isRecordType() && !BDRE->isByRef()) {
1731 bool HasUnion = false;
1732 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1733 FieldOffset,
1734 true,
1735 HasUnion);
1736 continue;
1737 }
Fariborz Jahanian8ada8eb2010-08-05 16:13:18 +00001738 // FIXME. Handle none __block Aggregate variables
1739#if 0
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001740 if (Ty->isUnionType() && !BDRE->isByRef())
1741 assert(false && "union block variable layout NYI");
Fariborz Jahanian8ada8eb2010-08-05 16:13:18 +00001742#endif
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001743 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1744 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1745 // __block variables are passed by their descriptior address. So, size
1746 // must reflect this.
1747 if (BDRE->isByRef())
1748 FieldSize = WordSizeInBits;
1749 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1750 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1751 FieldSize / WordSizeInBits));
1752 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1753 SkipIvars.push_back(GC_IVAR(FieldOffset,
1754 FieldSize / ByteSizeInBits));
1755 }
1756
1757 if (IvarsInfo.empty())
1758 return NullPtr;
1759
1760 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001761 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001762 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1763 printf("\n block variable layout for block: ");
1764 const unsigned char *s = (unsigned char*)BitMap.c_str();
1765 for (unsigned i = 0; i < BitMap.size(); i++)
1766 if (!(s[i] & 0xf0))
1767 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1768 else
1769 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1770 printf("\n");
1771 }
1772
1773 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001774}
1775
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001776llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001777 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001778 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001779 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001780 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1781
Owen Anderson3c4972d2009-07-29 18:54:39 +00001782 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001783 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001784}
1785
Fariborz Jahanianda320092009-01-29 19:24:30 +00001786void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001787 // FIXME: We shouldn't need this, the protocol decl should contain enough
1788 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001789 DefinedProtocols.insert(PD->getIdentifier());
1790
1791 // If we have generated a forward reference to this protocol, emit
1792 // it now. Otherwise do nothing, the protocol objects are lazily
1793 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001794 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001795 GetOrEmitProtocol(PD);
1796}
1797
Fariborz Jahanianda320092009-01-29 19:24:30 +00001798llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001799 if (DefinedProtocols.count(PD->getIdentifier()))
1800 return GetOrEmitProtocol(PD);
1801 return GetOrEmitProtocolRef(PD);
1802}
1803
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001804/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001805// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1806struct _objc_protocol {
1807struct _objc_protocol_extension *isa;
1808char *protocol_name;
1809struct _objc_protocol_list *protocol_list;
1810struct _objc__method_prototype_list *instance_methods;
1811struct _objc__method_prototype_list *class_methods
1812};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001813
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001815*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001816llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1817 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1818
1819 // Early exit if a defining object has already been generated.
1820 if (Entry && Entry->hasInitializer())
1821 return Entry;
1822
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001823 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001824 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001825 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1826
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001827 // Construct method lists.
1828 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1829 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001830 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001831 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001832 ObjCMethodDecl *MD = *i;
1833 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1834 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1835 OptInstanceMethods.push_back(C);
1836 } else {
1837 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001839 }
1840
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001841 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001842 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 ObjCMethodDecl *MD = *i;
1844 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1845 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1846 OptClassMethods.push_back(C);
1847 } else {
1848 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001849 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001850 }
1851
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001852 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001853 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001854 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001855 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001856 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001857 PD->protocol_begin(),
1858 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001860 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001861 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1862 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001864 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001865 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1866 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001867 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001868 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001869
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001871 // Already created, fix the linkage and update the initializer.
1872 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 Entry->setInitializer(Init);
1874 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001875 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001876 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001877 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001878 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001879 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001880 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001881 // FIXME: Is this necessary? Why only for protocol?
1882 Entry->setAlignment(4);
1883 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001884 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001885
1886 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001887}
1888
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001889llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001890 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1891
1892 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001893 // We use the initializer as a marker of whether this is a forward
1894 // reference or not. At module finalization we add the empty
1895 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001896 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001897 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001898 llvm::GlobalValue::ExternalLinkage,
1899 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001900 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001901 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001902 // FIXME: Is this necessary? Why only for protocol?
1903 Entry->setAlignment(4);
1904 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001905
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906 return Entry;
1907}
1908
1909/*
1910 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001911 uint32_t size;
1912 struct objc_method_description_list *optional_instance_methods;
1913 struct objc_method_description_list *optional_class_methods;
1914 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001915 };
1916*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001917llvm::Constant *
1918CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1919 const ConstantVector &OptInstanceMethods,
1920 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001921 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001922 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001924 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 Values[1] =
1926 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001927 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001928 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1929 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001931 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001932 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1933 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001934 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001935 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001936
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001937 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001938 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001939 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001940 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001941
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001942 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001943 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001944
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001945 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001946 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001947 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001948 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001949}
1950
1951/*
1952 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001953 struct objc_protocol_list *next;
1954 long count;
1955 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956 };
1957*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001958llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001959CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001960 ObjCProtocolDecl::protocol_iterator begin,
1961 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001962 std::vector<llvm::Constant*> ProtocolRefs;
1963
Daniel Dunbardbc933702008-08-21 21:57:41 +00001964 for (; begin != end; ++begin)
1965 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001966
1967 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001969 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001970
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001971 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001972 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001973
1974 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001975 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001976 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001977 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001978 ProtocolRefs.size() - 1);
1979 Values[2] =
1980 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1981 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001982 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001983
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001984 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001985 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001986 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001987 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001988 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001989}
1990
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001991void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1992 std::vector<llvm::Constant*> &Properties,
1993 const Decl *Container,
1994 const ObjCProtocolDecl *PROTO,
1995 const ObjCCommonTypesHelper &ObjCTypes) {
1996 std::vector<llvm::Constant*> Prop(2);
1997 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1998 E = PROTO->protocol_end(); P != E; ++P)
1999 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2000 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2001 E = PROTO->prop_end(); I != E; ++I) {
2002 const ObjCPropertyDecl *PD = *I;
2003 if (!PropertySet.insert(PD->getIdentifier()))
2004 continue;
2005 Prop[0] = GetPropertyName(PD->getIdentifier());
2006 Prop[1] = GetPropertyTypeString(PD, Container);
2007 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2008 }
2009}
2010
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002011/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002012 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002013 const char * const name;
2014 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002015 };
2016
2017 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002018 uint32_t entsize; // sizeof (struct _objc_property)
2019 uint32_t prop_count;
2020 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002021 };
2022*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002023llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002024 const Decl *Container,
2025 const ObjCContainerDecl *OCD,
2026 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002027 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002028 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002029 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2030 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002031 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002032 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002033 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002034 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002035 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002036 Prop));
2037 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002038 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002039 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
2040 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002041 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2042 ObjCTypes);
2043 }
2044 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2045 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2046 E = CD->protocol_end(); P != E; ++P)
2047 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2048 ObjCTypes);
2049 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002050
2051 // Return null for empty list.
2052 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002053 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002054
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002055 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002056 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002057 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2059 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002060 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002061 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002062 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002063 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002064
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002065 llvm::GlobalVariable *GV =
2066 CreateMetadataVar(Name, Init,
2067 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002068 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002069 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002070 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002071 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002072}
2073
2074/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002075 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002076 int count;
2077 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002078 };
2079*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002080llvm::Constant *
2081CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2082 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002083 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2085 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002086 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002087 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002088 Desc);
2089}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002090
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002091llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002092 const char *Section,
2093 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002094 // Return null for empty list.
2095 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002096 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002097
2098 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002099 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002100 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002101 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002102 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002103 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002104
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002105 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002107 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002108}
2109
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002110/*
2111 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002112 char *category_name;
2113 char *class_name;
2114 struct _objc_method_list *instance_methods;
2115 struct _objc_method_list *class_methods;
2116 struct _objc_protocol_list *protocols;
2117 uint32_t size; // <rdar://4585769>
2118 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002119 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002121void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002122 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002123
Mike Stumpf5408fe2009-05-16 07:57:57 +00002124 // FIXME: This is poor design, the OCD should have a pointer to the category
2125 // decl. Additionally, note that Category can be null for the @implementation
2126 // w/o an @interface case. Sema should just create one for us as it does for
2127 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002128 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002129 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002130 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002131
2132 llvm::SmallString<256> ExtName;
2133 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2134 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002135
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002136 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002137 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002138 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002139 // Instance methods should always be defined.
2140 InstanceMethods.push_back(GetMethodConstant(*i));
2141 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002142 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002143 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002144 // Class methods should always be defined.
2145 ClassMethods.push_back(GetMethodConstant(*i));
2146 }
2147
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002148 std::vector<llvm::Constant*> Values(7);
2149 Values[0] = GetClassName(OCD->getIdentifier());
2150 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002151 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002152 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002153 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002154 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002155 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002156 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002157 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002158 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002159 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002160 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002162 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002163 Category->protocol_begin(),
2164 Category->protocol_end());
2165 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002166 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002167 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002168 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002169
2170 // If there is no category @interface then there can be no properties.
2171 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002172 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002173 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002174 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002175 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002176 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002177
Owen Anderson08e25242009-07-27 22:29:56 +00002178 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002179 Values);
2180
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002181 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002182 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002183 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002184 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002185 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002186 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002187}
2188
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002189// FIXME: Get from somewhere?
2190enum ClassFlags {
2191 eClassFlags_Factory = 0x00001,
2192 eClassFlags_Meta = 0x00002,
2193 // <rdr://5142207>
2194 eClassFlags_HasCXXStructors = 0x02000,
2195 eClassFlags_Hidden = 0x20000,
2196 eClassFlags_ABI2_Hidden = 0x00010,
2197 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2198};
2199
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002200/*
2201 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002202 Class isa;
2203 Class super_class;
2204 const char *name;
2205 long version;
2206 long info;
2207 long instance_size;
2208 struct _objc_ivar_list *ivars;
2209 struct _objc_method_list *methods;
2210 struct _objc_cache *cache;
2211 struct _objc_protocol_list *protocols;
2212 // Objective-C 1.0 extensions (<rdr://4585769>)
2213 const char *ivar_layout;
2214 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002215 };
2216
2217 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002218*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002220 DefinedSymbols.insert(ID->getIdentifier());
2221
Chris Lattner8ec03f52008-11-24 03:54:41 +00002222 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002224 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002226 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002227 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00002228 Interface->protocol_begin(),
2229 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002231 if (ID->getNumIvarInitializers())
2232 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002233 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002234 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235
2236 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002237 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238 Flags |= eClassFlags_Hidden;
2239
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002240 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002241 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002242 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002243 // Instance methods should always be defined.
2244 InstanceMethods.push_back(GetMethodConstant(*i));
2245 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002246 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002247 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002248 // Class methods should always be defined.
2249 ClassMethods.push_back(GetMethodConstant(*i));
2250 }
2251
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002252 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002253 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002254 ObjCPropertyImplDecl *PID = *i;
2255
2256 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2257 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2258
2259 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2260 if (llvm::Constant *C = GetMethodConstant(MD))
2261 InstanceMethods.push_back(C);
2262 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2263 if (llvm::Constant *C = GetMethodConstant(MD))
2264 InstanceMethods.push_back(C);
2265 }
2266 }
2267
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002269 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002271 // Record a reference to the super class.
2272 LazySymbols.insert(Super->getIdentifier());
2273
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002274 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002275 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 ObjCTypes.ClassPtrTy);
2277 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002278 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002279 }
2280 Values[ 2] = GetClassName(ID->getIdentifier());
2281 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002282 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2283 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2284 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002285 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002286 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002287 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002288 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002289 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002291 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002292 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002293 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002295 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002297 std::string Name("\01L_OBJC_CLASS_");
2298 Name += ClassName;
2299 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2300 // Check for a forward reference.
2301 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2302 if (GV) {
2303 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2304 "Forward metaclass reference has incorrect type.");
2305 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2306 GV->setInitializer(Init);
2307 GV->setSection(Section);
2308 GV->setAlignment(4);
2309 CGM.AddUsedGlobal(GV);
2310 }
2311 else
2312 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002313 DefinedClasses.push_back(GV);
2314}
2315
2316llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2317 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002318 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002319 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002320 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002321
Daniel Dunbar04d40782009-04-14 06:00:08 +00002322 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002324
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325 std::vector<llvm::Constant*> Values(12);
2326 // The isa for the metaclass is the root of the hierarchy.
2327 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2328 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2329 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002330 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002331 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002333 // The super class for the metaclass is emitted as the name of the
2334 // super class. The runtime fixes this up to point to the
2335 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002338 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 ObjCTypes.ClassPtrTy);
2340 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002341 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342 }
2343 Values[ 2] = GetClassName(ID->getIdentifier());
2344 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002345 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2346 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2347 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002348 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002349 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002350 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002351 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002352 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002353 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002354 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 Values[ 9] = Protocols;
2356 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002357 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002359 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002360 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002361 Values);
2362
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002363 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002364 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002365
2366 // Check for a forward reference.
2367 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2368 if (GV) {
2369 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2370 "Forward metaclass reference has incorrect type.");
2371 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2372 GV->setInitializer(Init);
2373 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002374 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002375 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002376 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002377 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002378 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002379 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002380 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381
2382 return GV;
2383}
2384
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002385llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002386 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002387
Mike Stumpf5408fe2009-05-16 07:57:57 +00002388 // FIXME: Should we look these up somewhere other than the module. Its a bit
2389 // silly since we only generate these while processing an implementation, so
2390 // exactly one pointer would work if know when we entered/exitted an
2391 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002392
2393 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002394 // Previously, metaclass with internal linkage may have been defined.
2395 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002396 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2397 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002398 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2399 "Forward metaclass reference has incorrect type.");
2400 return GV;
2401 } else {
2402 // Generate as an external reference to keep a consistent
2403 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002404 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002405 llvm::GlobalValue::ExternalLinkage,
2406 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002407 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002408 }
2409}
2410
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002411llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2412 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2413
2414 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2415 true)) {
2416 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2417 "Forward class metadata reference has incorrect type.");
2418 return GV;
2419 } else {
2420 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2421 llvm::GlobalValue::ExternalLinkage,
2422 0,
2423 Name);
2424 }
2425}
2426
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427/*
2428 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002429 uint32_t size;
2430 const char *weak_ivar_layout;
2431 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432 };
2433*/
2434llvm::Constant *
2435CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002437 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438
2439 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002440 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002441 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002442 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002443 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444
2445 // Return null if no extension bits are used.
2446 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002447 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002449 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002450 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002451 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002452 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002453 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454}
2455
2456/*
2457 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002458 char *ivar_name;
2459 char *ivar_type;
2460 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002461 };
2462
2463 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002464 int ivar_count;
2465 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002466 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002467*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002468llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002469 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002470 std::vector<llvm::Constant*> Ivars, Ivar(3);
2471
2472 // When emitting the root class GCC emits ivar entries for the
2473 // actual class structure. It is not clear if we need to follow this
2474 // behavior; for now lets try and get away with not doing it. If so,
2475 // the cleanest solution would be to make up an ObjCInterfaceDecl
2476 // for the class.
2477 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002478 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002479
2480 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002481 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002483 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002484 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002485
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002486 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2487 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002488 // Ignore unnamed bit-fields.
2489 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002490 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002491 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2492 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002493 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002494 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002495 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002496 }
2497
2498 // Return null for empty list.
2499 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002500 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002501
2502 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002503 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002504 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002505 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002506 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002507 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002508
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002509 llvm::GlobalVariable *GV;
2510 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002511 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002512 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002513 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002514 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002515 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002516 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002517 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002518 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002519}
2520
2521/*
2522 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002523 SEL method_name;
2524 char *method_types;
2525 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002526 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002527
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002528 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002529 struct objc_method_list *obsolete;
2530 int count;
2531 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002532 };
2533*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002534
2535/// GetMethodConstant - Return a struct objc_method constant for the
2536/// given method if it has been defined. The result is null if the
2537/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002538llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002539 // FIXME: Use DenseMap::lookup
2540 llvm::Function *Fn = MethodDefinitions[MD];
2541 if (!Fn)
2542 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002543
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002544 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002545 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002546 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002547 ObjCTypes.SelectorPtrTy);
2548 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002549 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002550 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002551}
2552
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002553llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002554 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002555 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002556 // Return null for empty list.
2557 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002558 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002559
2560 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002561 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002562 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002563 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002564 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002565 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002566 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002567
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002568 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002569 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002570 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002571}
2572
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002573llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002574 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002575 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002576 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002577
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002578 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002579 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002580 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002581 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002582 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002583 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002584 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002585 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002586 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002587
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002588 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002589}
2590
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002591llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002592CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002593 llvm::Constant *Init,
2594 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002595 unsigned Align,
2596 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002597 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002598 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002599 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002600 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002601 if (Section)
2602 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002603 if (Align)
2604 GV->setAlignment(Align);
2605 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002606 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002607 return GV;
2608}
2609
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002610llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002611 // Abuse this interface function as a place to finalize.
2612 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002613 return NULL;
2614}
2615
Chris Lattner74391b42009-03-22 21:03:39 +00002616llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002617 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002618}
2619
Chris Lattner74391b42009-03-22 21:03:39 +00002620llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002621 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002622}
2623
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002624llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2625 return ObjCTypes.getCopyStructFn();
2626}
2627
Chris Lattner74391b42009-03-22 21:03:39 +00002628llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002629 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002630}
2631
John McCallf1549f62010-07-06 01:34:17 +00002632void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2633 return EmitTryOrSynchronizedStmt(CGF, S);
2634}
2635
2636void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2637 const ObjCAtSynchronizedStmt &S) {
2638 return EmitTryOrSynchronizedStmt(CGF, S);
2639}
2640
John McCallcc505292010-07-21 06:59:36 +00002641namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002642 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002643 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002644 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002645 llvm::Value *CallTryExitVar;
2646 llvm::Value *ExceptionData;
2647 ObjCTypesHelper &ObjCTypes;
2648 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002649 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002650 llvm::Value *CallTryExitVar,
2651 llvm::Value *ExceptionData,
2652 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002653 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002654 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2655
2656 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2657 // Check whether we need to call objc_exception_try_exit.
2658 // In optimized code, this branch will always be folded.
2659 llvm::BasicBlock *FinallyCallExit =
2660 CGF.createBasicBlock("finally.call_exit");
2661 llvm::BasicBlock *FinallyNoCallExit =
2662 CGF.createBasicBlock("finally.no_call_exit");
2663 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2664 FinallyCallExit, FinallyNoCallExit);
2665
2666 CGF.EmitBlock(FinallyCallExit);
2667 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2668 ->setDoesNotThrow();
2669
2670 CGF.EmitBlock(FinallyNoCallExit);
2671
2672 if (isa<ObjCAtTryStmt>(S)) {
2673 if (const ObjCAtFinallyStmt* FinallyStmt =
2674 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2675 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2676
2677 // Currently, the end of the cleanup must always exist.
2678 CGF.EnsureInsertPoint();
2679 } else {
2680 // Emit objc_sync_exit(expr); as finally's sole statement for
2681 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002682 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002683 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2684 ->setDoesNotThrow();
2685 }
2686 }
2687 };
John McCall87bb5822010-07-31 23:20:56 +00002688
2689 class FragileHazards {
2690 CodeGenFunction &CGF;
2691 llvm::SmallVector<llvm::Value*, 20> Locals;
2692 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2693
2694 llvm::InlineAsm *ReadHazard;
2695 llvm::InlineAsm *WriteHazard;
2696
2697 llvm::FunctionType *GetAsmFnType();
2698
2699 void collectLocals();
2700 void emitReadHazard(CGBuilderTy &Builder);
2701
2702 public:
2703 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002704
John McCall87bb5822010-07-31 23:20:56 +00002705 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002706 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002707 };
2708}
2709
2710/// Create the fragile-ABI read and write hazards based on the current
2711/// state of the function, which is presumed to be immediately prior
2712/// to a @try block. These hazards are used to maintain correct
2713/// semantics in the face of optimization and the fragile ABI's
2714/// cavalier use of setjmp/longjmp.
2715FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2716 collectLocals();
2717
2718 if (Locals.empty()) return;
2719
2720 // Collect all the blocks in the function.
2721 for (llvm::Function::iterator
2722 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2723 BlocksBeforeTry.insert(&*I);
2724
2725 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2726
2727 // Create a read hazard for the allocas. This inhibits dead-store
2728 // optimizations and forces the values to memory. This hazard is
2729 // inserted before any 'throwing' calls in the protected scope to
2730 // reflect the possibility that the variables might be read from the
2731 // catch block if the call throws.
2732 {
2733 std::string Constraint;
2734 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2735 if (I) Constraint += ',';
2736 Constraint += "*m";
2737 }
2738
2739 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2740 }
2741
2742 // Create a write hazard for the allocas. This inhibits folding
2743 // loads across the hazard. This hazard is inserted at the
2744 // beginning of the catch path to reflect the possibility that the
2745 // variables might have been written within the protected scope.
2746 {
2747 std::string Constraint;
2748 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2749 if (I) Constraint += ',';
2750 Constraint += "=*m";
2751 }
2752
2753 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2754 }
2755}
2756
2757/// Emit a write hazard at the current location.
2758void FragileHazards::emitWriteHazard() {
2759 if (Locals.empty()) return;
2760
2761 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2762 ->setDoesNotThrow();
2763}
2764
John McCall87bb5822010-07-31 23:20:56 +00002765void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2766 assert(!Locals.empty());
2767 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2768 ->setDoesNotThrow();
2769}
2770
2771/// Emit read hazards in all the protected blocks, i.e. all the blocks
2772/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002773void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002774 if (Locals.empty()) return;
2775
2776 CGBuilderTy Builder(CGF.getLLVMContext());
2777
2778 // Iterate through all blocks, skipping those prior to the try.
2779 for (llvm::Function::iterator
2780 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2781 llvm::BasicBlock &BB = *FI;
2782 if (BlocksBeforeTry.count(&BB)) continue;
2783
2784 // Walk through all the calls in the block.
2785 for (llvm::BasicBlock::iterator
2786 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2787 llvm::Instruction &I = *BI;
2788
2789 // Ignore instructions that aren't non-intrinsic calls.
2790 // These are the only calls that can possibly call longjmp.
2791 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2792 if (isa<llvm::IntrinsicInst>(I))
2793 continue;
2794
2795 // Ignore call sites marked nounwind. This may be questionable,
2796 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2797 llvm::CallSite CS(&I);
2798 if (CS.doesNotThrow()) continue;
2799
John McCall0b251722010-08-04 05:59:32 +00002800 // Insert a read hazard before the call. This will ensure that
2801 // any writes to the locals are performed before making the
2802 // call. If the call throws, then this is sufficient to
2803 // guarantee correctness as long as it doesn't also write to any
2804 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002805 Builder.SetInsertPoint(&BB, BI);
2806 emitReadHazard(Builder);
2807 }
2808 }
2809}
2810
2811static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2812 if (V) S.insert(V);
2813}
2814
2815void FragileHazards::collectLocals() {
2816 // Compute a set of allocas to ignore.
2817 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2818 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2819 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2820 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2821
2822 // Collect all the allocas currently in the function. This is
2823 // probably way too aggressive.
2824 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2825 for (llvm::BasicBlock::iterator
2826 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2827 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2828 Locals.push_back(&*I);
2829}
2830
2831llvm::FunctionType *FragileHazards::GetAsmFnType() {
2832 std::vector<const llvm::Type *> Tys(Locals.size());
2833 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2834 Tys[I] = Locals[I]->getType();
2835 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002836}
2837
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002838/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002839
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002840 Objective-C setjmp-longjmp (sjlj) Exception Handling
2841 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002842
John McCallf1549f62010-07-06 01:34:17 +00002843 A catch buffer is a setjmp buffer plus:
2844 - a pointer to the exception that was caught
2845 - a pointer to the previous exception data buffer
2846 - two pointers of reserved storage
2847 Therefore catch buffers form a stack, with a pointer to the top
2848 of the stack kept in thread-local storage.
2849
2850 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2851 objc_exception_try_exit pops the given catch buffer, which is
2852 required to be the top of the EH stack.
2853 objc_exception_throw pops the top of the EH stack, writes the
2854 thrown exception into the appropriate field, and longjmps
2855 to the setjmp buffer. It crashes the process (with a printf
2856 and an abort()) if there are no catch buffers on the stack.
2857 objc_exception_extract just reads the exception pointer out of the
2858 catch buffer.
2859
2860 There's no reason an implementation couldn't use a light-weight
2861 setjmp here --- something like __builtin_setjmp, but API-compatible
2862 with the heavyweight setjmp. This will be more important if we ever
2863 want to implement correct ObjC/C++ exception interactions for the
2864 fragile ABI.
2865
2866 Note that for this use of setjmp/longjmp to be correct, we may need
2867 to mark some local variables volatile: if a non-volatile local
2868 variable is modified between the setjmp and the longjmp, it has
2869 indeterminate value. For the purposes of LLVM IR, it may be
2870 sufficient to make loads and stores within the @try (to variables
2871 declared outside the @try) volatile. This is necessary for
2872 optimized correctness, but is not currently being done; this is
2873 being tracked as rdar://problem/8160285
2874
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002875 The basic framework for a @try-catch-finally is as follows:
2876 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002877 objc_exception_data d;
2878 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002879 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002880
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002881 objc_exception_try_enter(&d);
2882 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002883 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002884 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002885 // exception path
2886 id _caught = objc_exception_extract(&d);
2887
2888 // enter new try scope for handlers
2889 if (!setjmp(d.jmp_buf)) {
2890 ... match exception and execute catch blocks ...
2891
2892 // fell off end, rethrow.
2893 _rethrow = _caught;
2894 ... jump-through-finally to finally_rethrow ...
2895 } else {
2896 // exception in catch block
2897 _rethrow = objc_exception_extract(&d);
2898 _call_try_exit = false;
2899 ... jump-through-finally to finally_rethrow ...
2900 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002901 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002902 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002903
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002904 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002905 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002906 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002907
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002908 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002909 ... dispatch to finally destination ...
2910
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002912 objc_exception_throw(_rethrow);
2913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 finally_end:
2915 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002916
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002917 This framework differs slightly from the one gcc uses, in that gcc
2918 uses _rethrow to determine if objc_exception_try_exit should be called
2919 and if the object should be rethrown. This breaks in the face of
2920 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002921
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002922 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002923
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002924 - If there are no catch blocks, then we avoid emitting the second
2925 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002926
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002927 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2928 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002930 - FIXME: If there is no @finally block we can do a few more
2931 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002932
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002933 Rethrows and Jumps-Through-Finally
2934 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002935
John McCallf1549f62010-07-06 01:34:17 +00002936 '@throw;' is supported by pushing the currently-caught exception
2937 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002938
John McCallf1549f62010-07-06 01:34:17 +00002939 Branches through the @finally block are handled with an ordinary
2940 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2941 exceptions are not compatible with C++ exceptions, and this is
2942 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002943
John McCallf1549f62010-07-06 01:34:17 +00002944 @synchronized(expr) { stmt; } is emitted as if it were:
2945 id synch_value = expr;
2946 objc_sync_enter(synch_value);
2947 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002948*/
2949
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002950void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2951 const Stmt &S) {
2952 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002953
2954 // A destination for the fall-through edges of the catch handlers to
2955 // jump to.
2956 CodeGenFunction::JumpDest FinallyEnd =
2957 CGF.getJumpDestInCurrentScope("finally.end");
2958
2959 // A destination for the rethrow edge of the catch handlers to jump
2960 // to.
2961 CodeGenFunction::JumpDest FinallyRethrow =
2962 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002963
Daniel Dunbar1c566672009-02-24 01:43:46 +00002964 // For @synchronized, call objc_sync_enter(sync.expr). The
2965 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002966 // @synchronized. We can't avoid a temp here because we need the
2967 // value to be preserved. If the backend ever does liveness
2968 // correctly after setjmp, this will be unnecessary.
2969 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002970 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002971 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002972 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2973 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002974 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2975 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002976
2977 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2978 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002979 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002980
John McCall0b251722010-08-04 05:59:32 +00002981 // Allocate memory for the setjmp buffer. This needs to be kept
2982 // live throughout the try and catch blocks.
2983 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2984 "exceptiondata.ptr");
2985
John McCall87bb5822010-07-31 23:20:56 +00002986 // Create the fragile hazards. Note that this will not capture any
2987 // of the allocas required for exception processing, but will
2988 // capture the current basic block (which extends all the way to the
2989 // setjmp call) as "before the @try".
2990 FragileHazards Hazards(CGF);
2991
John McCallf1549f62010-07-06 01:34:17 +00002992 // Create a flag indicating whether the cleanup needs to call
2993 // objc_exception_try_exit. This is true except when
2994 // - no catches match and we're branching through the cleanup
2995 // just to rethrow the exception, or
2996 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002997 // The setjmp-safety rule here is that we should always store to this
2998 // variable in a place that dominates the branch through the cleanup
2999 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003000 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003001 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003002
John McCallf1549f62010-07-06 01:34:17 +00003003 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003004 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003005 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003006 CallTryExitVar,
3007 ExceptionData,
3008 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003009
3010 // Enter a try block:
3011 // - Call objc_exception_try_enter to push ExceptionData on top of
3012 // the EH stack.
3013 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3014 ->setDoesNotThrow();
3015
3016 // - Call setjmp on the exception data buffer.
3017 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3018 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3019 llvm::Value *SetJmpBuffer =
3020 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3021 llvm::CallInst *SetJmpResult =
3022 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3023 SetJmpResult->setDoesNotThrow();
3024
3025 // If setjmp returned 0, enter the protected block; otherwise,
3026 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003027 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3028 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003029 llvm::Value *DidCatch =
3030 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
3031 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson80f25672008-09-09 17:59:25 +00003032
John McCallf1549f62010-07-06 01:34:17 +00003033 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003034 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003035 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003036 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003037 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003038
3039 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003040
John McCallf1549f62010-07-06 01:34:17 +00003041 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003042 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003043
John McCall87bb5822010-07-31 23:20:56 +00003044 // Don't optimize loads of the in-scope locals across this point.
3045 Hazards.emitWriteHazard();
3046
John McCallf1549f62010-07-06 01:34:17 +00003047 // For a @synchronized (or a @try with no catches), just branch
3048 // through the cleanup to the rethrow block.
3049 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3050 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003051 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003052 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003053
3054 // Otherwise, we have to match against the caught exceptions.
3055 } else {
John McCall0b251722010-08-04 05:59:32 +00003056 // Retrieve the exception object. We may emit multiple blocks but
3057 // nothing can cross this so the value is already in SSA form.
3058 llvm::CallInst *Caught =
3059 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3060 ExceptionData, "caught");
3061 Caught->setDoesNotThrow();
3062
John McCallf1549f62010-07-06 01:34:17 +00003063 // Push the exception to rethrow onto the EH value stack for the
3064 // benefit of any @throws in the handlers.
3065 CGF.ObjCEHValueStack.push_back(Caught);
3066
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003067 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003068
John McCall0b251722010-08-04 05:59:32 +00003069 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003070
John McCall0b251722010-08-04 05:59:32 +00003071 llvm::BasicBlock *CatchBlock = 0;
3072 llvm::BasicBlock *CatchHandler = 0;
3073 if (HasFinally) {
3074 // Enter a new exception try block (in case a @catch block
3075 // throws an exception).
3076 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3077 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003078
John McCall0b251722010-08-04 05:59:32 +00003079 llvm::CallInst *SetJmpResult =
3080 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3081 "setjmp.result");
3082 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003083
John McCall0b251722010-08-04 05:59:32 +00003084 llvm::Value *Threw =
3085 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3086
3087 CatchBlock = CGF.createBasicBlock("catch");
3088 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3089 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3090
3091 CGF.EmitBlock(CatchBlock);
3092 }
3093
3094 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095
Daniel Dunbar55e40722008-09-27 07:03:52 +00003096 // Handle catch list. As a special case we check if everything is
3097 // matched and avoid generating code for falling off the end if
3098 // so.
3099 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003100 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3101 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003102
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003103 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003104 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003105
Anders Carlsson80f25672008-09-09 17:59:25 +00003106 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003107 if (!CatchParam) {
3108 AllMatched = true;
3109 } else {
John McCall183700f2009-09-21 23:43:11 +00003110 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003111
John McCallf1549f62010-07-06 01:34:17 +00003112 // catch(id e) always matches under this ABI, since only
3113 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003114 // FIXME: For the time being we also match id<X>; this should
3115 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003116 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003117 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003118 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003119
John McCallf1549f62010-07-06 01:34:17 +00003120 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003121 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003122 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3123
Anders Carlssondde0a942008-09-11 09:15:33 +00003124 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003125 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003126 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003127
3128 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003129 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003131
Anders Carlssondde0a942008-09-11 09:15:33 +00003132 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003133
3134 // The scope of the catch variable ends right here.
3135 CatchVarCleanups.ForceCleanup();
3136
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003137 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003138 break;
3139 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003140
Steve Naroff14108da2009-07-10 23:34:53 +00003141 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003142 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003143
3144 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003145 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3146 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003147
3148 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003149 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003150
John McCallf1549f62010-07-06 01:34:17 +00003151 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003152 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3153 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003154 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003155
John McCallf1549f62010-07-06 01:34:17 +00003156 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3157 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003158
3159 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003160 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003161
Anders Carlsson80f25672008-09-09 17:59:25 +00003162 // Emit the @catch block.
3163 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003164
3165 // Collect any cleanups for the catch variable. The scope lasts until
3166 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003167 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003168
Steve Naroff7ba138a2009-03-03 19:52:17 +00003169 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003170 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003171
John McCallf1549f62010-07-06 01:34:17 +00003172 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003173 llvm::Value *Tmp =
3174 CGF.Builder.CreateBitCast(Caught,
3175 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003176 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003177 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003178
Anders Carlssondde0a942008-09-11 09:15:33 +00003179 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003180
3181 // We're done with the catch variable.
3182 CatchVarCleanups.ForceCleanup();
3183
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003184 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003185
Anders Carlsson80f25672008-09-09 17:59:25 +00003186 CGF.EmitBlock(NextCatchBlock);
3187 }
3188
John McCallf1549f62010-07-06 01:34:17 +00003189 CGF.ObjCEHValueStack.pop_back();
3190
John McCall0b251722010-08-04 05:59:32 +00003191 // If nothing wanted anything to do with the caught exception,
3192 // kill the extract call.
3193 if (Caught->use_empty())
3194 Caught->eraseFromParent();
3195
3196 if (!AllMatched)
3197 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3198
3199 if (HasFinally) {
3200 // Emit the exception handler for the @catch blocks.
3201 CGF.EmitBlock(CatchHandler);
3202
3203 // In theory we might now need a write hazard, but actually it's
3204 // unnecessary because there's no local-accessing code between
3205 // the try's write hazard and here.
3206 //Hazards.emitWriteHazard();
3207
3208 // Don't pop the catch handler; the throw already did.
3209 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003210 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003211 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003212 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213
John McCall87bb5822010-07-31 23:20:56 +00003214 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003215 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003216
John McCallf1549f62010-07-06 01:34:17 +00003217 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003218 CGF.Builder.restoreIP(TryFallthroughIP);
3219 if (CGF.HaveInsertPoint())
3220 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003221 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003222 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003223
John McCallf1549f62010-07-06 01:34:17 +00003224 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003225 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003226 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003227 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003228 // Just look in the buffer for the exception to throw.
3229 llvm::CallInst *Caught =
3230 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3231 ExceptionData);
3232 Caught->setDoesNotThrow();
3233
3234 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003235 ->setDoesNotThrow();
3236 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003237 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003238
John McCall87bb5822010-07-31 23:20:56 +00003239 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003240}
3241
3242void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003243 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003244 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003245
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003246 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3247 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003248 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003249 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3250 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003251 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003252 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003253 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003254 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003255
John McCallf1549f62010-07-06 01:34:17 +00003256 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3257 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003258 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003259
3260 // Clear the insertion point to indicate we are in unreachable code.
3261 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003262}
3263
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003264/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003265/// object: objc_read_weak (id *src)
3266///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003267llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003268 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003269 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003270 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3271 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3272 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003273 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003274 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003275 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003276 return read_weak;
3277}
3278
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003279/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3280/// objc_assign_weak (id src, id *dst)
3281///
3282void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003283 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003284 const llvm::Type * SrcTy = src->getType();
3285 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003286 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003287 assert(Size <= 8 && "does not support size > 8");
3288 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003289 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003290 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3291 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003292 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3293 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003294 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003295 src, dst, "weakassign");
3296 return;
3297}
3298
Fariborz Jahanian58626502008-11-19 00:59:10 +00003299/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3300/// objc_assign_global (id src, id *dst)
3301///
3302void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003303 llvm::Value *src, llvm::Value *dst,
3304 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003305 const llvm::Type * SrcTy = src->getType();
3306 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003307 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003308 assert(Size <= 8 && "does not support size > 8");
3309 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003310 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003311 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3312 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003313 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3314 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003315 if (!threadlocal)
3316 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3317 src, dst, "globalassign");
3318 else
3319 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3320 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003321 return;
3322}
3323
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003324/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003325/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003326///
3327void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003328 llvm::Value *src, llvm::Value *dst,
3329 llvm::Value *ivarOffset) {
3330 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003331 const llvm::Type * SrcTy = src->getType();
3332 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003333 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003334 assert(Size <= 8 && "does not support size > 8");
3335 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003337 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3338 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003339 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3340 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003341 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3342 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003343 return;
3344}
3345
Fariborz Jahanian58626502008-11-19 00:59:10 +00003346/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3347/// objc_assign_strongCast (id src, id *dst)
3348///
3349void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003350 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003351 const llvm::Type * SrcTy = src->getType();
3352 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003353 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003354 assert(Size <= 8 && "does not support size > 8");
3355 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003356 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003357 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3358 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003359 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3360 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003361 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003362 src, dst, "weakassign");
3363 return;
3364}
3365
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003366void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003367 llvm::Value *DestPtr,
3368 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003369 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003370 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3371 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003372 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003373 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003374 return;
3375}
3376
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003377/// EmitObjCValueForIvar - Code Gen for ivar reference.
3378///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003379LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3380 QualType ObjectTy,
3381 llvm::Value *BaseValue,
3382 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003383 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003384 const ObjCInterfaceDecl *ID =
3385 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003386 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3387 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003388}
3389
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003390llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003391 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003392 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003393 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003394 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003395 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3396 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003397}
3398
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003399/* *** Private Interface *** */
3400
3401/// EmitImageInfo - Emit the image info marker used to encode some module
3402/// level information.
3403///
3404/// See: <rdr://4810609&4810587&4810587>
3405/// struct IMAGE_INFO {
3406/// unsigned version;
3407/// unsigned flags;
3408/// };
3409enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003410 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003411 eImageInfo_GarbageCollected = (1 << 1),
3412 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003413 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3414
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003415 // A flag indicating that the module has no instances of a @synthesize of a
3416 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003417 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003418};
3419
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003420void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003421 unsigned version = 0; // Version is unused?
3422 unsigned flags = 0;
3423
3424 // FIXME: Fix and continue?
3425 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3426 flags |= eImageInfo_GarbageCollected;
3427 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3428 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003429
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003430 // We never allow @synthesize of a superclass property.
3431 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003432
Chris Lattner77b89b82010-06-27 07:15:29 +00003433 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3434
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003435 // Emitted as int[2];
3436 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003437 llvm::ConstantInt::get(Int32Ty, version),
3438 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003439 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003440 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003441
3442 const char *Section;
3443 if (ObjCABI == 1)
3444 Section = "__OBJC, __image_info,regular";
3445 else
3446 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003447 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003448 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003449 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003450 Section,
3451 0,
3452 true);
3453 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003454}
3455
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003456
3457// struct objc_module {
3458// unsigned long version;
3459// unsigned long size;
3460// const char *name;
3461// Symtab symtab;
3462// };
3463
3464// FIXME: Get from somewhere
3465static const int ModuleVersion = 7;
3466
3467void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003468 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003469
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003470 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003471 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3472 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003473 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003474 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003475 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003476 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003477 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003478 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003479 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003480}
3481
3482llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003483 unsigned NumClasses = DefinedClasses.size();
3484 unsigned NumCategories = DefinedCategories.size();
3485
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003486 // Return null if no symbols were defined.
3487 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003488 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003489
3490 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003491 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003492 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003493 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3494 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003495
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003496 // The runtime expects exactly the list of defined classes followed
3497 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003498 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003499 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003500 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003501 ObjCTypes.Int8PtrTy);
3502 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003503 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003504 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003505 ObjCTypes.Int8PtrTy);
3506
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003507 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003508 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003509 NumClasses + NumCategories),
3510 Symbols);
3511
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003512 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003513
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003514 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003515 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3516 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003517 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003518 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003519}
3520
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003521llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003522 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003523 LazySymbols.insert(ID->getIdentifier());
3524
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003525 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003526
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003527 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003528 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003529 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003530 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003531 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003532 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3533 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003534 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003535 }
3536
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003537 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003538}
3539
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003540llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3541 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003542 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003543
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003544 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003545 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003546 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003547 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003548 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003549 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3550 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003551 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003552 }
3553
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003554 if (lvalue)
3555 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003556 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003557}
3558
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003559llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003560 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003561
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003562 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003563 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003564 llvm::ConstantArray::get(VMContext,
3565 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003566 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003567 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003568
Owen Andersona1cf15f2009-07-14 23:10:40 +00003569 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003570}
3571
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003572/// GetIvarLayoutName - Returns a unique constant for the given
3573/// ivar layout bitmap.
3574llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003575 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003576 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003577}
3578
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003579void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003581 bool ForStrongLayout,
3582 bool &HasUnion) {
3583 const RecordDecl *RD = RT->getDecl();
3584 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003585 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003586 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003588 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003589
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003590 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3591 ForStrongLayout, HasUnion);
3592}
3593
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003594void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003595 const llvm::StructLayout *Layout,
3596 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003597 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003598 unsigned int BytePos, bool ForStrongLayout,
3599 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003600 bool IsUnion = (RD && RD->isUnion());
3601 uint64_t MaxUnionIvarSize = 0;
3602 uint64_t MaxSkippedUnionIvarSize = 0;
3603 FieldDecl *MaxField = 0;
3604 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003605 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003606 uint64_t MaxFieldOffset = 0;
3607 uint64_t MaxSkippedFieldOffset = 0;
3608 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003609
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003610 if (RecFields.empty())
3611 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003612 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3613 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3614
Chris Lattnerf1690852009-03-31 08:48:01 +00003615 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003616 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003617 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003618 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003619 // Note that 'i' here is actually the field index inside RD of Field,
3620 // although this dependency is hidden.
3621 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3622 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003623 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003624 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003625
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003626 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003627 if (!Field->getIdentifier() || Field->isBitField()) {
3628 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003629 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003630 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003631 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003632
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003633 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003634 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003635 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003636 if (FQT->isUnionType())
3637 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003638
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003639 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003640 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003641 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003642 continue;
3643 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003644
Chris Lattnerf1690852009-03-31 08:48:01 +00003645 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003646 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003647 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003648 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003649 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003650 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003651 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3652 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003653 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003654 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003655 FQT = CArray->getElementType();
3656 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003657
3658 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003659 "layout for array of unions not supported");
3660 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003661 int OldIndex = IvarsInfo.size() - 1;
3662 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003663
Ted Kremenek6217b802009-07-29 21:53:49 +00003664 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003665 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003666 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003667
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003668 // Replicate layout information for each array element. Note that
3669 // one element is already done.
3670 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003671 for (int FirstIndex = IvarsInfo.size() - 1,
3672 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003673 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003674 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3675 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3676 IvarsInfo[i].ivar_size));
3677 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3678 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3679 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003680 }
3681 continue;
3682 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003683 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003684 // At this point, we are done with Record/Union and array there of.
3685 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003686 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003687
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003688 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003689 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3690 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003691 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003692 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003693 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003694 MaxUnionIvarSize = UnionIvarSize;
3695 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003696 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003697 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003698 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003699 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003700 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003701 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003702 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003703 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3704 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003705 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003706 // FIXME: Why the asymmetry? We divide by word size in bits on other
3707 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003708 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003709 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003710 MaxSkippedUnionIvarSize = UnionIvarSize;
3711 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003712 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003713 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003714 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003715 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003716 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003717 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003718 }
3719 }
3720 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003721
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003722 if (LastFieldBitfield) {
3723 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003724 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3725 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003726 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003727 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003728 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003729 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3730 + ((BitFieldSize % ByteSizeInBits) != 0);
3731 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003732 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003733
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003734 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003735 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003736 MaxUnionIvarSize));
3737 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003738 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003739 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003740}
3741
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003742/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3743/// the computations and returning the layout bitmap (for ivar or blocks) in
3744/// the given argument BitMap string container. Routine reads
3745/// two containers, IvarsInfo and SkipIvars which are assumed to be
3746/// filled already by the caller.
3747llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003748 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003749 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003750
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003751 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003752 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003754 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003755 if (IvarsInfo[0].ivar_bytepos == 0) {
3756 WordsToSkip = 0;
3757 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003758 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003759 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3760 WordsToScan = IvarsInfo[0].ivar_size;
3761 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003762 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003763 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003764 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003765 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003766 // consecutive 'scanned' object pointers.
3767 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003768 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003769 // Skip over 'gc'able object pointer which lay over each other.
3770 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3771 continue;
3772 // Must skip over 1 or more words. We save current skip/scan values
3773 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003774 SKIP_SCAN SkScan;
3775 SkScan.skip = WordsToSkip;
3776 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003777 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003778
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003779 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003780 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3781 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003782 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003783 WordsToSkip = 0;
3784 WordsToScan = IvarsInfo[i].ivar_size;
3785 }
3786 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003787 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003788 SKIP_SCAN SkScan;
3789 SkScan.skip = WordsToSkip;
3790 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003791 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003792 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003793
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003794 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003795 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003796 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003797 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003798 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003799 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003800 IvarsInfo[LastIndex].ivar_bytepos +
3801 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003802 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003803 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003804 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003805 SKIP_SCAN SkScan;
3806 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3807 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003808 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003809 }
3810 }
3811 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3812 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003813 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003814 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003815 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3816 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3817 // 0xM0 followed by 0x0N detected.
3818 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3819 for (int j = i+1; j < SkipScan; j++)
3820 SkipScanIvars[j] = SkipScanIvars[j+1];
3821 --SkipScan;
3822 }
3823 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003824
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003825 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003826 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003827 unsigned char byte;
3828 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3829 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3830 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3831 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003832
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003833 // first skip big.
3834 for (unsigned int ix = 0; ix < skip_big; ix++)
3835 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003836
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003837 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003838 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003839 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003840 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003841 byte |= 0xf;
3842 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003843 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003844 byte |= scan_small;
3845 scan_small = 0;
3846 }
3847 BitMap += byte;
3848 }
3849 // next scan big
3850 for (unsigned int ix = 0; ix < scan_big; ix++)
3851 BitMap += (unsigned char)(0x0f);
3852 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003853 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003854 byte = scan_small;
3855 BitMap += byte;
3856 }
3857 }
3858 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003859 unsigned char zero = 0;
3860 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003861
3862 llvm::GlobalVariable * Entry =
3863 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3864 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3865 "__TEXT,__cstring,cstring_literals",
3866 1, true);
3867 return getConstantGEP(VMContext, Entry, 0, 0);
3868}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003869
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003870/// BuildIvarLayout - Builds ivar layout bitmap for the class
3871/// implementation for the __strong or __weak case.
3872/// The layout map displays which words in ivar list must be skipped
3873/// and which must be scanned by GC (see below). String is built of bytes.
3874/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3875/// of words to skip and right nibble is count of words to scan. So, each
3876/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3877/// represented by a 0x00 byte which also ends the string.
3878/// 1. when ForStrongLayout is true, following ivars are scanned:
3879/// - id, Class
3880/// - object *
3881/// - __strong anything
3882///
3883/// 2. When ForStrongLayout is false, following ivars are scanned:
3884/// - __weak anything
3885///
3886llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3887 const ObjCImplementationDecl *OMD,
3888 bool ForStrongLayout) {
3889 bool hasUnion = false;
3890
3891 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3892 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3893 return llvm::Constant::getNullValue(PtrTy);
3894
3895 llvm::SmallVector<FieldDecl*, 32> RecFields;
3896 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3897 CGM.getContext().CollectObjCIvars(OI, RecFields);
3898
3899 // Add this implementations synthesized ivars.
3900 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3901 CGM.getContext().CollectNonClassIvars(OI, Ivars);
3902 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3903 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3904
3905 if (RecFields.empty())
3906 return llvm::Constant::getNullValue(PtrTy);
3907
3908 SkipIvars.clear();
3909 IvarsInfo.clear();
3910
3911 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3912 if (IvarsInfo.empty())
3913 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003914 // Sort on byte position in case we encounterred a union nested in
3915 // the ivar list.
3916 if (hasUnion && !IvarsInfo.empty())
3917 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3918 if (hasUnion && !SkipIvars.empty())
3919 std::sort(SkipIvars.begin(), SkipIvars.end());
3920
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003921 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003922 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003923
3924 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003925 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003926 ForStrongLayout ? "strong" : "weak",
3927 OMD->getClassInterface()->getNameAsCString());
3928 const unsigned char *s = (unsigned char*)BitMap.c_str();
3929 for (unsigned i = 0; i < BitMap.size(); i++)
3930 if (!(s[i] & 0xf0))
3931 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3932 else
3933 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3934 printf("\n");
3935 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003936 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003937}
3938
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003939llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003940 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3941
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003942 // FIXME: Avoid std::string copying.
3943 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003944 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003945 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003946 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003947 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003948
Owen Andersona1cf15f2009-07-14 23:10:40 +00003949 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003950}
3951
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003952// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003953llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003954 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3955}
3956
3957// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003958llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003959 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3960}
3961
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003962llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003963 std::string TypeStr;
3964 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3965
3966 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003967
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003968 if (!Entry)
3969 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003970 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003971 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003972 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003973
Owen Andersona1cf15f2009-07-14 23:10:40 +00003974 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003975}
3976
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003977llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003978 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003979 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3980 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003981
3982 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3983
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003984 if (!Entry)
3985 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003986 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003987 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003988 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003989
Owen Andersona1cf15f2009-07-14 23:10:40 +00003990 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003991}
3992
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003993// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003994llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003995 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003996
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003997 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003998 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003999 llvm::ConstantArray::get(VMContext,
4000 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004001 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004002 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004003
Owen Andersona1cf15f2009-07-14 23:10:40 +00004004 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004005}
4006
4007// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004008// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004009llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004010CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4011 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004012 std::string TypeStr;
4013 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004014 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4015}
4016
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004017void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004018 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004019 llvm::SmallVectorImpl<char> &Name) {
4020 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004021 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004022 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4023 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004024 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004025 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004026 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004027 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004028}
4029
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004030void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004031 EmitModuleInfo();
4032
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004033 // Emit the dummy bodies for any protocols which were referenced but
4034 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004035 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004036 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4037 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004038 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004039
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004040 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004041 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004042 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004043 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004044 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004045 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004046 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004047 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004048 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004049 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004050 }
4051
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004052 // Add assembler directives to add lazy undefined symbol references
4053 // for classes which are referenced but not defined. This is
4054 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004055 //
4056 // FIXME: It would be nice if we had an LLVM construct for this.
4057 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4058 llvm::SmallString<256> Asm;
4059 Asm += CGM.getModule().getModuleInlineAsm();
4060 if (!Asm.empty() && Asm.back() != '\n')
4061 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004062
Daniel Dunbar33063492009-09-07 00:20:42 +00004063 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004064 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4065 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004066 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4067 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004068 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004069 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004070 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004071 }
4072
4073 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4074 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4075 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4076 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004077
Daniel Dunbar33063492009-09-07 00:20:42 +00004078 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004079 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004080}
4081
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004082CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004083 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004084 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004085 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004086 ObjCABI = 2;
4087}
4088
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004089/* *** */
4090
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004091ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004092 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004093 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4094 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004095
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004096 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004097 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004098 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004099 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004100 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004101
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004102 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004103 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004104 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004105
Mike Stumpf5408fe2009-05-16 07:57:57 +00004106 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4107 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004108 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004109 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004110
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004111 // I'm not sure I like this. The implicit coordination is a bit
4112 // gross. We should solve this in a reasonable fashion because this
4113 // is a pretty common task (match some runtime data structure with
4114 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004115
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004116 // FIXME: This is leaked.
4117 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004118
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004119 // struct _objc_super {
4120 // id self;
4121 // Class cls;
4122 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004123 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004124 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004125 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004126 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004127 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004128 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004129 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004130 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004131 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004132
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004133 SuperCTy = Ctx.getTagDeclType(RD);
4134 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004135
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004136 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004137 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4138
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004139 // struct _prop_t {
4140 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004142 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004143 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004145 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004146
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004147 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004148 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004149 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004150 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004151 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004152 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004153 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004154 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004155 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004156 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004157 PropertyListTy);
4158 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004159 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004160
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004161 // struct _objc_method {
4162 // SEL _cmd;
4163 // char *method_type;
4164 // char *_imp;
4165 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004166 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004167 Int8PtrTy,
4168 Int8PtrTy,
4169 NULL);
4170 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004172 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004173 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004174 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004175 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004176}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004177
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004178ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004179 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004180 // struct _objc_method_description {
4181 // SEL name;
4182 // char *types;
4183 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004184 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004185 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004186 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004187 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004188 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004189 MethodDescriptionTy);
4190
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004191 // struct _objc_method_description_list {
4192 // int count;
4193 // struct _objc_method_description[1];
4194 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004195 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004196 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004197 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004198 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004199 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004200 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004202 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004203 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004204 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004205
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004206 // Protocol description structures
4207
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004208 // struct _objc_protocol_extension {
4209 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4210 // struct _objc_method_description_list *optional_instance_methods;
4211 // struct _objc_method_description_list *optional_class_methods;
4212 // struct _objc_property_list *instance_properties;
4213 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004215 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004216 MethodDescriptionListPtrTy,
4217 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004218 PropertyListPtrTy,
4219 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004221 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004222
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004223 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004224 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004225
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004226 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004227
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004228 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4229 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004231 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004232 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004233 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004234 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004235 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004236 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004237 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4238
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004239 // struct _objc_protocol {
4240 // struct _objc_protocol_extension *isa;
4241 // char *protocol_name;
4242 // struct _objc_protocol **_objc_protocol_list;
4243 // struct _objc_method_description_list *instance_methods;
4244 // struct _objc_method_description_list *class_methods;
4245 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004246 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004247 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004248 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004249 MethodDescriptionListPtrTy,
4250 MethodDescriptionListPtrTy,
4251 NULL);
4252 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4253
4254 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004255 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004256 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004257 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004258 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004259
4260 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004261 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004262 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004263
4264 // Class description structures
4265
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004266 // struct _objc_ivar {
4267 // char *ivar_name;
4268 // char *ivar_type;
4269 // int ivar_offset;
4270 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004271 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004272 Int8PtrTy,
4273 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004274 NULL);
4275 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4276
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004277 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004278 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004279 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004280 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004281
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004282 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004283 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004284 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004285 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004286
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004287 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004288 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004289 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004290 Int8PtrTy,
4291 PropertyListPtrTy,
4292 NULL);
4293 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004294 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004295
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004296 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004297
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004298 // struct _objc_class {
4299 // Class isa;
4300 // Class super_class;
4301 // char *name;
4302 // long version;
4303 // long info;
4304 // long instance_size;
4305 // struct _objc_ivar_list *ivars;
4306 // struct _objc_method_list *methods;
4307 // struct _objc_cache *cache;
4308 // struct _objc_protocol_list *protocols;
4309 // char *ivar_layout;
4310 // struct _objc_class_ext *ext;
4311 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004312 T = llvm::StructType::get(VMContext,
4313 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004314 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004315 Int8PtrTy,
4316 LongTy,
4317 LongTy,
4318 LongTy,
4319 IvarListPtrTy,
4320 MethodListPtrTy,
4321 CachePtrTy,
4322 ProtocolListPtrTy,
4323 Int8PtrTy,
4324 ClassExtensionPtrTy,
4325 NULL);
4326 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004327
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004328 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4329 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004330 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004331
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004332 // struct _objc_category {
4333 // char *category_name;
4334 // char *class_name;
4335 // struct _objc_method_list *instance_method;
4336 // struct _objc_method_list *class_method;
4337 // uint32_t size; // sizeof(struct _objc_category)
4338 // struct _objc_property_list *instance_properties;// category's @property
4339 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004340 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004341 Int8PtrTy,
4342 MethodListPtrTy,
4343 MethodListPtrTy,
4344 ProtocolListPtrTy,
4345 IntTy,
4346 PropertyListPtrTy,
4347 NULL);
4348 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4349
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004350 // Global metadata structures
4351
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004352 // struct _objc_symtab {
4353 // long sel_ref_cnt;
4354 // SEL *refs;
4355 // short cls_def_cnt;
4356 // short cat_def_cnt;
4357 // char *defs[cls_def_cnt + cat_def_cnt];
4358 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004359 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004360 SelectorPtrTy,
4361 ShortTy,
4362 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004363 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004364 NULL);
4365 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004366 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004367
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004368 // struct _objc_module {
4369 // long version;
4370 // long size; // sizeof(struct _objc_module)
4371 // char *name;
4372 // struct _objc_symtab* symtab;
4373 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004375 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004376 LongTy,
4377 Int8PtrTy,
4378 SymtabPtrTy,
4379 NULL);
4380 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004381
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
Mike Stumpf5408fe2009-05-16 07:57:57 +00004383 // FIXME: This is the size of the setjmp buffer and should be target
4384 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004385 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004386
Anders Carlsson124526b2008-09-09 10:10:21 +00004387 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004388 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004389 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004390
4391 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004392 llvm::StructType::get(VMContext,
4393 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4394 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004395 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004396 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004397 ExceptionDataTy);
4398
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004399}
4400
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004401ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004402 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004403 // struct _method_list_t {
4404 // uint32_t entsize; // sizeof(struct _objc_method)
4405 // uint32_t method_count;
4406 // struct _objc_method method_list[method_count];
4407 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004408 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004409 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004410 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004411 NULL);
4412 CGM.getModule().addTypeName("struct.__method_list_t",
4413 MethodListnfABITy);
4414 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004415 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004416
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004417 // struct _protocol_t {
4418 // id isa; // NULL
4419 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004420 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004421 // const struct method_list_t * const instance_methods;
4422 // const struct method_list_t * const class_methods;
4423 // const struct method_list_t *optionalInstanceMethods;
4424 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004425 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004426 // const uint32_t size; // sizeof(struct _protocol_t)
4427 // const uint32_t flags; // = 0
4428 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004429
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004430 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004431 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432
Owen Anderson47a434f2009-08-05 23:18:46 +00004433 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004434 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004435 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004436 ProtocolListTyHolder),
4437 MethodListnfABIPtrTy,
4438 MethodListnfABIPtrTy,
4439 MethodListnfABIPtrTy,
4440 MethodListnfABIPtrTy,
4441 PropertyListPtrTy,
4442 IntTy,
4443 IntTy,
4444 NULL);
4445 CGM.getModule().addTypeName("struct._protocol_t",
4446 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004447
4448 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004449 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450
Fariborz Jahanianda320092009-01-29 19:24:30 +00004451 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004452 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004453 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004454 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004455 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004456 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004457 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004458 NULL);
4459 CGM.getModule().addTypeName("struct._objc_protocol_list",
4460 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004461 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004462 ProtocolListnfABITy);
4463
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004464 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004465 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004466
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004467 // struct _ivar_t {
4468 // unsigned long int *offset; // pointer to ivar offset location
4469 // char *name;
4470 // char *type;
4471 // uint32_t alignment;
4472 // uint32_t size;
4473 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004474 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004475 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004476 Int8PtrTy,
4477 Int8PtrTy,
4478 IntTy,
4479 IntTy,
4480 NULL);
4481 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004482
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004483 // struct _ivar_list_t {
4484 // uint32 entsize; // sizeof(struct _ivar_t)
4485 // uint32 count;
4486 // struct _iver_t list[count];
4487 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004488 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004489 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004490 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004491 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004492 NULL);
4493 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004494
Owen Anderson96e0fc72009-07-29 22:16:19 +00004495 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004497 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004498 // uint32_t const flags;
4499 // uint32_t const instanceStart;
4500 // uint32_t const instanceSize;
4501 // uint32_t const reserved; // only when building for 64bit targets
4502 // const uint8_t * const ivarLayout;
4503 // const char *const name;
4504 // const struct _method_list_t * const baseMethods;
4505 // const struct _objc_protocol_list *const baseProtocols;
4506 // const struct _ivar_list_t *const ivars;
4507 // const uint8_t * const weakIvarLayout;
4508 // const struct _prop_list_t * const properties;
4509 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004510
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004511 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004512 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004513 IntTy,
4514 IntTy,
4515 Int8PtrTy,
4516 Int8PtrTy,
4517 MethodListnfABIPtrTy,
4518 ProtocolListnfABIPtrTy,
4519 IvarListnfABIPtrTy,
4520 Int8PtrTy,
4521 PropertyListPtrTy,
4522 NULL);
4523 CGM.getModule().addTypeName("struct._class_ro_t",
4524 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004525
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004526 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4527 std::vector<const llvm::Type*> Params;
4528 Params.push_back(ObjectPtrTy);
4529 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004530 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4532
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004533 // struct _class_t {
4534 // struct _class_t *isa;
4535 // struct _class_t * const superclass;
4536 // void *cache;
4537 // IMP *vtable;
4538 // struct class_ro_t *ro;
4539 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004540
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004541 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004542 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004543 llvm::StructType::get(VMContext,
4544 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545 llvm::PointerType::getUnqual(ClassTyHolder),
4546 CachePtrTy,
4547 llvm::PointerType::getUnqual(ImpnfABITy),
4548 llvm::PointerType::getUnqual(ClassRonfABITy),
4549 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004550 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4551
4552 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004553 ClassnfABITy);
4554
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004555 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004556 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004558 // struct _category_t {
4559 // const char * const name;
4560 // struct _class_t *const cls;
4561 // const struct _method_list_t * const instance_methods;
4562 // const struct _method_list_t * const class_methods;
4563 // const struct _protocol_list_t * const protocols;
4564 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004565 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004566 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004567 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004568 MethodListnfABIPtrTy,
4569 MethodListnfABIPtrTy,
4570 ProtocolListnfABIPtrTy,
4571 PropertyListPtrTy,
4572 NULL);
4573 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004574
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004575 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004576 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4577 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004578
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004579 // MessageRefTy - LLVM for:
4580 // struct _message_ref_t {
4581 // IMP messenger;
4582 // SEL name;
4583 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004585 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004586 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004587 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004588 SourceLocation(),
4589 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004590 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004591 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004592 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004593 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004594 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004595
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004596 MessageRefCTy = Ctx.getTagDeclType(RD);
4597 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4598 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004600 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004601 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004603 // SuperMessageRefTy - LLVM for:
4604 // struct _super_message_ref_t {
4605 // SUPER_IMP messenger;
4606 // SEL name;
4607 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004608 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004609 SelectorPtrTy,
4610 NULL);
4611 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004612
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004613 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004614 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4615
Daniel Dunbare588b992009-03-01 04:46:24 +00004616
4617 // struct objc_typeinfo {
4618 // const void** vtable; // objc_ehtype_vtable + 2
4619 // const char* name; // c++ typeinfo string
4620 // Class cls;
4621 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004622 EHTypeTy = llvm::StructType::get(VMContext,
4623 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004624 Int8PtrTy,
4625 ClassnfABIPtrTy,
4626 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004627 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004628 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004629}
4630
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004631llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004632 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004633
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004634 return NULL;
4635}
4636
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004637void CGObjCNonFragileABIMac::AddModuleClassList(const
4638 std::vector<llvm::GlobalValue*>
4639 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004640 const char *SymbolName,
4641 const char *SectionName) {
4642 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004643
Daniel Dunbar463b8762009-05-15 21:48:48 +00004644 if (!NumClasses)
4645 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004646
Daniel Dunbar463b8762009-05-15 21:48:48 +00004647 std::vector<llvm::Constant*> Symbols(NumClasses);
4648 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004649 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004650 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004652 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004653 NumClasses),
4654 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004655
Daniel Dunbar463b8762009-05-15 21:48:48 +00004656 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004657 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004658 llvm::GlobalValue::InternalLinkage,
4659 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004660 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004661 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004662 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004663 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004664}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004665
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004666void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4667 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004668
Daniel Dunbar463b8762009-05-15 21:48:48 +00004669 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004670 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004671 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004672 "\01L_OBJC_LABEL_CLASS_$",
4673 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004674
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004675 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4676 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4677 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4678 continue;
4679 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004680 }
4681
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004682 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4683 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4684 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4685 continue;
4686 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4687 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004688
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004689 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004690 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4691 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004693 // Build list of all implemented category addresses in array
4694 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004695 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004696 "\01L_OBJC_LABEL_CATEGORY_$",
4697 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004698 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004699 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4700 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004702 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004703}
4704
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004705/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004706/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004708/// message dispatch call for all the rest.
4709///
4710bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004711 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4712 default:
4713 assert(0 && "Invalid dispatch method!");
4714 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004715 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004716 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004717 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004718 case CodeGenOptions::Mixed:
4719 break;
4720 }
4721
4722 // If so, see whether this selector is in the white-list of things which must
4723 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004724 if (NonLegacyDispatchMethods.empty()) {
4725 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4726 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4727 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4728 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4729 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4730 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4731 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4732 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4733 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4734 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004736 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4737 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4738 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4739 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4740 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4741 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4742 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4743 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004744 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004745 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004746 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4747 &CGM.getContext().Idents.get("objects"),
4748 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004749 };
4750 NonLegacyDispatchMethods.insert(
4751 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004752 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004753
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004754 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004755}
4756
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004757// Metadata flags
4758enum MetaDataDlags {
4759 CLS = 0x0,
4760 CLS_META = 0x1,
4761 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004762 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004763 CLS_EXCEPTION = 0x20
4764};
4765/// BuildClassRoTInitializer - generate meta-data for:
4766/// struct _class_ro_t {
4767/// uint32_t const flags;
4768/// uint32_t const instanceStart;
4769/// uint32_t const instanceSize;
4770/// uint32_t const reserved; // only when building for 64bit targets
4771/// const uint8_t * const ivarLayout;
4772/// const char *const name;
4773/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004774/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004775/// const struct _ivar_list_t *const ivars;
4776/// const uint8_t * const weakIvarLayout;
4777/// const struct _prop_list_t * const properties;
4778/// }
4779///
4780llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004781 unsigned flags,
4782 unsigned InstanceStart,
4783 unsigned InstanceSize,
4784 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004785 std::string ClassName = ID->getNameAsString();
4786 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004787 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4788 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4789 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004790 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004791 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4792 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004793 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004794 // const struct _method_list_t * const baseMethods;
4795 std::vector<llvm::Constant*> Methods;
4796 std::string MethodListName("\01l_OBJC_$_");
4797 if (flags & CLS_META) {
4798 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004799 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004800 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004801 // Class methods should always be defined.
4802 Methods.push_back(GetMethodConstant(*i));
4803 }
4804 } else {
4805 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004806 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004807 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004808 // Instance methods should always be defined.
4809 Methods.push_back(GetMethodConstant(*i));
4810 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004811 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004812 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004813 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004814
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004815 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4816 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004817
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004818 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4819 if (llvm::Constant *C = GetMethodConstant(MD))
4820 Methods.push_back(C);
4821 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4822 if (llvm::Constant *C = GetMethodConstant(MD))
4823 Methods.push_back(C);
4824 }
4825 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004826 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004827 Values[ 5] = EmitMethodList(MethodListName,
4828 "__DATA, __objc_const", Methods);
4829
Fariborz Jahanianda320092009-01-29 19:24:30 +00004830 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4831 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004833 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004834 OID->protocol_begin(),
4835 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004836
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004837 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004838 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004839 else
4840 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004841 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4842 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004843 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004844 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004845 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004846 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4847 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004848 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004849 Values);
4850 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4852 llvm::GlobalValue::InternalLinkage,
4853 Init,
4854 (flags & CLS_META) ?
4855 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4856 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004857 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004858 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004859 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004860 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004861
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004862}
4863
4864/// BuildClassMetaData - This routine defines that to-level meta-data
4865/// for the given ClassName for:
4866/// struct _class_t {
4867/// struct _class_t *isa;
4868/// struct _class_t * const superclass;
4869/// void *cache;
4870/// IMP *vtable;
4871/// struct class_ro_t *ro;
4872/// }
4873///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004874llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004875 std::string &ClassName,
4876 llvm::Constant *IsAGV,
4877 llvm::Constant *SuperClassGV,
4878 llvm::Constant *ClassRoGV,
4879 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004880 std::vector<llvm::Constant*> Values(5);
4881 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882 Values[1] = SuperClassGV;
4883 if (!Values[1])
4884 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004885 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4886 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4887 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004888 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004889 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004890 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4891 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004892 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004893 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004894 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004895 if (HiddenVisibility)
4896 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004897 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004898}
4899
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004900bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004901CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004902 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004903}
4904
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004905void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004906 uint32_t &InstanceStart,
4907 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004908 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004909 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004910
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004911 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004912 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004913
4914 // If there are no fields, the start is the same as the end.
4915 if (!RL.getFieldCount())
4916 InstanceStart = InstanceSize;
4917 else
4918 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004919}
4920
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004921void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4922 std::string ClassName = ID->getNameAsString();
4923 if (!ObjCEmptyCacheVar) {
4924 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004925 CGM.getModule(),
4926 ObjCTypes.CacheTy,
4927 false,
4928 llvm::GlobalValue::ExternalLinkage,
4929 0,
4930 "_objc_empty_cache");
4931
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004932 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004933 CGM.getModule(),
4934 ObjCTypes.ImpnfABITy,
4935 false,
4936 llvm::GlobalValue::ExternalLinkage,
4937 0,
4938 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004939 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004940 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004941 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004942 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004943 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004944 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004945 uint32_t InstanceSize = InstanceStart;
4946 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004947 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4948 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004949
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004950 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004951
4952 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004953 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004954 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004955 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004956 if (ID->getNumIvarInitializers())
4957 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004958 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004959 // class is root
4960 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004961 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004962 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004963 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004964 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004965 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4966 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4967 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004968 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004969 if (Root->hasAttr<WeakImportAttr>())
4970 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004971 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004972 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004973 ObjCMetaClassName +
4974 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004975 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004976 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4977 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004978 }
4979 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4980 InstanceStart,
4981 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004982 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004983 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004984 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4985 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004986 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004987
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004988 // Metadata for the class
4989 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004990 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004991 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004992 if (ID->getNumIvarInitializers())
4993 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004994
Douglas Gregor68584ed2009-06-18 16:11:24 +00004995 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004996 flags |= CLS_EXCEPTION;
4997
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004998 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004999 flags |= CLS_ROOT;
5000 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005001 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005002 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005003 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005004 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005005 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005006 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5007 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005008 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005009 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005010 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005011 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005013 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005015 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005016 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005017 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5018 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005019 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005020
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005021 // Determine if this class is also "non-lazy".
5022 if (ImplementationIsNonLazy(ID))
5023 DefinedNonLazyClasses.push_back(ClassMD);
5024
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005025 // Force the definition of the EHType if necessary.
5026 if (flags & CLS_EXCEPTION)
5027 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005028}
5029
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005030/// GenerateProtocolRef - This routine is called to generate code for
5031/// a protocol reference expression; as in:
5032/// @code
5033/// @protocol(Proto1);
5034/// @endcode
5035/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5036/// which will hold address of the protocol meta-data.
5037///
5038llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005039 const ObjCProtocolDecl *PD) {
5040
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005041 // This routine is called for @protocol only. So, we must build definition
5042 // of protocol's meta-data (not a reference to it!)
5043 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005044 llvm::Constant *Init =
5045 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5046 ObjCTypes.ExternalProtocolPtrTy);
5047
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005048 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
5049 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005050
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005051 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5052 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005053 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005054 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055 CGM.getModule(),
5056 Init->getType(), false,
5057 llvm::GlobalValue::WeakAnyLinkage,
5058 Init,
5059 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005060 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5061 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005062 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005063 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005064}
5065
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005066/// GenerateCategory - Build metadata for a category implementation.
5067/// struct _category_t {
5068/// const char * const name;
5069/// struct _class_t *const cls;
5070/// const struct _method_list_t * const instance_methods;
5071/// const struct _method_list_t * const class_methods;
5072/// const struct _protocol_list_t * const protocols;
5073/// const struct _prop_list_t * const properties;
5074/// }
5075///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005076void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005077 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005078 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005079 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5080 "_$_" + OCD->getNameAsString());
5081 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005082 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005083
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005084 std::vector<llvm::Constant*> Values(6);
5085 Values[0] = GetClassName(OCD->getIdentifier());
5086 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005087 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005088 if (Interface->hasAttr<WeakImportAttr>())
5089 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5090
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005091 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005092 std::vector<llvm::Constant*> Methods;
5093 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005094 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005095 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096
5097 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005098 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005099 // Instance methods should always be defined.
5100 Methods.push_back(GetMethodConstant(*i));
5101 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005102
5103 Values[2] = EmitMethodList(MethodListName,
5104 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005105 Methods);
5106
5107 MethodListName = Prefix;
5108 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5109 OCD->getNameAsString();
5110 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005111 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005112 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005113 // Class methods should always be defined.
5114 Methods.push_back(GetMethodConstant(*i));
5115 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005116
5117 Values[3] = EmitMethodList(MethodListName,
5118 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005119 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005120 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005121 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005122 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005123 llvm::SmallString<256> ExtName;
5124 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5125 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005126 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005127 + Interface->getName() + "_$_"
5128 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005129 Category->protocol_begin(),
5130 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005131 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5132 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005133 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005134 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5135 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137
5138 llvm::Constant *Init =
5139 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005140 Values);
5141 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005142 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005143 false,
5144 llvm::GlobalValue::InternalLinkage,
5145 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005146 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005147 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005148 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005149 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005150 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005151 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005152
5153 // Determine if this category is also "non-lazy".
5154 if (ImplementationIsNonLazy(OCD))
5155 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005156}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005157
5158/// GetMethodConstant - Return a struct objc_method constant for the
5159/// given method if it has been defined. The result is null if the
5160/// method has not been defined. The return value has type MethodPtrTy.
5161llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005162 const ObjCMethodDecl *MD) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005163 // FIXME: Use DenseMap::lookup
5164 llvm::Function *Fn = MethodDefinitions[MD];
5165 if (!Fn)
5166 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005167
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005168 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005169 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005170 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005171 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005172 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005173 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005174 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005175}
5176
5177/// EmitMethodList - Build meta-data for method declarations
5178/// struct _method_list_t {
5179/// uint32_t entsize; // sizeof(struct _objc_method)
5180/// uint32_t method_count;
5181/// struct _objc_method method_list[method_count];
5182/// }
5183///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005184llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5185 const char *Section,
5186 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005187 // Return null for empty list.
5188 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005189 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005190
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005191 std::vector<llvm::Constant*> Values(3);
5192 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005193 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005194 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005195 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005196 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005197 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005198 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005199 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005200 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005201
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005202 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005203 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005204 llvm::GlobalValue::InternalLinkage,
5205 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005206 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005207 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005208 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005209 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005210 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005211 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005212 ObjCTypes.MethodListnfABIPtrTy);
5213}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005214
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005215/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5216/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005217llvm::GlobalVariable *
5218CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5219 const ObjCIvarDecl *Ivar) {
5220 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005221 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005222 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005223 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005224 CGM.getModule().getGlobalVariable(Name);
5225 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005226 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005227 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005228 false,
5229 llvm::GlobalValue::ExternalLinkage,
5230 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005231 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005232 return IvarOffsetGV;
5233}
5234
Daniel Dunbare83be122010-04-02 21:14:02 +00005235llvm::Constant *
5236CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5237 const ObjCIvarDecl *Ivar,
5238 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005239 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005240 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005241 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005242 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005243 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005244
Mike Stumpf5408fe2009-05-16 07:57:57 +00005245 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5246 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005247 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5248 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5249 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005250 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005251 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005252 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005253 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005254 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005255}
5256
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005257/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005258/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005259/// IvarListnfABIPtrTy.
5260/// struct _ivar_t {
5261/// unsigned long int *offset; // pointer to ivar offset location
5262/// char *name;
5263/// char *type;
5264/// uint32_t alignment;
5265/// uint32_t size;
5266/// }
5267/// struct _ivar_list_t {
5268/// uint32 entsize; // sizeof(struct _ivar_t)
5269/// uint32 count;
5270/// struct _iver_t list[count];
5271/// }
5272///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005273
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005274llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005275 const ObjCImplementationDecl *ID) {
5276
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005277 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005279 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5280 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005281
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005282 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005283
Daniel Dunbar91636d62009-04-20 00:33:43 +00005284 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005285 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005286 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005287
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005288 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5289 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005290 // Ignore unnamed bit-fields.
5291 if (!IVD->getDeclName())
5292 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005293 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005294 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005295 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5296 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005297 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005298 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005299 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005300 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005301 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005302 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005303 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005304 // NOTE. Size of a bitfield does not match gcc's, because of the
5305 // way bitfields are treated special in each. But I am told that
5306 // 'size' for bitfield ivars is ignored by the runtime so it does
5307 // not matter. If it matters, there is enough info to get the
5308 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005309 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005310 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005311 }
5312 // Return null for empty list.
5313 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005314 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005315 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005316 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005317 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5318 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005319 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005320 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005321 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005322 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005323 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5324 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005325 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005326 llvm::GlobalValue::InternalLinkage,
5327 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005328 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005329 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005330 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005331 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005332
Chris Lattnerad64e022009-07-17 23:57:13 +00005333 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005334 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005335}
5336
5337llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005338 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005339 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005340
Fariborz Jahanianda320092009-01-29 19:24:30 +00005341 if (!Entry) {
5342 // We use the initializer as a marker of whether this is a forward
5343 // reference or not. At module finalization we add the empty
5344 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005345 Entry =
5346 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5347 llvm::GlobalValue::ExternalLinkage,
5348 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005349 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005350 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005351 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005352
Fariborz Jahanianda320092009-01-29 19:24:30 +00005353 return Entry;
5354}
5355
5356/// GetOrEmitProtocol - Generate the protocol meta-data:
5357/// @code
5358/// struct _protocol_t {
5359/// id isa; // NULL
5360/// const char * const protocol_name;
5361/// const struct _protocol_list_t * protocol_list; // super protocols
5362/// const struct method_list_t * const instance_methods;
5363/// const struct method_list_t * const class_methods;
5364/// const struct method_list_t *optionalInstanceMethods;
5365/// const struct method_list_t *optionalClassMethods;
5366/// const struct _prop_list_t * properties;
5367/// const uint32_t size; // sizeof(struct _protocol_t)
5368/// const uint32_t flags; // = 0
5369/// }
5370/// @endcode
5371///
5372
5373llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005375 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005376
Fariborz Jahanianda320092009-01-29 19:24:30 +00005377 // Early exit if a defining object has already been generated.
5378 if (Entry && Entry->hasInitializer())
5379 return Entry;
5380
Fariborz Jahanianda320092009-01-29 19:24:30 +00005381 // Construct method lists.
5382 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5383 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005384 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005385 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005386 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005387 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005388 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5389 OptInstanceMethods.push_back(C);
5390 } else {
5391 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005392 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005394
5395 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005396 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005397 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005398 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005399 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5400 OptClassMethods.push_back(C);
5401 } else {
5402 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005405
Fariborz Jahanianda320092009-01-29 19:24:30 +00005406 std::vector<llvm::Constant*> Values(10);
5407 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005408 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005409 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005410 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5411 PD->protocol_begin(),
5412 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005413
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005414 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005415 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 "__DATA, __objc_const",
5417 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005418 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005419 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005420 "__DATA, __objc_const",
5421 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005422 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005423 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005424 "__DATA, __objc_const",
5425 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005426 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005427 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005428 "__DATA, __objc_const",
5429 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005430 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005431 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005433 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005434 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005435 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005436 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005437 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005438
Fariborz Jahanianda320092009-01-29 19:24:30 +00005439 if (Entry) {
5440 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005441 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005442 Entry->setInitializer(Init);
5443 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005445 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5446 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5447 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005448 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005449 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005450 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005451 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005452 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005453 CGM.AddUsedGlobal(Entry);
5454
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005455 // Use this protocol meta-data to build protocol list table in section
5456 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005457 llvm::GlobalVariable *PTGV =
5458 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5459 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5460 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005461 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005462 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005463 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005464 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005465 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005466 return Entry;
5467}
5468
5469/// EmitProtocolList - Generate protocol list meta-data:
5470/// @code
5471/// struct _protocol_list_t {
5472/// long protocol_count; // Note, this is 32/64 bit
5473/// struct _protocol_t[protocol_count];
5474/// }
5475/// @endcode
5476///
5477llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005478CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5479 ObjCProtocolDecl::protocol_iterator begin,
5480 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005481 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005482
Fariborz Jahanianda320092009-01-29 19:24:30 +00005483 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005485 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005486
Daniel Dunbar948e2582009-02-15 07:36:20 +00005487 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005488 llvm::SmallString<256> TmpName;
5489 Name.toVector(TmpName);
5490 llvm::GlobalVariable *GV =
5491 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005492 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005493 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005494
Daniel Dunbar948e2582009-02-15 07:36:20 +00005495 for (; begin != end; ++begin)
5496 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5497
Fariborz Jahanianda320092009-01-29 19:24:30 +00005498 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005499 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005500 ObjCTypes.ProtocolnfABIPtrTy));
5501
Fariborz Jahanianda320092009-01-29 19:24:30 +00005502 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005503 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005504 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005505 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005506 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005507 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005508 ProtocolRefs.size()),
5509 ProtocolRefs);
5510
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005511 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005512 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005513 llvm::GlobalValue::InternalLinkage,
5514 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005515 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005516 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005517 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005518 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005519 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005520 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005521 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005522}
5523
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005524/// GetMethodDescriptionConstant - This routine build following meta-data:
5525/// struct _objc_method {
5526/// SEL _cmd;
5527/// char *method_type;
5528/// char *_imp;
5529/// }
5530
5531llvm::Constant *
5532CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5533 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005534 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5536 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005537 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005538 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005539 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005540 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005541}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005542
5543/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5544/// This code gen. amounts to generating code for:
5545/// @code
5546/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5547/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005548///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005549LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005550 CodeGen::CodeGenFunction &CGF,
5551 QualType ObjectTy,
5552 llvm::Value *BaseValue,
5553 const ObjCIvarDecl *Ivar,
5554 unsigned CVRQualifiers) {
5555 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005556 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5557 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005558}
5559
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005560llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005561 CodeGen::CodeGenFunction &CGF,
5562 const ObjCInterfaceDecl *Interface,
5563 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005564 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005565}
5566
Fariborz Jahanian46551122009-02-04 00:22:57 +00005567CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005568 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005569 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005570 QualType ResultType,
5571 Selector Sel,
5572 llvm::Value *Receiver,
5573 QualType Arg0Ty,
5574 bool IsSuper,
5575 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005576 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5577 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005578 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005579 llvm::Value *Arg0 = Receiver;
5580 if (!IsSuper)
5581 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005583 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005584 // FIXME. This is too much work to get the ABI-specific result type needed to
5585 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005586 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005587 = Types.getFunctionInfo(ResultType, CallArgList(),
5588 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005589 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005590 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005591 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005592#if 0
5593 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005594 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005595 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005596 // FIXME. Is there a better way of getting these names.
5597 // They are available in RuntimeFunctions vector pair.
5598 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005599 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005600#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005601 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005602 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005603 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005604 } else {
5605 Fn = ObjCTypes.getMessageSendStretFixupFn();
5606 Name += "objc_msgSend_stret_fixup";
5607 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005608 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5609 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5610 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005611 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005612#if 0
5613// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005614 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005615 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005616 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005617 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005618#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005619 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005620 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005621 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005622 } else {
5623 Fn = ObjCTypes.getMessageSendFixupFn();
5624 Name += "objc_msgSend_fixup";
5625 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005626 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005627 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005628 Name += '_';
5629 std::string SelName(Sel.getAsString());
5630 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005631 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005632 if (SelName[i] == ':')
5633 SelName[i] = '_';
5634 Name += SelName;
5635 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5636 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005637 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005638 std::vector<llvm::Constant*> Values(2);
5639 Values[0] = Fn;
5640 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005641 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005642 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005643 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005644 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005645 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005646 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005647 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005648 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005649 }
5650 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005651
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005652 CallArgList ActualArgs;
5653 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005654 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005655 ObjCTypes.MessageRefCPtrTy));
5656 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005657 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005658 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005659 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5660 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005661 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005662 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005663 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005664 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005665}
5666
5667/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005668CodeGen::RValue
5669CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005670 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005671 QualType ResultType,
5672 Selector Sel,
5673 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005674 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005675 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005676 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005677 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005678 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5679 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005681 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005682 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005683 Receiver, CGF.getContext().getObjCIdType(),
5684 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005685}
5686
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005687llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005688CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005689 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5690
Daniel Dunbardfff2302009-03-02 05:18:14 +00005691 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005693 false, llvm::GlobalValue::ExternalLinkage,
5694 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005695 }
5696
5697 return GV;
5698}
5699
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005700llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5701 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005702 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005704 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005705 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005706 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 Entry =
5708 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005709 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005711 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005712 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005713 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005714 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005715 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005716 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005717 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005718
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005719 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005720}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005721
Daniel Dunbar11394522009-04-18 08:51:00 +00005722llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005724 const ObjCInterfaceDecl *ID) {
5725 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726
Daniel Dunbar11394522009-04-18 08:51:00 +00005727 if (!Entry) {
5728 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5729 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005730 Entry =
5731 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005732 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005733 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005734 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005735 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005736 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005738 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005739 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005740 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005742 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005743}
5744
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005745/// EmitMetaClassRef - Return a Value * of the address of _class_t
5746/// meta-data
5747///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005748llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5749 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005750 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5751 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005752 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005753
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005754 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005755 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005756 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005757 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005758 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005760 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005761 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005762 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 ObjCTypes.ClassnfABIPtrTy));
5764
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005765 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005766 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005767
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005768 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005769}
5770
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005771/// GetClass - Return a reference to the class for the given interface
5772/// decl.
5773llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5774 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005775 if (ID->hasAttr<WeakImportAttr>()) {
5776 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5777 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5778 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5779 }
5780
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005781 return EmitClassRef(Builder, ID);
5782}
5783
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005784/// Generates a message send where the super is the receiver. This is
5785/// a message send to self with special delivery semantics indicating
5786/// which class's method should be called.
5787CodeGen::RValue
5788CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005789 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005790 QualType ResultType,
5791 Selector Sel,
5792 const ObjCInterfaceDecl *Class,
5793 bool isCategoryImpl,
5794 llvm::Value *Receiver,
5795 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005796 const CodeGen::CallArgList &CallArgs,
5797 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005798 // ...
5799 // Create and init a super structure; this is a (receiver, class)
5800 // pair we will pass to objc_msgSendSuper.
5801 llvm::Value *ObjCSuper =
5802 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005803
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005804 llvm::Value *ReceiverAsObject =
5805 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5806 CGF.Builder.CreateStore(ReceiverAsObject,
5807 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005808
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005809 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005810 llvm::Value *Target;
5811 if (IsClassMessage) {
5812 if (isCategoryImpl) {
5813 // Message sent to "super' in a class method defined in
5814 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005815 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005816 Target = CGF.Builder.CreateStructGEP(Target, 0);
5817 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005818 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005819 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005820 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005821 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005822
Mike Stumpf5408fe2009-05-16 07:57:57 +00005823 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5824 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005825 const llvm::Type *ClassTy =
5826 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5827 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5828 CGF.Builder.CreateStore(Target,
5829 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005830
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005831 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005832 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5833 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005835 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005836 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005837 ObjCSuper, ObjCTypes.SuperPtrCTy,
5838 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005839}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005840
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005841llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005842 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005843 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005845 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005846 llvm::Constant *Casted =
5847 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5848 ObjCTypes.SelectorPtrTy);
5849 Entry =
5850 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5851 llvm::GlobalValue::InternalLinkage,
5852 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005853 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005854 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005855 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005856
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005857 if (lval)
5858 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005859 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005860}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005861/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005862/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005863///
5864void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005865 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005866 llvm::Value *dst,
5867 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005868 const llvm::Type * SrcTy = src->getType();
5869 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005870 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005871 assert(Size <= 8 && "does not support size > 8");
5872 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5873 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005874 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5875 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005876 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5877 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005878 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5879 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005880 return;
5881}
5882
5883/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5884/// objc_assign_strongCast (id src, id *dst)
5885///
5886void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005887 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005888 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005889 const llvm::Type * SrcTy = src->getType();
5890 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005891 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005892 assert(Size <= 8 && "does not support size > 8");
5893 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005894 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005895 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5896 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005897 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5898 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005899 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005900 src, dst, "weakassign");
5901 return;
5902}
5903
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005904void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905 CodeGen::CodeGenFunction &CGF,
5906 llvm::Value *DestPtr,
5907 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005908 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005909 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5910 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005911 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005912 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005913 return;
5914}
5915
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005916/// EmitObjCWeakRead - Code gen for loading value of a __weak
5917/// object: objc_read_weak (id *src)
5918///
5919llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005920 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005921 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005922 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005923 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5924 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005925 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005926 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005927 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005928 return read_weak;
5929}
5930
5931/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5932/// objc_assign_weak (id src, id *dst)
5933///
5934void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005935 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005936 const llvm::Type * SrcTy = src->getType();
5937 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005938 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005939 assert(Size <= 8 && "does not support size > 8");
5940 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5941 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005942 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5943 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005944 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5945 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005946 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005947 src, dst, "weakassign");
5948 return;
5949}
5950
5951/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5952/// objc_assign_global (id src, id *dst)
5953///
5954void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005955 llvm::Value *src, llvm::Value *dst,
5956 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005957 const llvm::Type * SrcTy = src->getType();
5958 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005959 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005960 assert(Size <= 8 && "does not support size > 8");
5961 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5962 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005963 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5964 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005965 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5966 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005967 if (!threadlocal)
5968 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5969 src, dst, "globalassign");
5970 else
5971 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5972 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005973 return;
5974}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005975
John McCall740e8072010-07-21 00:41:47 +00005976namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005977 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005978 llvm::Value *SyncExitFn;
5979 llvm::Value *SyncArg;
5980 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5981 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5982
5983 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5984 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5985 }
5986 };
5987}
5988
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005989void
John McCallf1549f62010-07-06 01:34:17 +00005990CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5991 const ObjCAtSynchronizedStmt &S) {
5992 // Evaluate the lock operand. This should dominate the cleanup.
5993 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005994
John McCallf1549f62010-07-06 01:34:17 +00005995 // Acquire the lock.
5996 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5997 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5998 ->setDoesNotThrow();
5999
6000 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006001 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6002 ObjCTypes.getSyncExitFn(),
6003 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006004
John McCallf1549f62010-07-06 01:34:17 +00006005 // Emit the body of the statement.
6006 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006007
John McCallf1549f62010-07-06 01:34:17 +00006008 // Pop the lock-release cleanup.
6009 CGF.PopCleanupBlock();
6010}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006011
John McCallf1549f62010-07-06 01:34:17 +00006012namespace {
6013 struct CatchHandler {
6014 const VarDecl *Variable;
6015 const Stmt *Body;
6016 llvm::BasicBlock *Block;
6017 llvm::Value *TypeInfo;
6018 };
John McCall8e3f8612010-07-13 22:12:14 +00006019
John McCall1f0fca52010-07-21 07:22:38 +00006020 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006021 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6022 MightThrow(MightThrow), Fn(Fn) {}
6023 bool MightThrow;
6024 llvm::Value *Fn;
6025
6026 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6027 if (!MightThrow) {
6028 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6029 return;
6030 }
6031
6032 CGF.EmitCallOrInvoke(Fn, 0, 0);
6033 }
6034 };
John McCallf1549f62010-07-06 01:34:17 +00006035}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006036
John McCall5a180392010-07-24 00:37:23 +00006037llvm::Constant *
6038CGObjCNonFragileABIMac::GetEHType(QualType T) {
6039 // There's a particular fixed type info for 'id'.
6040 if (T->isObjCIdType() ||
6041 T->isObjCQualifiedIdType()) {
6042 llvm::Constant *IDEHType =
6043 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6044 if (!IDEHType)
6045 IDEHType =
6046 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6047 false,
6048 llvm::GlobalValue::ExternalLinkage,
6049 0, "OBJC_EHTYPE_id");
6050 return IDEHType;
6051 }
6052
6053 // All other types should be Objective-C interface pointer types.
6054 const ObjCObjectPointerType *PT =
6055 T->getAs<ObjCObjectPointerType>();
6056 assert(PT && "Invalid @catch type.");
6057 const ObjCInterfaceType *IT = PT->getInterfaceType();
6058 assert(IT && "Invalid @catch type.");
6059 return GetInterfaceEHType(IT->getDecl(), false);
6060}
6061
John McCallf1549f62010-07-06 01:34:17 +00006062void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6063 const ObjCAtTryStmt &S) {
6064 // Jump destination for falling out of catch bodies.
6065 CodeGenFunction::JumpDest Cont;
6066 if (S.getNumCatchStmts())
6067 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006068
John McCallf1549f62010-07-06 01:34:17 +00006069 CodeGenFunction::FinallyInfo FinallyInfo;
6070 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6071 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6072 ObjCTypes.getObjCBeginCatchFn(),
6073 ObjCTypes.getObjCEndCatchFn(),
6074 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006075
John McCallf1549f62010-07-06 01:34:17 +00006076 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006077
John McCallf1549f62010-07-06 01:34:17 +00006078 // Enter the catch, if there is one.
6079 if (S.getNumCatchStmts()) {
6080 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6081 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006082 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006083
John McCallf1549f62010-07-06 01:34:17 +00006084 Handlers.push_back(CatchHandler());
6085 CatchHandler &Handler = Handlers.back();
6086 Handler.Variable = CatchDecl;
6087 Handler.Body = CatchStmt->getCatchBody();
6088 Handler.Block = CGF.createBasicBlock("catch");
6089
6090 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006091 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006092 Handler.TypeInfo = 0; // catch-all
6093 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006094 break;
6095 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006096
John McCall5a180392010-07-24 00:37:23 +00006097 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006098 }
John McCallf1549f62010-07-06 01:34:17 +00006099
6100 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6101 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6102 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006103 }
John McCallf1549f62010-07-06 01:34:17 +00006104
6105 // Emit the try body.
6106 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006107
John McCallf1549f62010-07-06 01:34:17 +00006108 // Leave the try.
6109 if (S.getNumCatchStmts())
6110 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006111
John McCallf1549f62010-07-06 01:34:17 +00006112 // Remember where we were.
6113 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006114
John McCallf1549f62010-07-06 01:34:17 +00006115 // Emit the handlers.
6116 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6117 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006118
John McCallf1549f62010-07-06 01:34:17 +00006119 CGF.EmitBlock(Handler.Block);
6120 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006121
John McCallf1549f62010-07-06 01:34:17 +00006122 // Enter the catch.
6123 llvm::CallInst *Exn =
6124 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6125 "exn.adjusted");
6126 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006127
John McCallf1549f62010-07-06 01:34:17 +00006128 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006129 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006130 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6131 EndCatchMightThrow,
6132 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006133
John McCallf1549f62010-07-06 01:34:17 +00006134 // Bind the catch parameter if it exists.
6135 if (const VarDecl *CatchParam = Handler.Variable) {
6136 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6137 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006138
John McCallf1549f62010-07-06 01:34:17 +00006139 CGF.EmitLocalBlockVarDecl(*CatchParam);
6140 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006141 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006142
John McCallf1549f62010-07-06 01:34:17 +00006143 CGF.ObjCEHValueStack.push_back(Exn);
6144 CGF.EmitStmt(Handler.Body);
6145 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006146
John McCallf1549f62010-07-06 01:34:17 +00006147 // Leave the earlier cleanup.
6148 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006149
John McCallf1549f62010-07-06 01:34:17 +00006150 CGF.EmitBranchThroughCleanup(Cont);
6151 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006152
John McCallf1549f62010-07-06 01:34:17 +00006153 // Go back to the try-statement fallthrough.
6154 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006155
John McCallf1549f62010-07-06 01:34:17 +00006156 // Pop out of the normal cleanup on the finally.
6157 if (S.getFinallyStmt())
6158 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006159
John McCallff8e1152010-07-23 21:56:41 +00006160 if (Cont.isValid())
6161 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006162}
6163
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006164/// EmitThrowStmt - Generate code for a throw statement.
6165void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6166 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006167 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006168 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006169 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006170 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006171 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006172 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006173 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006174 "Unexpected rethrow outside @catch block.");
6175 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006176 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006177 }
6178
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006179 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006180 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6181 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6182 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006183 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006184 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006185 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006186 } else {
6187 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6188 ->setDoesNotReturn();
6189 CGF.Builder.CreateUnreachable();
6190 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006191
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006192 // Clear the insertion point to indicate we are in unreachable code.
6193 CGF.Builder.ClearInsertionPoint();
6194}
Daniel Dunbare588b992009-03-01 04:46:24 +00006195
John McCall5a180392010-07-24 00:37:23 +00006196llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006197CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006198 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006199 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006200
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006201 // If we don't need a definition, return the entry if found or check
6202 // if we use an external reference.
6203 if (!ForDefinition) {
6204 if (Entry)
6205 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006206
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006207 // If this type (or a super class) has the __objc_exception__
6208 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006209 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006210 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006211 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006212 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006213 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006214 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006215 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006216 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006217
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006218 // Otherwise we need to either make a new entry or fill in the
6219 // initializer.
6220 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006221 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006222 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006223 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006224 CGM.getModule().getGlobalVariable(VTableName);
6225 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006226 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6227 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006228 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006229 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006230
Chris Lattner77b89b82010-06-27 07:15:29 +00006231 llvm::Value *VTableIdx =
6232 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006233
6234 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006235 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006236 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006237 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006238 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006239 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006240
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006241 if (Entry) {
6242 Entry->setInitializer(Init);
6243 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006244 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006245 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006246 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006247 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006248 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006249 }
6250
Daniel Dunbar04d40782009-04-14 06:00:08 +00006251 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006252 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006253 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6254 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006255
6256 if (ForDefinition) {
6257 Entry->setSection("__DATA,__objc_const");
6258 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6259 } else {
6260 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6261 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006262
6263 return Entry;
6264}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006265
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006266/* *** */
6267
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006268CodeGen::CGObjCRuntime *
6269CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006270 return new CGObjCMac(CGM);
6271}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006272
6273CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006274CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006275 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006276}