blob: f5befceb0fc800c0fb89ef88ac691b9878195725 [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 McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-04-22 07:32:20 +000045// Common CGObjCRuntime functions, these don't belong here, but they
46// don't belong in CGObjCRuntime either so we will live with it for
47// now.
48
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000049static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
50 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000051 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000052 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000053 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000054
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000055 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
56 // in here; it should never be necessary because that should be the lexical
57 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000058
Daniel Dunbar532d4da2009-05-03 13:15:50 +000059 // If we know have an implementation (and the ivar is in it) then
60 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000061 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000062 if (ID && ID->getClassInterface() == Container)
63 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
64 else
65 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000066
67 // Compute field index.
68 //
69 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
70 // implemented. This should be fixed to get the information from the layout
71 // directly.
72 unsigned Index = 0;
73 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
74 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
75 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
76 if (Ivar == Ivars[k])
77 break;
78 ++Index;
79 }
80 assert(Index != Ivars.size() && "Ivar is not inside container!");
David Chisnalle0d98762010-11-03 16:12:44 +000081 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbare83be122010-04-02 21:14:02 +000082
Daniel Dunbar532d4da2009-05-03 13:15:50 +000083 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000084}
85
86uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
87 const ObjCInterfaceDecl *OID,
88 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000089 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
90}
91
92uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
93 const ObjCImplementationDecl *OID,
94 const ObjCIvarDecl *Ivar) {
95 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000096}
97
98LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
99 const ObjCInterfaceDecl *OID,
100 llvm::Value *BaseValue,
101 const ObjCIvarDecl *Ivar,
102 unsigned CVRQualifiers,
103 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000105 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000106 QualType IvarTy = Ivar->getType();
107 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000109 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000110 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000111
Daniel Dunbar6d5eb762010-08-21 03:44:13 +0000112 if (!Ivar->isBitField()) {
113 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
114 LV.getQuals().addCVRQualifiers(CVRQualifiers);
115 return LV;
116 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000117
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000118 // We need to compute an access strategy for this bit-field. We are given the
119 // offset to the first byte in the bit-field, the sub-byte offset is taken
120 // from the original layout. We reuse the normal bit-field access strategy by
121 // treating this as an access to a struct where the bit-field is in byte 0,
122 // and adjust the containing type size as appropriate.
123 //
124 // FIXME: Note that currently we make a very conservative estimate of the
125 // alignment of the bit-field, because (a) it is not clear what guarantees the
126 // runtime makes us, and (b) we don't have a way to specify that the struct is
127 // at an alignment plus offset.
128 //
129 // Note, there is a subtle invariant here: we can only call this routine on
130 // non-synthesized ivars but we may be called for synthesized ivars. However,
131 // a synthesized ivar can never be a bit-field, so this is safe.
132 const ASTRecordLayout &RL =
133 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000134 uint64_t TypeSizeInBits = CGF.CGM.getContext().toBits(RL.getSize());
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000135 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
136 uint64_t BitOffset = FieldBitOffset % 8;
137 uint64_t ContainingTypeAlign = 8;
138 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbar56229f52010-04-05 16:20:33 +0000139 uint64_t BitFieldSize =
140 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000141
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000142 // Allocate a new CGBitFieldInfo object to describe this access.
143 //
144 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
145 // layout object. However, this is blocked on other cleanups to the
146 // Objective-C code, so for now we just live with allocating a bunch of these
147 // objects.
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000148 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
149 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
150 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000151
Daniel Dunbar76695ca2010-08-21 04:05:54 +0000152 return LValue::MakeBitfield(V, *Info,
153 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000154}
155
156///
157
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000158namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000159
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000161
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162// FIXME: We should find a nicer way to make the labels for metadata, string
163// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000164
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000165class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000166protected:
167 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000168
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000169private:
170 llvm::Constant *getMessageSendFn() const {
171 // id objc_msgSend (id, SEL, ...)
172 std::vector<const llvm::Type*> Params;
173 Params.push_back(ObjectPtrTy);
174 Params.push_back(SelectorPtrTy);
175 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000176 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
177 Params, true),
178 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000179 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000180
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000181 llvm::Constant *getMessageSendStretFn() const {
182 // id objc_msgSend_stret (id, SEL, ...)
183 std::vector<const llvm::Type*> Params;
184 Params.push_back(ObjectPtrTy);
185 Params.push_back(SelectorPtrTy);
186 return
Owen Anderson0032b272009-08-13 21:57:51 +0000187 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000188 Params, true),
189 "objc_msgSend_stret");
190
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000191 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000192
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000193 llvm::Constant *getMessageSendFpretFn() const {
194 // FIXME: This should be long double on x86_64?
195 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
196 std::vector<const llvm::Type*> Params;
197 Params.push_back(ObjectPtrTy);
198 Params.push_back(SelectorPtrTy);
199 return
Owen Anderson0032b272009-08-13 21:57:51 +0000200 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
201 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000202 Params,
203 true),
204 "objc_msgSend_fpret");
205
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000206 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000207
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000208 llvm::Constant *getMessageSendSuperFn() const {
209 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
210 const char *SuperName = "objc_msgSendSuper";
211 std::vector<const llvm::Type*> Params;
212 Params.push_back(SuperPtrTy);
213 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000214 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000215 Params, true),
216 SuperName);
217 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000218
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000219 llvm::Constant *getMessageSendSuperFn2() const {
220 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
221 const char *SuperName = "objc_msgSendSuper2";
222 std::vector<const llvm::Type*> Params;
223 Params.push_back(SuperPtrTy);
224 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000225 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000226 Params, true),
227 SuperName);
228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000229
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000230 llvm::Constant *getMessageSendSuperStretFn() const {
231 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
232 // SEL op, ...)
233 std::vector<const llvm::Type*> Params;
234 Params.push_back(Int8PtrTy);
235 Params.push_back(SuperPtrTy);
236 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000237 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000238 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000239 Params, true),
240 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000241 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000242
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000243 llvm::Constant *getMessageSendSuperStretFn2() const {
244 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
245 // SEL op, ...)
246 std::vector<const llvm::Type*> Params;
247 Params.push_back(Int8PtrTy);
248 Params.push_back(SuperPtrTy);
249 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000250 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000251 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000252 Params, true),
253 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000254 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000255
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000256 llvm::Constant *getMessageSendSuperFpretFn() const {
257 // There is no objc_msgSendSuper_fpret? How can that work?
258 return getMessageSendSuperFn();
259 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000260
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000261 llvm::Constant *getMessageSendSuperFpretFn2() const {
262 // There is no objc_msgSendSuper_fpret? How can that work?
263 return getMessageSendSuperFn2();
264 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000265
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000266protected:
267 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000268
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000269public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000270 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000271 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000273 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
274 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000275
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000276 /// PtrObjectPtrTy - LLVM type for id *
277 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000278
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000279 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000280 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000281 /// ProtocolPtrTy - LLVM type for external protocol handles
282 /// (typeof(Protocol))
283 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000284
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000285 // SuperCTy - clang type for struct objc_super.
286 QualType SuperCTy;
287 // SuperPtrCTy - clang type for struct objc_super *.
288 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000289
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000290 /// SuperTy - LLVM type for struct objc_super.
291 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000292 /// SuperPtrTy - LLVM type for struct objc_super *.
293 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000294
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000295 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
296 /// in GCC parlance).
297 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000299 /// PropertyListTy - LLVM type for struct objc_property_list
300 /// (_prop_list_t in GCC parlance).
301 const llvm::StructType *PropertyListTy;
302 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
303 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000304
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000305 // MethodTy - LLVM type for struct objc_method.
306 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000307
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000308 /// CacheTy - LLVM type for struct objc_cache.
309 const llvm::Type *CacheTy;
310 /// CachePtrTy - LLVM type for struct objc_cache *.
311 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000312
Chris Lattner72db6c32009-04-22 02:44:54 +0000313 llvm::Constant *getGetPropertyFn() {
314 CodeGen::CodeGenTypes &Types = CGM.getTypes();
315 ASTContext &Ctx = CGM.getContext();
316 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000317 llvm::SmallVector<CanQualType,4> Params;
318 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
319 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000320 Params.push_back(IdType);
321 Params.push_back(SelType);
322 Params.push_back(Ctx.LongTy);
323 Params.push_back(Ctx.BoolTy);
324 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000325 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000326 FunctionType::ExtInfo()),
327 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000328 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
329 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000330
Chris Lattner72db6c32009-04-22 02:44:54 +0000331 llvm::Constant *getSetPropertyFn() {
332 CodeGen::CodeGenTypes &Types = CGM.getTypes();
333 ASTContext &Ctx = CGM.getContext();
334 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000335 llvm::SmallVector<CanQualType,6> Params;
336 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
337 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000338 Params.push_back(IdType);
339 Params.push_back(SelType);
340 Params.push_back(Ctx.LongTy);
341 Params.push_back(IdType);
342 Params.push_back(Ctx.BoolTy);
343 Params.push_back(Ctx.BoolTy);
344 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000345 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000346 FunctionType::ExtInfo()),
347 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000348 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
349 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000350
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000351
352 llvm::Constant *getCopyStructFn() {
353 CodeGen::CodeGenTypes &Types = CGM.getTypes();
354 ASTContext &Ctx = CGM.getContext();
355 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
356 llvm::SmallVector<CanQualType,5> Params;
357 Params.push_back(Ctx.VoidPtrTy);
358 Params.push_back(Ctx.VoidPtrTy);
359 Params.push_back(Ctx.LongTy);
360 Params.push_back(Ctx.BoolTy);
361 Params.push_back(Ctx.BoolTy);
362 const llvm::FunctionType *FTy =
363 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
364 FunctionType::ExtInfo()),
365 false);
366 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
367 }
368
Chris Lattner72db6c32009-04-22 02:44:54 +0000369 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000370 CodeGen::CodeGenTypes &Types = CGM.getTypes();
371 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000372 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000373 llvm::SmallVector<CanQualType,1> Params;
374 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000375 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000376 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000377 FunctionType::ExtInfo()),
378 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000379 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000382 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000383 llvm::Constant *getGcReadWeakFn() {
384 // id objc_read_weak (id *)
385 std::vector<const llvm::Type*> Args;
386 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000387 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000388 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000390 }
391
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000392 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000393 llvm::Constant *getGcAssignWeakFn() {
394 // id objc_assign_weak (id, id *)
395 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
396 Args.push_back(ObjectPtrTy->getPointerTo());
397 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000398 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000402 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000403 llvm::Constant *getGcAssignGlobalFn() {
404 // id objc_assign_global(id, id *)
405 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
406 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000407 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000408 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000411
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000412 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
413 llvm::Constant *getGcAssignThreadLocalFn() {
414 // id objc_assign_threadlocal(id src, id * dest)
415 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
416 Args.push_back(ObjectPtrTy->getPointerTo());
417 llvm::FunctionType *FTy =
418 llvm::FunctionType::get(ObjectPtrTy, Args, false);
419 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
420 }
421
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000422 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000424 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
426 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000427 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000428 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000429 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000430 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
431 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000432
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000433 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
434 llvm::Constant *GcMemmoveCollectableFn() {
435 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
436 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
437 Args.push_back(Int8PtrTy);
438 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000439 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000440 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
441 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000442
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000443 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000445 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000446 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
447 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000448 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000449 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
451 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000452
453 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000454 llvm::Constant *getExceptionThrowFn() {
455 // void objc_exception_throw(id)
456 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
457 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000458 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000459 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
460 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000461
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000462 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
463 llvm::Constant *getExceptionRethrowFn() {
464 // void objc_exception_rethrow(void)
465 std::vector<const llvm::Type*> Args;
466 llvm::FunctionType *FTy =
John McCall7ec404c2010-10-16 08:21:07 +0000467 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000468 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
469 }
470
Daniel Dunbar1c566672009-02-24 01:43:46 +0000471 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000472 llvm::Constant *getSyncEnterFn() {
473 // void objc_sync_enter (id)
474 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
475 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000476 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000477 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
478 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000479
Daniel Dunbar1c566672009-02-24 01:43:46 +0000480 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000481 llvm::Constant *getSyncExitFn() {
482 // void objc_sync_exit (id)
483 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
484 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000485 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000486 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
487 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000488
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000489 llvm::Constant *getSendFn(bool IsSuper) const {
490 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
491 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000492
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000493 llvm::Constant *getSendFn2(bool IsSuper) const {
494 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
495 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000496
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000497 llvm::Constant *getSendStretFn(bool IsSuper) const {
498 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
499 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000500
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000501 llvm::Constant *getSendStretFn2(bool IsSuper) const {
502 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
503 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000504
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000505 llvm::Constant *getSendFpretFn(bool IsSuper) const {
506 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
507 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000508
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000509 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
510 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
511 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000512
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000513 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
514 ~ObjCCommonTypesHelper(){}
515};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000516
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000517/// ObjCTypesHelper - Helper class that encapsulates lazy
518/// construction of varies types used during ObjC generation.
519class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000520public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000521 /// SymtabTy - LLVM type for struct objc_symtab.
522 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
524 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000525 /// ModuleTy - LLVM type for struct objc_module.
526 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000527
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000528 /// ProtocolTy - LLVM type for struct objc_protocol.
529 const llvm::StructType *ProtocolTy;
530 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
531 const llvm::Type *ProtocolPtrTy;
532 /// ProtocolExtensionTy - LLVM type for struct
533 /// objc_protocol_extension.
534 const llvm::StructType *ProtocolExtensionTy;
535 /// ProtocolExtensionTy - LLVM type for struct
536 /// objc_protocol_extension *.
537 const llvm::Type *ProtocolExtensionPtrTy;
538 /// MethodDescriptionTy - LLVM type for struct
539 /// objc_method_description.
540 const llvm::StructType *MethodDescriptionTy;
541 /// MethodDescriptionListTy - LLVM type for struct
542 /// objc_method_description_list.
543 const llvm::StructType *MethodDescriptionListTy;
544 /// MethodDescriptionListPtrTy - LLVM type for struct
545 /// objc_method_description_list *.
546 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000547 /// ProtocolListTy - LLVM type for struct objc_property_list.
548 const llvm::Type *ProtocolListTy;
549 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
550 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000551 /// CategoryTy - LLVM type for struct objc_category.
552 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000553 /// ClassTy - LLVM type for struct objc_class.
554 const llvm::StructType *ClassTy;
555 /// ClassPtrTy - LLVM type for struct objc_class *.
556 const llvm::Type *ClassPtrTy;
557 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
558 const llvm::StructType *ClassExtensionTy;
559 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
560 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000561 // IvarTy - LLVM type for struct objc_ivar.
562 const llvm::StructType *IvarTy;
563 /// IvarListTy - LLVM type for struct objc_ivar_list.
564 const llvm::Type *IvarListTy;
565 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
566 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000567 /// MethodListTy - LLVM type for struct objc_method_list.
568 const llvm::Type *MethodListTy;
569 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
570 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000571
Anders Carlsson124526b2008-09-09 10:10:21 +0000572 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
573 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574
Anders Carlsson124526b2008-09-09 10:10:21 +0000575 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000576 llvm::Constant *getExceptionTryEnterFn() {
577 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000578 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000579 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000580 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581 Params, false),
582 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000583 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000584
585 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000586 llvm::Constant *getExceptionTryExitFn() {
587 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000588 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000589 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000590 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000591 Params, false),
592 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000593 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000594
595 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000596 llvm::Constant *getExceptionExtractFn() {
597 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000598 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
599 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000600 Params, false),
601 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Chris Lattner34b02a12009-04-22 02:26:14 +0000603 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604
Anders Carlsson124526b2008-09-09 10:10:21 +0000605 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000606 llvm::Constant *getExceptionMatchFn() {
607 std::vector<const llvm::Type*> Params;
608 Params.push_back(ClassPtrTy);
609 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000611 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000612 Params, false),
613 "objc_exception_match");
614
Chris Lattner34b02a12009-04-22 02:26:14 +0000615 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000616
Anders Carlsson124526b2008-09-09 10:10:21 +0000617 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000618 llvm::Constant *getSetJmpFn() {
619 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000620 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000621 return
Owen Anderson0032b272009-08-13 21:57:51 +0000622 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000623 Params, false),
624 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Chris Lattner34b02a12009-04-22 02:26:14 +0000626 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000628public:
629 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000630 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000631};
632
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000633/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000634/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000635class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000636public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // MethodListnfABITy - LLVM for struct _method_list_t
639 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000641 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
642 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000643
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000644 // ProtocolnfABITy = LLVM for struct _protocol_t
645 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Daniel Dunbar948e2582009-02-15 07:36:20 +0000647 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
648 const llvm::Type *ProtocolnfABIPtrTy;
649
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000650 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
651 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000652
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000653 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
654 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000656 // ClassnfABITy - LLVM for struct _class_t
657 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000659 // ClassnfABIPtrTy - LLVM for struct _class_t*
660 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000662 // IvarnfABITy - LLVM for struct _ivar_t
663 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000665 // IvarListnfABITy - LLVM for struct _ivar_list_t
666 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000668 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
669 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000671 // ClassRonfABITy - LLVM for struct _class_ro_t
672 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000673
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000674 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
675 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000676
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000677 // CategorynfABITy - LLVM for struct _category_t
678 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000680 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000681
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000682 // MessageRefTy - LLVM for:
683 // struct _message_ref_t {
684 // IMP messenger;
685 // SEL name;
686 // };
687 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000688 // MessageRefCTy - clang type for struct _message_ref_t
689 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000690
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000691 // MessageRefPtrTy - LLVM for struct _message_ref_t*
692 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000693 // MessageRefCPtrTy - clang type for struct _message_ref_t*
694 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695
Fariborz Jahanianef163782009-02-05 01:13:09 +0000696 // MessengerTy - Type of the messenger (shown as IMP above)
697 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000698
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000699 // SuperMessageRefTy - LLVM for:
700 // struct _super_message_ref_t {
701 // SUPER_IMP messenger;
702 // SEL name;
703 // };
704 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000705
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000706 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
707 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000708
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 llvm::Constant *getMessageSendFixupFn() {
710 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
711 std::vector<const llvm::Type*> Params;
712 Params.push_back(ObjectPtrTy);
713 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000714 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000715 Params, true),
716 "objc_msgSend_fixup");
717 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000718
Chris Lattner1c02f862009-04-22 02:53:24 +0000719 llvm::Constant *getMessageSendFpretFixupFn() {
720 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
721 std::vector<const llvm::Type*> Params;
722 Params.push_back(ObjectPtrTy);
723 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000724 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000725 Params, true),
726 "objc_msgSend_fpret_fixup");
727 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000728
Chris Lattner1c02f862009-04-22 02:53:24 +0000729 llvm::Constant *getMessageSendStretFixupFn() {
730 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
731 std::vector<const llvm::Type*> Params;
732 Params.push_back(ObjectPtrTy);
733 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000734 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000735 Params, true),
736 "objc_msgSend_stret_fixup");
737 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000738
Chris Lattner1c02f862009-04-22 02:53:24 +0000739 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000741 // struct _super_message_ref_t*, ...)
742 std::vector<const llvm::Type*> Params;
743 Params.push_back(SuperPtrTy);
744 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000745 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000746 Params, true),
747 "objc_msgSendSuper2_fixup");
748 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Chris Lattner1c02f862009-04-22 02:53:24 +0000750 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000751 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000752 // struct _super_message_ref_t*, ...)
753 std::vector<const llvm::Type*> Params;
754 Params.push_back(SuperPtrTy);
755 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000756 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000757 Params, true),
758 "objc_msgSendSuper2_stret_fixup");
759 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000760
Chris Lattner8a569112009-04-22 02:15:23 +0000761 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000762 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000763 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000764 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000765
Chris Lattner8a569112009-04-22 02:15:23 +0000766 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000767
Chris Lattner8a569112009-04-22 02:15:23 +0000768 llvm::Constant *getObjCBeginCatchFn() {
769 std::vector<const llvm::Type*> Params;
770 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000771 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000772 Params, false),
773 "objc_begin_catch");
774 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000775
776 const llvm::StructType *EHTypeTy;
777 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000778
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000779 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
780 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000783class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000784public:
785 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000786 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000787 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000788 unsigned ivar_bytepos;
789 unsigned ivar_size;
790 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000792
793 // Allow sorting based on byte pos.
794 bool operator<(const GC_IVAR &b) const {
795 return ivar_bytepos < b.ivar_bytepos;
796 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000797 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000798
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000799 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000800 public:
801 unsigned skip;
802 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000803 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000804 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000805 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000806
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000807protected:
808 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000809 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000810 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000811 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000812
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000813 // gc ivar layout bitmap calculation helper caches.
814 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
815 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000816
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000817 /// LazySymbols - Symbols to generate a lazy reference for. See
818 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000819 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000820
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000821 /// DefinedSymbols - External symbols which are defined by this
822 /// module. The symbols in this list and LazySymbols are used to add
823 /// special linker symbols which ensure that Objective-C modules are
824 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000825 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000826
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000827 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000828 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000829
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000830 /// MethodVarNames - uniqued method variable names.
831 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000833 /// DefinedCategoryNames - list of category names in form Class_Category.
834 llvm::SetVector<std::string> DefinedCategoryNames;
835
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000836 /// MethodVarTypes - uniqued method type signatures. We have to use
837 /// a StringMap here because have no other unique reference.
838 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000839
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000840 /// MethodDefinitions - map of methods which have been defined in
841 /// this translation unit.
842 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000843
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000844 /// PropertyNames - uniqued method variable names.
845 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000847 /// ClassReferences - uniqued class references.
848 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000850 /// SelectorReferences - uniqued selector references.
851 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000853 /// Protocols - Protocols for which an objc_protocol structure has
854 /// been emitted. Forward declarations are handled by creating an
855 /// empty structure whose initializer is filled in when/if defined.
856 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000857
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000858 /// DefinedProtocols - Protocols which have actually been
859 /// defined. We should not need this, see FIXME in GenerateProtocol.
860 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000861
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000862 /// DefinedClasses - List of defined classes.
863 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000864
865 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
866 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000867
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000868 /// DefinedCategories - List of defined categories.
869 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000870
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000871 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
872 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000873
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000874 /// GetNameForMethod - Return a name for the given method.
875 /// \param[out] NameOut - The return value.
876 void GetNameForMethod(const ObjCMethodDecl *OMD,
877 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000878 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000879
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000880 /// GetMethodVarName - Return a unique constant for the given
881 /// selector's name. The return value has type char *.
882 llvm::Constant *GetMethodVarName(Selector Sel);
883 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000885 /// GetMethodVarType - Return a unique constant for the given
886 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000888 // FIXME: This is a horrible name.
889 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000890 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000891
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000892 /// GetPropertyName - Return a unique constant for the given
893 /// name. The return value has type char *.
894 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000895
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000896 // FIXME: This can be dropped once string functions are unified.
897 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
898 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000899
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000900 /// GetClassName - Return a unique constant for the given selector's
901 /// name. The return value has type char *.
902 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000903
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000904 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
905
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000906 /// BuildIvarLayout - Builds ivar layout bitmap for the class
907 /// implementation for the __strong or __weak case.
908 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000909 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
910 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000911
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000912 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000914 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000915 unsigned int BytePos, bool ForStrongLayout,
916 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000917 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000918 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000919 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000920 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000921 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000922 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000923
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000924 /// GetIvarLayoutName - Returns a unique constant for the given
925 /// ivar layout bitmap.
926 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
927 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000928
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000929 /// EmitPropertyList - Emit the given property list. The return
930 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000931 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000932 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000933 const ObjCContainerDecl *OCD,
934 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000935
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000936 /// PushProtocolProperties - Push protocol's property on the input stack.
937 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
938 std::vector<llvm::Constant*> &Properties,
939 const Decl *Container,
940 const ObjCProtocolDecl *PROTO,
941 const ObjCCommonTypesHelper &ObjCTypes);
942
Fariborz Jahanianda320092009-01-29 19:24:30 +0000943 /// GetProtocolRef - Return a reference to the internal protocol
944 /// description, creating an empty one if it has not been
945 /// defined. The return value has type ProtocolPtrTy.
946 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000947
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000948 /// CreateMetadataVar - Create a global variable with internal
949 /// linkage for use by the Objective-C runtime.
950 ///
951 /// This is a convenience wrapper which not only creates the
952 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000953 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000954 ///
955 /// \param Name - The variable name.
956 /// \param Init - The variable initializer; this is also used to
957 /// define the type of the variable.
958 /// \param Section - The section the variable should go into, or 0.
959 /// \param Align - The alignment for the variable, or 0.
960 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000961 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000962 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000963 llvm::Constant *Init,
964 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000965 unsigned Align,
966 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000967
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000968 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000969 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000970 QualType ResultType,
971 llvm::Value *Sel,
972 llvm::Value *Arg0,
973 QualType Arg0Ty,
974 bool IsSuper,
975 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000976 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000977 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000978
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000979 /// EmitImageInfo - Emit the image info marker used to encode some module
980 /// level information.
981 void EmitImageInfo();
982
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000983public:
Owen Anderson69243822009-07-13 04:10:07 +0000984 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000985 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000986
David Chisnall0d13f6f2010-01-23 02:40:42 +0000987 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000988
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000989 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
990 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000991
Fariborz Jahanianda320092009-01-29 19:24:30 +0000992 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000993
Fariborz Jahanianda320092009-01-29 19:24:30 +0000994 /// GetOrEmitProtocol - Get the protocol object for the given
995 /// declaration, emitting it if necessary. The return value has type
996 /// ProtocolPtrTy.
997 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000998
Fariborz Jahanianda320092009-01-29 19:24:30 +0000999 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1000 /// object for the given declaration, emitting it if needed. These
1001 /// forward references will be filled in with empty bodies if no
1002 /// definition is seen. The return value has type ProtocolPtrTy.
1003 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001004 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1005 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001006
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001007};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001008
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001009class CGObjCMac : public CGObjCCommonMac {
1010private:
1011 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001012
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001013 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001014 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001015 void EmitModuleInfo();
1016
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001017 /// EmitModuleSymols - Emit module symbols, the list of defined
1018 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001019 llvm::Constant *EmitModuleSymbols();
1020
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001021 /// FinishModule - Write out global data structures at the end of
1022 /// processing a translation unit.
1023 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001024
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001025 /// EmitClassExtension - Generate the class extension structure used
1026 /// to store the weak ivar layout and properties. The return value
1027 /// has type ClassExtensionPtrTy.
1028 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1029
1030 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1031 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001032 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001033 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001034
1035 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1036 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001037
1038 /// EmitIvarList - Emit the ivar list for the given
1039 /// implementation. If ForClass is true the list of class ivars
1040 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1041 /// interface ivars will be emitted. The return value has type
1042 /// IvarListPtrTy.
1043 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001044 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001045
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001046 /// EmitMetaClass - Emit a forward reference to the class structure
1047 /// for the metaclass of the given interface. The return value has
1048 /// type ClassPtrTy.
1049 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1050
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001051 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001052 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001053 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1054 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001055 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001056
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001057 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001058
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001059 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001060
1061 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001062 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001063 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001064 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001065 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001066
1067 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001068 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001069 /// - TypeName: The name for the type containing the methods.
1070 /// - IsProtocol: True iff these methods are for a protocol.
1071 /// - ClassMethds: True iff these are class methods.
1072 /// - Required: When true, only "required" methods are
1073 /// listed. Similarly, when false only "optional" methods are
1074 /// listed. For classes this should always be true.
1075 /// - begin, end: The method list to output.
1076 ///
1077 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001078 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001079 const char *Section,
1080 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001081
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001082 /// GetOrEmitProtocol - Get the protocol object for the given
1083 /// declaration, emitting it if necessary. The return value has type
1084 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001085 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001086
1087 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1088 /// object for the given declaration, emitting it if needed. These
1089 /// forward references will be filled in with empty bodies if no
1090 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001091 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001092
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001093 /// EmitProtocolExtension - Generate the protocol extension
1094 /// structure used to store optional instance and class methods, and
1095 /// protocol properties. The return value has type
1096 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001097 llvm::Constant *
1098 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1099 const ConstantVector &OptInstanceMethods,
1100 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001101
1102 /// EmitProtocolList - Generate the list of referenced
1103 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001104 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001105 ObjCProtocolDecl::protocol_iterator begin,
1106 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001108 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1109 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001110 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1111 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
1113public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001114 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001115
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001116 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001117
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001118 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001119 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001120 QualType ResultType,
1121 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001122 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001123 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001124 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001125 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001126
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001127 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001128 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001129 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001130 QualType ResultType,
1131 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001132 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001133 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001134 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001135 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001136 const CallArgList &CallArgs,
1137 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001138
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001139 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001140 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001141
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001142 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1143 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001144
1145 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1146 /// untyped one.
1147 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1148 const ObjCMethodDecl *Method);
1149
John McCall5a180392010-07-24 00:37:23 +00001150 virtual llvm::Constant *GetEHType(QualType T);
1151
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001152 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001153
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001154 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001155
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001156 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001157 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001158
Chris Lattner74391b42009-03-22 21:03:39 +00001159 virtual llvm::Constant *GetPropertyGetFunction();
1160 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001161 virtual llvm::Constant *GetGetStructFunction();
1162 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001163 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001164
John McCallf1549f62010-07-06 01:34:17 +00001165 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1166 const ObjCAtTryStmt &S);
1167 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1168 const ObjCAtSynchronizedStmt &S);
1169 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001170 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1171 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001172 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001173 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001174 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001175 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001176 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001177 llvm::Value *src, llvm::Value *dest,
1178 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001179 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001180 llvm::Value *src, llvm::Value *dest,
1181 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001182 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1183 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001184 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1185 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001186 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001187
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001188 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1189 QualType ObjectTy,
1190 llvm::Value *BaseValue,
1191 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001192 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001193 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001194 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001195 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001196};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001198class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001199private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001200 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001201 llvm::GlobalVariable* ObjCEmptyCacheVar;
1202 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001203
Daniel Dunbar11394522009-04-18 08:51:00 +00001204 /// SuperClassReferences - uniqued super class references.
1205 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001206
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001207 /// MetaClassReferences - uniqued meta class references.
1208 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001209
1210 /// EHTypeReferences - uniqued class ehtype references.
1211 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001212
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001213 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001214 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001215 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001216
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001217 /// DefinedMetaClasses - List of defined meta-classes.
1218 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1219
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001220 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001221 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001222 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001223
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001224 /// FinishNonFragileABIModule - Write out global data structures at the end of
1225 /// processing a translation unit.
1226 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001227
Daniel Dunbar463b8762009-05-15 21:48:48 +00001228 /// AddModuleClassList - Add the given list of class pointers to the
1229 /// module with the provided symbol and section names.
1230 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1231 const char *SymbolName,
1232 const char *SectionName);
1233
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1235 unsigned InstanceStart,
1236 unsigned InstanceSize,
1237 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001238 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001239 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001240 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001241 llvm::Constant *ClassRoGV,
1242 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001243
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001244 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001246 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001247
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001248 /// EmitMethodList - Emit the method list for the given
1249 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001250 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001251 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001252 const ConstantVector &Methods);
1253 /// EmitIvarList - Emit the ivar list for the given
1254 /// implementation. If ForClass is true the list of class ivars
1255 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1256 /// interface ivars will be emitted. The return value has type
1257 /// IvarListnfABIPtrTy.
1258 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001259
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001260 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001261 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001262 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001263
Fariborz Jahanianda320092009-01-29 19:24:30 +00001264 /// GetOrEmitProtocol - Get the protocol object for the given
1265 /// declaration, emitting it if necessary. The return value has type
1266 /// ProtocolPtrTy.
1267 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Fariborz Jahanianda320092009-01-29 19:24:30 +00001269 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1270 /// object for the given declaration, emitting it if needed. These
1271 /// forward references will be filled in with empty bodies if no
1272 /// definition is seen. The return value has type ProtocolPtrTy.
1273 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274
Fariborz Jahanianda320092009-01-29 19:24:30 +00001275 /// EmitProtocolList - Generate the list of referenced
1276 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001277 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001278 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001279 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001280
Fariborz Jahanian46551122009-02-04 00:22:57 +00001281 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001282 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001283 QualType ResultType,
1284 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001285 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001286 QualType Arg0Ty,
1287 bool IsSuper,
1288 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001289
1290 /// GetClassGlobal - Return the global variable for the Objective-C
1291 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001292 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001293
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001294 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001295 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001296 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001297 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001298
Daniel Dunbar11394522009-04-18 08:51:00 +00001299 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1300 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1302 const ObjCInterfaceDecl *ID);
1303
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001304 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1305 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001306 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001307 const ObjCInterfaceDecl *ID);
1308
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001309 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1310 /// the given ivar.
1311 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001312 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001313 const ObjCInterfaceDecl *ID,
1314 const ObjCIvarDecl *Ivar);
1315
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001316 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1317 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001318 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1319 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001320
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001321 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001322 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001323 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001324 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001325
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001326 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001327 return "OBJC_METACLASS_$_";
1328 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001329
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001330 const char *getClassSymbolPrefix() const {
1331 return "OBJC_CLASS_$_";
1332 }
1333
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001334 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001335 uint32_t &InstanceStart,
1336 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001337
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001338 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001339 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001340 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1341 return CGM.getContext().Selectors.getSelector(0, &II);
1342 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001343
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001344 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001345 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1346 return CGM.getContext().Selectors.getSelector(1, &II);
1347 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001348
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001349 /// ImplementationIsNonLazy - Check whether the given category or
1350 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001351 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001352
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001353public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001354 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 // FIXME. All stubs for now!
1356 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001357
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001358 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001359 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001360 QualType ResultType,
1361 Selector Sel,
1362 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001363 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001364 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001365 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001366
1367 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001368 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001369 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001370 QualType ResultType,
1371 Selector Sel,
1372 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001373 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001374 llvm::Value *Receiver,
1375 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001376 const CallArgList &CallArgs,
1377 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001379 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001380 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001381
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001382 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1383 bool lvalue = false)
1384 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001385
1386 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1387 /// untyped one.
1388 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1389 const ObjCMethodDecl *Method)
1390 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001391
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001392 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001393
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001394 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001395 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001396 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397
John McCall5a180392010-07-24 00:37:23 +00001398 virtual llvm::Constant *GetEHType(QualType T);
1399
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001400 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001401 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001402 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001403 virtual llvm::Constant *GetPropertySetFunction() {
1404 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001405 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001406
David Chisnall8fac25d2010-12-26 22:13:16 +00001407 virtual llvm::Constant *GetSetStructFunction() {
1408 return ObjCTypes.getCopyStructFn();
1409 }
1410 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001411 return ObjCTypes.getCopyStructFn();
1412 }
1413
Chris Lattner74391b42009-03-22 21:03:39 +00001414 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001415 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001416 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001417
John McCallf1549f62010-07-06 01:34:17 +00001418 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1419 const ObjCAtTryStmt &S);
1420 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1421 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001423 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001424 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001425 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001427 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001429 llvm::Value *src, llvm::Value *dest,
1430 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001431 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001432 llvm::Value *src, llvm::Value *dest,
1433 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001434 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001435 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001436 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1437 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001438 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001439 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1440 QualType ObjectTy,
1441 llvm::Value *BaseValue,
1442 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001443 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001444 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001445 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001446 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001447};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001448
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001449} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001450
1451/* *** Helper Functions *** */
1452
1453/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001454static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001455 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001456 unsigned idx0,
1457 unsigned idx1) {
1458 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001459 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1460 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001461 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001462 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001463}
1464
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001465/// hasObjCExceptionAttribute - Return true if this class or any super
1466/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001467static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001468 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001469 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001470 return true;
1471 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001472 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001473 return false;
1474}
1475
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001476/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001478CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001479 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001480 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001481 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001482}
1483
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001484/// GetClass - Return a reference to the class for the given interface
1485/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001486llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001487 const ObjCInterfaceDecl *ID) {
1488 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001489}
1490
1491/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001492llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1493 bool lval) {
1494 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001495}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001496llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001497 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001498 return EmitSelector(Builder, Method->getSelector());
1499}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001500
John McCall5a180392010-07-24 00:37:23 +00001501llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1502 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1503 return 0;
1504}
1505
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001506/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001507/*
1508 struct __builtin_CFString {
1509 const int *isa; // point to __CFConstantStringClassReference
1510 int flags;
1511 const char *str;
1512 long length;
1513 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001514*/
1515
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001516/// or Generate a constant NSString object.
1517/*
1518 struct __builtin_NSString {
1519 const int *isa; // point to __NSConstantStringClassReference
1520 const char *str;
1521 unsigned int length;
1522 };
1523*/
1524
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001525llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001526 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001527 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1528 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001529 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001530}
1531
1532/// Generates a message send where the super is the receiver. This is
1533/// a message send to self with special delivery semantics indicating
1534/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001535CodeGen::RValue
1536CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001537 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001538 QualType ResultType,
1539 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001540 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001541 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001542 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001543 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001544 const CodeGen::CallArgList &CallArgs,
1545 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001546 // Create and init a super structure; this is a (receiver, class)
1547 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001548 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001549 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001550 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001551 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001552 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001553 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001554
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001555 // If this is a class message the metaclass is passed as the target.
1556 llvm::Value *Target;
1557 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001558 if (isCategoryImpl) {
1559 // Message sent to 'super' in a class method defined in a category
1560 // implementation requires an odd treatment.
1561 // If we are in a class method, we must retrieve the
1562 // _metaclass_ for the current class, pointed at by
1563 // the class's "isa" pointer. The following assumes that
1564 // isa" is the first ivar in a class (which it must be).
1565 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1566 Target = CGF.Builder.CreateStructGEP(Target, 0);
1567 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001568 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001569 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1570 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1571 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1572 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001573 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001574 }
1575 else if (isCategoryImpl)
1576 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1577 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001578 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1579 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1580 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001581 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001582 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1583 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001584 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001585 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001586 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001587 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001588 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001589 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001590 EmitSelector(CGF.Builder, Sel),
1591 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001592 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001593}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001594
1595/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001596CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001597 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001598 QualType ResultType,
1599 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001600 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001601 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001602 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001603 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001604 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001605 EmitSelector(CGF.Builder, Sel),
1606 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001607 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001608}
1609
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001610CodeGen::RValue
1611CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001612 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001613 QualType ResultType,
1614 llvm::Value *Sel,
1615 llvm::Value *Arg0,
1616 QualType Arg0Ty,
1617 bool IsSuper,
1618 const CallArgList &CallArgs,
1619 const ObjCMethodDecl *Method,
1620 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001621 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001622 if (!IsSuper)
1623 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001624 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001625 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001626 CGF.getContext().getObjCSelType()));
1627 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001628
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001629 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001630 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001631 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001632 const llvm::FunctionType *FTy =
1633 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001634
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001635 if (Method)
1636 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1637 CGM.getContext().getCanonicalType(ResultType) &&
1638 "Result type mismatch!");
1639
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001640 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001641 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCalla1bcc4d2011-02-26 09:12:15 +00001642 CGF.EmitNullInitialization(Return.getValue(), ResultType);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001643 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001644 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001645 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1646 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1647 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001648 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001649 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001650 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001651 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001652 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001653 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001654}
1655
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001656static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1657 if (FQT.isObjCGCStrong())
1658 return Qualifiers::Strong;
1659
1660 if (FQT.isObjCGCWeak())
1661 return Qualifiers::Weak;
1662
1663 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1664 return Qualifiers::Strong;
1665
1666 if (const PointerType *PT = FQT->getAs<PointerType>())
1667 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1668
1669 return Qualifiers::GCNone;
1670}
1671
John McCall6b5a61b2011-02-07 10:33:21 +00001672llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1673 const CGBlockInfo &blockInfo) {
1674 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001675 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001676
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001677 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall6b5a61b2011-02-07 10:33:21 +00001678 return nullPtr;
1679
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001680 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001681 SkipIvars.clear();
1682 IvarsInfo.clear();
1683 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1684 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1685
Fariborz Jahanian81979822010-09-09 00:21:45 +00001686 // __isa is the first field in block descriptor and must assume by runtime's
1687 // convention that it is GC'able.
1688 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001689
1690 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1691
1692 // Calculate the basic layout of the block structure.
1693 const llvm::StructLayout *layout =
1694 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1695
1696 // Ignore the optional 'this' capture: C++ objects are not assumed
1697 // to be GC'ed.
1698
1699 // Walk the captured variables.
1700 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1701 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1702 const VarDecl *variable = ci->getVariable();
1703 QualType type = variable->getType();
1704
1705 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1706
1707 // Ignore constant captures.
1708 if (capture.isConstant()) continue;
1709
1710 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1711
1712 // __block variables are passed by their descriptor address.
1713 if (ci->isByRef()) {
1714 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001715 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001716 }
1717
1718 assert(!type->isArrayType() && "array variable should not be caught");
1719 if (const RecordType *record = type->getAs<RecordType>()) {
1720 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001721 continue;
1722 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001723
John McCall6b5a61b2011-02-07 10:33:21 +00001724 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1725 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1726
1727 if (GCAttr == Qualifiers::Strong)
1728 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1729 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001730 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001731 SkipIvars.push_back(GC_IVAR(fieldOffset,
1732 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001733 }
1734
1735 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001736 return nullPtr;
1737
1738 // Sort on byte position; captures might not be allocated in order,
1739 // and unions can do funny things.
1740 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1741 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001742
1743 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001744 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001745 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1746 printf("\n block variable layout for block: ");
1747 const unsigned char *s = (unsigned char*)BitMap.c_str();
1748 for (unsigned i = 0; i < BitMap.size(); i++)
1749 if (!(s[i] & 0xf0))
1750 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1751 else
1752 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1753 printf("\n");
1754 }
1755
1756 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001757}
1758
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001759llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001760 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001761 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001762 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001763 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1764
Owen Anderson3c4972d2009-07-29 18:54:39 +00001765 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001766 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001767}
1768
Fariborz Jahanianda320092009-01-29 19:24:30 +00001769void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001770 // FIXME: We shouldn't need this, the protocol decl should contain enough
1771 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001772 DefinedProtocols.insert(PD->getIdentifier());
1773
1774 // If we have generated a forward reference to this protocol, emit
1775 // it now. Otherwise do nothing, the protocol objects are lazily
1776 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001777 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001778 GetOrEmitProtocol(PD);
1779}
1780
Fariborz Jahanianda320092009-01-29 19:24:30 +00001781llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001782 if (DefinedProtocols.count(PD->getIdentifier()))
1783 return GetOrEmitProtocol(PD);
1784 return GetOrEmitProtocolRef(PD);
1785}
1786
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001787/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001788// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1789struct _objc_protocol {
1790struct _objc_protocol_extension *isa;
1791char *protocol_name;
1792struct _objc_protocol_list *protocol_list;
1793struct _objc__method_prototype_list *instance_methods;
1794struct _objc__method_prototype_list *class_methods
1795};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001796
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001797See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001799llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1800 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1801
1802 // Early exit if a defining object has already been generated.
1803 if (Entry && Entry->hasInitializer())
1804 return Entry;
1805
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001806 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001807 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001808 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1809
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001810 // Construct method lists.
1811 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1812 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001814 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001815 ObjCMethodDecl *MD = *i;
1816 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1817 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1818 OptInstanceMethods.push_back(C);
1819 } else {
1820 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001821 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001822 }
1823
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001824 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001825 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001826 ObjCMethodDecl *MD = *i;
1827 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1828 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1829 OptClassMethods.push_back(C);
1830 } else {
1831 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001832 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001833 }
1834
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001836 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001837 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001839 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001840 PD->protocol_begin(),
1841 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001843 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001844 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1845 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001847 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001848 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1849 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001850 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001851 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001852
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001854 // Already created, fix the linkage and update the initializer.
1855 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856 Entry->setInitializer(Init);
1857 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001858 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001859 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001861 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001862 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001864 // FIXME: Is this necessary? Why only for protocol?
1865 Entry->setAlignment(4);
1866 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001867 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001868
1869 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870}
1871
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001872llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1874
1875 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001876 // We use the initializer as a marker of whether this is a forward
1877 // reference or not. At module finalization we add the empty
1878 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001879 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001880 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001881 llvm::GlobalValue::ExternalLinkage,
1882 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001883 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001885 // FIXME: Is this necessary? Why only for protocol?
1886 Entry->setAlignment(4);
1887 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001889 return Entry;
1890}
1891
1892/*
1893 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001894 uint32_t size;
1895 struct objc_method_description_list *optional_instance_methods;
1896 struct objc_method_description_list *optional_class_methods;
1897 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001898 };
1899*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001900llvm::Constant *
1901CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1902 const ConstantVector &OptInstanceMethods,
1903 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001904 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001905 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001907 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001908 Values[1] =
1909 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001910 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001911 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1912 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001913 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001914 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001915 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1916 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001917 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001918 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001920 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001921 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001922 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001923 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001924
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001926 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001927
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001928 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001929 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001931 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001932}
1933
1934/*
1935 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001936 struct objc_protocol_list *next;
1937 long count;
1938 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001939 };
1940*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001941llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001942CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001943 ObjCProtocolDecl::protocol_iterator begin,
1944 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945 std::vector<llvm::Constant*> ProtocolRefs;
1946
Daniel Dunbardbc93372008-08-21 21:57:41 +00001947 for (; begin != end; ++begin)
1948 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001949
1950 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001952 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001953
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001955 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956
1957 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001958 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001959 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001960 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001961 ProtocolRefs.size() - 1);
1962 Values[2] =
1963 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1964 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001966
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001967 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001969 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001970 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001971 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972}
1973
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001974void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1975 std::vector<llvm::Constant*> &Properties,
1976 const Decl *Container,
1977 const ObjCProtocolDecl *PROTO,
1978 const ObjCCommonTypesHelper &ObjCTypes) {
1979 std::vector<llvm::Constant*> Prop(2);
1980 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1981 E = PROTO->protocol_end(); P != E; ++P)
1982 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1983 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1984 E = PROTO->prop_end(); I != E; ++I) {
1985 const ObjCPropertyDecl *PD = *I;
1986 if (!PropertySet.insert(PD->getIdentifier()))
1987 continue;
1988 Prop[0] = GetPropertyName(PD->getIdentifier());
1989 Prop[1] = GetPropertyTypeString(PD, Container);
1990 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1991 }
1992}
1993
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001994/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001995 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 const char * const name;
1997 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001998 };
1999
2000 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002001 uint32_t entsize; // sizeof (struct _objc_property)
2002 uint32_t prop_count;
2003 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002004 };
2005*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002006llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002007 const Decl *Container,
2008 const ObjCContainerDecl *OCD,
2009 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002010 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002011 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002012 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2013 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002014 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002015 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002016 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002017 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002018 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002019 Prop));
2020 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002021 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002022 for (ObjCInterfaceDecl::all_protocol_iterator
2023 P = OID->all_referenced_protocol_begin(),
2024 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002025 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2026 ObjCTypes);
2027 }
2028 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2029 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2030 E = CD->protocol_end(); P != E; ++P)
2031 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2032 ObjCTypes);
2033 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002034
2035 // Return null for empty list.
2036 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002037 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002038
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002039 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002040 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002041 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002042 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2043 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002044 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002045 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002046 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002047 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002048
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002049 llvm::GlobalVariable *GV =
2050 CreateMetadataVar(Name, Init,
2051 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002052 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002053 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002054 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002055 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002056}
2057
2058/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002059 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002060 int count;
2061 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062 };
2063*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002064llvm::Constant *
2065CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2066 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002067 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2069 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002070 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002071 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002072 Desc);
2073}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002074
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002075llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002076 const char *Section,
2077 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002078 // Return null for empty list.
2079 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002080 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002081
2082 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002083 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002085 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002086 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002087 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002088
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002089 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002090 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002091 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002092}
2093
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002094/*
2095 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002096 char *category_name;
2097 char *class_name;
2098 struct _objc_method_list *instance_methods;
2099 struct _objc_method_list *class_methods;
2100 struct _objc_protocol_list *protocols;
2101 uint32_t size; // <rdar://4585769>
2102 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002103 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002105void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002106 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002107
Mike Stumpf5408fe2009-05-16 07:57:57 +00002108 // FIXME: This is poor design, the OCD should have a pointer to the category
2109 // decl. Additionally, note that Category can be null for the @implementation
2110 // w/o an @interface case. Sema should just create one for us as it does for
2111 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002112 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002113 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002114 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002115
2116 llvm::SmallString<256> ExtName;
2117 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2118 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002119
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002120 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002121 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002122 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002123 // Instance methods should always be defined.
2124 InstanceMethods.push_back(GetMethodConstant(*i));
2125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002126 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002127 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002128 // Class methods should always be defined.
2129 ClassMethods.push_back(GetMethodConstant(*i));
2130 }
2131
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002132 std::vector<llvm::Constant*> Values(7);
2133 Values[0] = GetClassName(OCD->getIdentifier());
2134 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002135 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002137 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002138 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002139 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002140 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002141 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002142 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002143 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002144 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002146 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002147 Category->protocol_begin(),
2148 Category->protocol_end());
2149 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002150 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002151 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002152 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002153
2154 // If there is no category @interface then there can be no properties.
2155 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002156 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002157 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002158 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002159 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002160 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161
Owen Anderson08e25242009-07-27 22:29:56 +00002162 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002163 Values);
2164
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002165 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002166 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002167 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002168 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002169 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002170 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002171}
2172
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173// FIXME: Get from somewhere?
2174enum ClassFlags {
2175 eClassFlags_Factory = 0x00001,
2176 eClassFlags_Meta = 0x00002,
2177 // <rdr://5142207>
2178 eClassFlags_HasCXXStructors = 0x02000,
2179 eClassFlags_Hidden = 0x20000,
2180 eClassFlags_ABI2_Hidden = 0x00010,
2181 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2182};
2183
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184/*
2185 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186 Class isa;
2187 Class super_class;
2188 const char *name;
2189 long version;
2190 long info;
2191 long instance_size;
2192 struct _objc_ivar_list *ivars;
2193 struct _objc_method_list *methods;
2194 struct _objc_cache *cache;
2195 struct _objc_protocol_list *protocols;
2196 // Objective-C 1.0 extensions (<rdr://4585769>)
2197 const char *ivar_layout;
2198 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 };
2200
2201 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002202*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002204 DefinedSymbols.insert(ID->getIdentifier());
2205
Chris Lattner8ec03f52008-11-24 03:54:41 +00002206 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002208 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002209 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002210 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002211 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002212 Interface->all_referenced_protocol_begin(),
2213 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002215 if (ID->getNumIvarInitializers())
2216 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002217 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002218 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219
2220 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002221 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 Flags |= eClassFlags_Hidden;
2223
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002224 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002225 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002226 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002227 // Instance methods should always be defined.
2228 InstanceMethods.push_back(GetMethodConstant(*i));
2229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002230 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002231 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002232 // Class methods should always be defined.
2233 ClassMethods.push_back(GetMethodConstant(*i));
2234 }
2235
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002237 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002238 ObjCPropertyImplDecl *PID = *i;
2239
2240 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2241 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2242
2243 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2244 if (llvm::Constant *C = GetMethodConstant(MD))
2245 InstanceMethods.push_back(C);
2246 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2247 if (llvm::Constant *C = GetMethodConstant(MD))
2248 InstanceMethods.push_back(C);
2249 }
2250 }
2251
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002252 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002253 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002255 // Record a reference to the super class.
2256 LazySymbols.insert(Super->getIdentifier());
2257
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002258 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002259 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002260 ObjCTypes.ClassPtrTy);
2261 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002262 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 }
2264 Values[ 2] = GetClassName(ID->getIdentifier());
2265 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002266 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2267 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2268 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002269 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002270 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002271 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002272 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002273 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002275 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002277 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002278 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002279 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002281 std::string Name("\01L_OBJC_CLASS_");
2282 Name += ClassName;
2283 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2284 // Check for a forward reference.
2285 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2286 if (GV) {
2287 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2288 "Forward metaclass reference has incorrect type.");
2289 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2290 GV->setInitializer(Init);
2291 GV->setSection(Section);
2292 GV->setAlignment(4);
2293 CGM.AddUsedGlobal(GV);
2294 }
2295 else
2296 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 DefinedClasses.push_back(GV);
2298}
2299
2300llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2301 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002302 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002304 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002305
John McCall1fb0caa2010-10-22 21:05:15 +00002306 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002308
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 std::vector<llvm::Constant*> Values(12);
2310 // The isa for the metaclass is the root of the hierarchy.
2311 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2312 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2313 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002314 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002315 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002317 // The super class for the metaclass is emitted as the name of the
2318 // super class. The runtime fixes this up to point to the
2319 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002320 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002321 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002322 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 ObjCTypes.ClassPtrTy);
2324 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002325 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 }
2327 Values[ 2] = GetClassName(ID->getIdentifier());
2328 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002329 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2330 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2331 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002332 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002333 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002334 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002335 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002336 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002337 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002338 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 Values[ 9] = Protocols;
2340 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002341 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002343 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002344 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345 Values);
2346
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002347 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002348 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002349
2350 // Check for a forward reference.
2351 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2352 if (GV) {
2353 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2354 "Forward metaclass reference has incorrect type.");
2355 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2356 GV->setInitializer(Init);
2357 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002358 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002359 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002360 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002361 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002362 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002363 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002364 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002365
2366 return GV;
2367}
2368
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002369llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002370 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002371
Mike Stumpf5408fe2009-05-16 07:57:57 +00002372 // FIXME: Should we look these up somewhere other than the module. Its a bit
2373 // silly since we only generate these while processing an implementation, so
2374 // exactly one pointer would work if know when we entered/exitted an
2375 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002376
2377 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002378 // Previously, metaclass with internal linkage may have been defined.
2379 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002380 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2381 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002382 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2383 "Forward metaclass reference has incorrect type.");
2384 return GV;
2385 } else {
2386 // Generate as an external reference to keep a consistent
2387 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002388 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002389 llvm::GlobalValue::ExternalLinkage,
2390 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002391 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002392 }
2393}
2394
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002395llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2396 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2397
2398 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2399 true)) {
2400 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2401 "Forward class metadata reference has incorrect type.");
2402 return GV;
2403 } else {
2404 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2405 llvm::GlobalValue::ExternalLinkage,
2406 0,
2407 Name);
2408 }
2409}
2410
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411/*
2412 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002413 uint32_t size;
2414 const char *weak_ivar_layout;
2415 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416 };
2417*/
2418llvm::Constant *
2419CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002420 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002421 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422
2423 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002424 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002425 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002426 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002427 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428
2429 // Return null if no extension bits are used.
2430 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002431 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002434 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002435 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002437 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438}
2439
2440/*
2441 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002442 char *ivar_name;
2443 char *ivar_type;
2444 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445 };
2446
2447 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 int ivar_count;
2449 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002451*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002453 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 std::vector<llvm::Constant*> Ivars, Ivar(3);
2455
2456 // When emitting the root class GCC emits ivar entries for the
2457 // actual class structure. It is not clear if we need to follow this
2458 // behavior; for now lets try and get away with not doing it. If so,
2459 // the cleanest solution would be to make up an ObjCInterfaceDecl
2460 // for the class.
2461 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002462 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002463
2464 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002465 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002466
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002467 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002468 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002469
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002470 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2471 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002472 // Ignore unnamed bit-fields.
2473 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002474 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002475 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2476 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002478 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002479 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 }
2481
2482 // Return null for empty list.
2483 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002484 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002485
2486 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002487 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002488 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002489 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002490 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002491 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002493 llvm::GlobalVariable *GV;
2494 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002495 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002496 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002497 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002498 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002499 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002500 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002501 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002502 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002503}
2504
2505/*
2506 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002507 SEL method_name;
2508 char *method_types;
2509 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002510 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002511
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002512 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513 struct objc_method_list *obsolete;
2514 int count;
2515 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002516 };
2517*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002518
2519/// GetMethodConstant - Return a struct objc_method constant for the
2520/// given method if it has been defined. The result is null if the
2521/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002522llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002523 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002524 if (!Fn)
2525 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002526
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002527 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002528 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002529 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002530 ObjCTypes.SelectorPtrTy);
2531 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002532 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002533 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002534}
2535
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002536llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002537 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002538 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539 // Return null for empty list.
2540 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002541 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002542
2543 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002544 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002545 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002546 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002547 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002548 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002549 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002550
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002551 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002552 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002553 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002554}
2555
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002556llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002557 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002558 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002559 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002560
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002561 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002562 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002563 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002564 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002565 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002566 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002567 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002568 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002569 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002570
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002571 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002572}
2573
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002574llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002575CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002576 llvm::Constant *Init,
2577 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002578 unsigned Align,
2579 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002580 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002581 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002582 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002583 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002584 if (Section)
2585 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002586 if (Align)
2587 GV->setAlignment(Align);
2588 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002589 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002590 return GV;
2591}
2592
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002593llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002594 // Abuse this interface function as a place to finalize.
2595 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002596 return NULL;
2597}
2598
Chris Lattner74391b42009-03-22 21:03:39 +00002599llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002600 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002601}
2602
Chris Lattner74391b42009-03-22 21:03:39 +00002603llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002604 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002605}
2606
David Chisnall8fac25d2010-12-26 22:13:16 +00002607llvm::Constant *CGObjCMac::GetGetStructFunction() {
2608 return ObjCTypes.getCopyStructFn();
2609}
2610llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002611 return ObjCTypes.getCopyStructFn();
2612}
2613
Chris Lattner74391b42009-03-22 21:03:39 +00002614llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002615 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002616}
2617
John McCallf1549f62010-07-06 01:34:17 +00002618void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2619 return EmitTryOrSynchronizedStmt(CGF, S);
2620}
2621
2622void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2623 const ObjCAtSynchronizedStmt &S) {
2624 return EmitTryOrSynchronizedStmt(CGF, S);
2625}
2626
John McCallcc505292010-07-21 06:59:36 +00002627namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002628 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002629 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002630 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002631 llvm::Value *CallTryExitVar;
2632 llvm::Value *ExceptionData;
2633 ObjCTypesHelper &ObjCTypes;
2634 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002635 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002636 llvm::Value *CallTryExitVar,
2637 llvm::Value *ExceptionData,
2638 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002639 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002640 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2641
2642 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2643 // Check whether we need to call objc_exception_try_exit.
2644 // In optimized code, this branch will always be folded.
2645 llvm::BasicBlock *FinallyCallExit =
2646 CGF.createBasicBlock("finally.call_exit");
2647 llvm::BasicBlock *FinallyNoCallExit =
2648 CGF.createBasicBlock("finally.no_call_exit");
2649 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2650 FinallyCallExit, FinallyNoCallExit);
2651
2652 CGF.EmitBlock(FinallyCallExit);
2653 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2654 ->setDoesNotThrow();
2655
2656 CGF.EmitBlock(FinallyNoCallExit);
2657
2658 if (isa<ObjCAtTryStmt>(S)) {
2659 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002660 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2661 // Save the current cleanup destination in case there's
2662 // control flow inside the finally statement.
2663 llvm::Value *CurCleanupDest =
2664 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2665
John McCallcc505292010-07-21 06:59:36 +00002666 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2667
John McCalld96a8e72010-08-11 00:16:14 +00002668 if (CGF.HaveInsertPoint()) {
2669 CGF.Builder.CreateStore(CurCleanupDest,
2670 CGF.getNormalCleanupDestSlot());
2671 } else {
2672 // Currently, the end of the cleanup must always exist.
2673 CGF.EnsureInsertPoint();
2674 }
2675 }
John McCallcc505292010-07-21 06:59:36 +00002676 } else {
2677 // Emit objc_sync_exit(expr); as finally's sole statement for
2678 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002679 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002680 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2681 ->setDoesNotThrow();
2682 }
2683 }
2684 };
John McCall87bb5822010-07-31 23:20:56 +00002685
2686 class FragileHazards {
2687 CodeGenFunction &CGF;
2688 llvm::SmallVector<llvm::Value*, 20> Locals;
2689 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2690
2691 llvm::InlineAsm *ReadHazard;
2692 llvm::InlineAsm *WriteHazard;
2693
2694 llvm::FunctionType *GetAsmFnType();
2695
2696 void collectLocals();
2697 void emitReadHazard(CGBuilderTy &Builder);
2698
2699 public:
2700 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002701
John McCall87bb5822010-07-31 23:20:56 +00002702 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002703 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002704 };
2705}
2706
2707/// Create the fragile-ABI read and write hazards based on the current
2708/// state of the function, which is presumed to be immediately prior
2709/// to a @try block. These hazards are used to maintain correct
2710/// semantics in the face of optimization and the fragile ABI's
2711/// cavalier use of setjmp/longjmp.
2712FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2713 collectLocals();
2714
2715 if (Locals.empty()) return;
2716
2717 // Collect all the blocks in the function.
2718 for (llvm::Function::iterator
2719 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2720 BlocksBeforeTry.insert(&*I);
2721
2722 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2723
2724 // Create a read hazard for the allocas. This inhibits dead-store
2725 // optimizations and forces the values to memory. This hazard is
2726 // inserted before any 'throwing' calls in the protected scope to
2727 // reflect the possibility that the variables might be read from the
2728 // catch block if the call throws.
2729 {
2730 std::string Constraint;
2731 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2732 if (I) Constraint += ',';
2733 Constraint += "*m";
2734 }
2735
2736 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2737 }
2738
2739 // Create a write hazard for the allocas. This inhibits folding
2740 // loads across the hazard. This hazard is inserted at the
2741 // beginning of the catch path to reflect the possibility that the
2742 // variables might have been written within the protected scope.
2743 {
2744 std::string Constraint;
2745 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2746 if (I) Constraint += ',';
2747 Constraint += "=*m";
2748 }
2749
2750 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2751 }
2752}
2753
2754/// Emit a write hazard at the current location.
2755void FragileHazards::emitWriteHazard() {
2756 if (Locals.empty()) return;
2757
2758 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2759 ->setDoesNotThrow();
2760}
2761
John McCall87bb5822010-07-31 23:20:56 +00002762void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2763 assert(!Locals.empty());
2764 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2765 ->setDoesNotThrow();
2766}
2767
2768/// Emit read hazards in all the protected blocks, i.e. all the blocks
2769/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002770void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002771 if (Locals.empty()) return;
2772
2773 CGBuilderTy Builder(CGF.getLLVMContext());
2774
2775 // Iterate through all blocks, skipping those prior to the try.
2776 for (llvm::Function::iterator
2777 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2778 llvm::BasicBlock &BB = *FI;
2779 if (BlocksBeforeTry.count(&BB)) continue;
2780
2781 // Walk through all the calls in the block.
2782 for (llvm::BasicBlock::iterator
2783 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2784 llvm::Instruction &I = *BI;
2785
2786 // Ignore instructions that aren't non-intrinsic calls.
2787 // These are the only calls that can possibly call longjmp.
2788 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2789 if (isa<llvm::IntrinsicInst>(I))
2790 continue;
2791
2792 // Ignore call sites marked nounwind. This may be questionable,
2793 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2794 llvm::CallSite CS(&I);
2795 if (CS.doesNotThrow()) continue;
2796
John McCall0b251722010-08-04 05:59:32 +00002797 // Insert a read hazard before the call. This will ensure that
2798 // any writes to the locals are performed before making the
2799 // call. If the call throws, then this is sufficient to
2800 // guarantee correctness as long as it doesn't also write to any
2801 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002802 Builder.SetInsertPoint(&BB, BI);
2803 emitReadHazard(Builder);
2804 }
2805 }
2806}
2807
2808static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2809 if (V) S.insert(V);
2810}
2811
2812void FragileHazards::collectLocals() {
2813 // Compute a set of allocas to ignore.
2814 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2815 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2816 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2817 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2818
2819 // Collect all the allocas currently in the function. This is
2820 // probably way too aggressive.
2821 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2822 for (llvm::BasicBlock::iterator
2823 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2824 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2825 Locals.push_back(&*I);
2826}
2827
2828llvm::FunctionType *FragileHazards::GetAsmFnType() {
2829 std::vector<const llvm::Type *> Tys(Locals.size());
2830 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2831 Tys[I] = Locals[I]->getType();
2832 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002833}
2834
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002835/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002837 Objective-C setjmp-longjmp (sjlj) Exception Handling
2838 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002839
John McCallf1549f62010-07-06 01:34:17 +00002840 A catch buffer is a setjmp buffer plus:
2841 - a pointer to the exception that was caught
2842 - a pointer to the previous exception data buffer
2843 - two pointers of reserved storage
2844 Therefore catch buffers form a stack, with a pointer to the top
2845 of the stack kept in thread-local storage.
2846
2847 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2848 objc_exception_try_exit pops the given catch buffer, which is
2849 required to be the top of the EH stack.
2850 objc_exception_throw pops the top of the EH stack, writes the
2851 thrown exception into the appropriate field, and longjmps
2852 to the setjmp buffer. It crashes the process (with a printf
2853 and an abort()) if there are no catch buffers on the stack.
2854 objc_exception_extract just reads the exception pointer out of the
2855 catch buffer.
2856
2857 There's no reason an implementation couldn't use a light-weight
2858 setjmp here --- something like __builtin_setjmp, but API-compatible
2859 with the heavyweight setjmp. This will be more important if we ever
2860 want to implement correct ObjC/C++ exception interactions for the
2861 fragile ABI.
2862
2863 Note that for this use of setjmp/longjmp to be correct, we may need
2864 to mark some local variables volatile: if a non-volatile local
2865 variable is modified between the setjmp and the longjmp, it has
2866 indeterminate value. For the purposes of LLVM IR, it may be
2867 sufficient to make loads and stores within the @try (to variables
2868 declared outside the @try) volatile. This is necessary for
2869 optimized correctness, but is not currently being done; this is
2870 being tracked as rdar://problem/8160285
2871
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002872 The basic framework for a @try-catch-finally is as follows:
2873 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002874 objc_exception_data d;
2875 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002876 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002877
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002878 objc_exception_try_enter(&d);
2879 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002880 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002881 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002882 // exception path
2883 id _caught = objc_exception_extract(&d);
2884
2885 // enter new try scope for handlers
2886 if (!setjmp(d.jmp_buf)) {
2887 ... match exception and execute catch blocks ...
2888
2889 // fell off end, rethrow.
2890 _rethrow = _caught;
2891 ... jump-through-finally to finally_rethrow ...
2892 } else {
2893 // exception in catch block
2894 _rethrow = objc_exception_extract(&d);
2895 _call_try_exit = false;
2896 ... jump-through-finally to finally_rethrow ...
2897 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002898 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002899 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002900
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002901 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002902 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002904
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002905 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002906 ... dispatch to finally destination ...
2907
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002908 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002909 objc_exception_throw(_rethrow);
2910
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911 finally_end:
2912 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 This framework differs slightly from the one gcc uses, in that gcc
2915 uses _rethrow to determine if objc_exception_try_exit should be called
2916 and if the object should be rethrown. This breaks in the face of
2917 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002918
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002919 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002920
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002921 - If there are no catch blocks, then we avoid emitting the second
2922 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002923
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002924 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2925 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002926
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002927 - FIXME: If there is no @finally block we can do a few more
2928 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002930 Rethrows and Jumps-Through-Finally
2931 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002932
John McCallf1549f62010-07-06 01:34:17 +00002933 '@throw;' is supported by pushing the currently-caught exception
2934 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002935
John McCallf1549f62010-07-06 01:34:17 +00002936 Branches through the @finally block are handled with an ordinary
2937 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2938 exceptions are not compatible with C++ exceptions, and this is
2939 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002940
John McCallf1549f62010-07-06 01:34:17 +00002941 @synchronized(expr) { stmt; } is emitted as if it were:
2942 id synch_value = expr;
2943 objc_sync_enter(synch_value);
2944 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002945*/
2946
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002947void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2948 const Stmt &S) {
2949 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002950
2951 // A destination for the fall-through edges of the catch handlers to
2952 // jump to.
2953 CodeGenFunction::JumpDest FinallyEnd =
2954 CGF.getJumpDestInCurrentScope("finally.end");
2955
2956 // A destination for the rethrow edge of the catch handlers to jump
2957 // to.
2958 CodeGenFunction::JumpDest FinallyRethrow =
2959 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002960
Daniel Dunbar1c566672009-02-24 01:43:46 +00002961 // For @synchronized, call objc_sync_enter(sync.expr). The
2962 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002963 // @synchronized. We can't avoid a temp here because we need the
2964 // value to be preserved. If the backend ever does liveness
2965 // correctly after setjmp, this will be unnecessary.
2966 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002967 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002968 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002969 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2970 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002971 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2972 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002973
2974 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2975 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002976 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002977
John McCall0b251722010-08-04 05:59:32 +00002978 // Allocate memory for the setjmp buffer. This needs to be kept
2979 // live throughout the try and catch blocks.
2980 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2981 "exceptiondata.ptr");
2982
John McCall87bb5822010-07-31 23:20:56 +00002983 // Create the fragile hazards. Note that this will not capture any
2984 // of the allocas required for exception processing, but will
2985 // capture the current basic block (which extends all the way to the
2986 // setjmp call) as "before the @try".
2987 FragileHazards Hazards(CGF);
2988
John McCallf1549f62010-07-06 01:34:17 +00002989 // Create a flag indicating whether the cleanup needs to call
2990 // objc_exception_try_exit. This is true except when
2991 // - no catches match and we're branching through the cleanup
2992 // just to rethrow the exception, or
2993 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002994 // The setjmp-safety rule here is that we should always store to this
2995 // variable in a place that dominates the branch through the cleanup
2996 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002997 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002998 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002999
John McCall9e2213d2010-10-04 23:42:51 +00003000 // A slot containing the exception to rethrow. Only needed when we
3001 // have both a @catch and a @finally.
3002 llvm::Value *PropagatingExnVar = 0;
3003
John McCallf1549f62010-07-06 01:34:17 +00003004 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003005 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003006 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003007 CallTryExitVar,
3008 ExceptionData,
3009 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003010
3011 // Enter a try block:
3012 // - Call objc_exception_try_enter to push ExceptionData on top of
3013 // the EH stack.
3014 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3015 ->setDoesNotThrow();
3016
3017 // - Call setjmp on the exception data buffer.
3018 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3019 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3020 llvm::Value *SetJmpBuffer =
3021 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3022 llvm::CallInst *SetJmpResult =
3023 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3024 SetJmpResult->setDoesNotThrow();
3025
3026 // If setjmp returned 0, enter the protected block; otherwise,
3027 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003028 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3029 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003030 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003031 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3032 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003033
John McCallf1549f62010-07-06 01:34:17 +00003034 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003035 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003036 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003037 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003038 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003039
3040 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003041
John McCallf1549f62010-07-06 01:34:17 +00003042 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003043 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003044
John McCall87bb5822010-07-31 23:20:56 +00003045 // Don't optimize loads of the in-scope locals across this point.
3046 Hazards.emitWriteHazard();
3047
John McCallf1549f62010-07-06 01:34:17 +00003048 // For a @synchronized (or a @try with no catches), just branch
3049 // through the cleanup to the rethrow block.
3050 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3051 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003052 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003053 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003054
3055 // Otherwise, we have to match against the caught exceptions.
3056 } else {
John McCall0b251722010-08-04 05:59:32 +00003057 // Retrieve the exception object. We may emit multiple blocks but
3058 // nothing can cross this so the value is already in SSA form.
3059 llvm::CallInst *Caught =
3060 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3061 ExceptionData, "caught");
3062 Caught->setDoesNotThrow();
3063
John McCallf1549f62010-07-06 01:34:17 +00003064 // Push the exception to rethrow onto the EH value stack for the
3065 // benefit of any @throws in the handlers.
3066 CGF.ObjCEHValueStack.push_back(Caught);
3067
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003068 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
John McCall0b251722010-08-04 05:59:32 +00003070 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003071
John McCall0b251722010-08-04 05:59:32 +00003072 llvm::BasicBlock *CatchBlock = 0;
3073 llvm::BasicBlock *CatchHandler = 0;
3074 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003075 // Save the currently-propagating exception before
3076 // objc_exception_try_enter clears the exception slot.
3077 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3078 "propagating_exception");
3079 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3080
John McCall0b251722010-08-04 05:59:32 +00003081 // Enter a new exception try block (in case a @catch block
3082 // throws an exception).
3083 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3084 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003085
John McCall0b251722010-08-04 05:59:32 +00003086 llvm::CallInst *SetJmpResult =
3087 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3088 "setjmp.result");
3089 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090
John McCall0b251722010-08-04 05:59:32 +00003091 llvm::Value *Threw =
3092 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3093
3094 CatchBlock = CGF.createBasicBlock("catch");
3095 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3096 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3097
3098 CGF.EmitBlock(CatchBlock);
3099 }
3100
3101 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102
Daniel Dunbar55e40722008-09-27 07:03:52 +00003103 // Handle catch list. As a special case we check if everything is
3104 // matched and avoid generating code for falling off the end if
3105 // so.
3106 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003107 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3108 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003109
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003110 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003111 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003112
Anders Carlsson80f25672008-09-09 17:59:25 +00003113 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003114 if (!CatchParam) {
3115 AllMatched = true;
3116 } else {
John McCall183700f2009-09-21 23:43:11 +00003117 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003118
John McCallf1549f62010-07-06 01:34:17 +00003119 // catch(id e) always matches under this ABI, since only
3120 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003121 // FIXME: For the time being we also match id<X>; this should
3122 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003123 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003124 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003126
John McCallf1549f62010-07-06 01:34:17 +00003127 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003128 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003129 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3130
Anders Carlssondde0a942008-09-11 09:15:33 +00003131 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003132 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003133 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003134
3135 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003136 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003137 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003138
Anders Carlssondde0a942008-09-11 09:15:33 +00003139 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003140
3141 // The scope of the catch variable ends right here.
3142 CatchVarCleanups.ForceCleanup();
3143
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003144 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003145 break;
3146 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003147
Steve Naroff14108da2009-07-10 23:34:53 +00003148 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003149 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003150
3151 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003152 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3153 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003154
3155 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003156 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003157
John McCallf1549f62010-07-06 01:34:17 +00003158 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003159 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3160 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003161 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003162
John McCallf1549f62010-07-06 01:34:17 +00003163 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3164 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003165
3166 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003167 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003168
Anders Carlsson80f25672008-09-09 17:59:25 +00003169 // Emit the @catch block.
3170 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003171
3172 // Collect any cleanups for the catch variable. The scope lasts until
3173 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003174 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003175
John McCallb6bbcc92010-10-15 04:57:14 +00003176 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003177 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003178
John McCallf1549f62010-07-06 01:34:17 +00003179 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180 llvm::Value *Tmp =
3181 CGF.Builder.CreateBitCast(Caught,
3182 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003183 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003184 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003185
Anders Carlssondde0a942008-09-11 09:15:33 +00003186 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003187
3188 // We're done with the catch variable.
3189 CatchVarCleanups.ForceCleanup();
3190
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003191 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003192
Anders Carlsson80f25672008-09-09 17:59:25 +00003193 CGF.EmitBlock(NextCatchBlock);
3194 }
3195
John McCallf1549f62010-07-06 01:34:17 +00003196 CGF.ObjCEHValueStack.pop_back();
3197
John McCall0b251722010-08-04 05:59:32 +00003198 // If nothing wanted anything to do with the caught exception,
3199 // kill the extract call.
3200 if (Caught->use_empty())
3201 Caught->eraseFromParent();
3202
3203 if (!AllMatched)
3204 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3205
3206 if (HasFinally) {
3207 // Emit the exception handler for the @catch blocks.
3208 CGF.EmitBlock(CatchHandler);
3209
3210 // In theory we might now need a write hazard, but actually it's
3211 // unnecessary because there's no local-accessing code between
3212 // the try's write hazard and here.
3213 //Hazards.emitWriteHazard();
3214
John McCall9e2213d2010-10-04 23:42:51 +00003215 // Extract the new exception and save it to the
3216 // propagating-exception slot.
3217 assert(PropagatingExnVar);
3218 llvm::CallInst *NewCaught =
3219 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3220 ExceptionData, "caught");
3221 NewCaught->setDoesNotThrow();
3222 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3223
John McCall0b251722010-08-04 05:59:32 +00003224 // Don't pop the catch handler; the throw already did.
3225 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003226 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003227 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003229
John McCall87bb5822010-07-31 23:20:56 +00003230 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003231 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003232
John McCallf1549f62010-07-06 01:34:17 +00003233 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003234 CGF.Builder.restoreIP(TryFallthroughIP);
3235 if (CGF.HaveInsertPoint())
3236 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003237 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003238 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003239
John McCallf1549f62010-07-06 01:34:17 +00003240 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003241 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003242 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003243 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003244 // If we have a propagating-exception variable, check it.
3245 llvm::Value *PropagatingExn;
3246 if (PropagatingExnVar) {
3247 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003248
John McCall9e2213d2010-10-04 23:42:51 +00003249 // Otherwise, just look in the buffer for the exception to throw.
3250 } else {
3251 llvm::CallInst *Caught =
3252 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3253 ExceptionData);
3254 Caught->setDoesNotThrow();
3255 PropagatingExn = Caught;
3256 }
3257
3258 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003259 ->setDoesNotThrow();
3260 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003261 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003262
John McCall87bb5822010-07-31 23:20:56 +00003263 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003264}
3265
3266void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003267 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003268 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003270 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3271 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003272 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003273 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3274 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003275 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003276 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003277 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003278 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003279
John McCallf1549f62010-07-06 01:34:17 +00003280 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3281 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003282 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003283
3284 // Clear the insertion point to indicate we are in unreachable code.
3285 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003286}
3287
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003288/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003289/// object: objc_read_weak (id *src)
3290///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003291llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003292 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003293 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003294 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3295 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3296 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003297 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003298 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003299 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003300 return read_weak;
3301}
3302
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003303/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3304/// objc_assign_weak (id src, id *dst)
3305///
3306void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003307 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003308 const llvm::Type * SrcTy = src->getType();
3309 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003310 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003311 assert(Size <= 8 && "does not support size > 8");
3312 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003313 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003314 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3315 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003316 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3317 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003318 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003319 src, dst, "weakassign");
3320 return;
3321}
3322
Fariborz Jahanian58626502008-11-19 00:59:10 +00003323/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3324/// objc_assign_global (id src, id *dst)
3325///
3326void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003327 llvm::Value *src, llvm::Value *dst,
3328 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003329 const llvm::Type * SrcTy = src->getType();
3330 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003331 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003332 assert(Size <= 8 && "does not support size > 8");
3333 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003335 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3336 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003337 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3338 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003339 if (!threadlocal)
3340 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3341 src, dst, "globalassign");
3342 else
3343 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3344 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003345 return;
3346}
3347
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003348/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003349/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003350///
3351void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003352 llvm::Value *src, llvm::Value *dst,
3353 llvm::Value *ivarOffset) {
3354 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003355 const llvm::Type * SrcTy = src->getType();
3356 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003357 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003358 assert(Size <= 8 && "does not support size > 8");
3359 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003360 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003361 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3362 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003363 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3364 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003365 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3366 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003367 return;
3368}
3369
Fariborz Jahanian58626502008-11-19 00:59:10 +00003370/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3371/// objc_assign_strongCast (id src, id *dst)
3372///
3373void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003374 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003375 const llvm::Type * SrcTy = src->getType();
3376 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003377 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003378 assert(Size <= 8 && "does not support size > 8");
3379 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003380 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003381 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3382 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003383 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3384 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003385 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003386 src, dst, "weakassign");
3387 return;
3388}
3389
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003390void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003391 llvm::Value *DestPtr,
3392 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003393 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003394 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3395 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003396 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003397 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003398 return;
3399}
3400
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003401/// EmitObjCValueForIvar - Code Gen for ivar reference.
3402///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003403LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3404 QualType ObjectTy,
3405 llvm::Value *BaseValue,
3406 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003407 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003408 const ObjCInterfaceDecl *ID =
3409 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003410 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3411 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003412}
3413
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003414llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003415 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003416 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003417 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003418 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003419 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3420 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003421}
3422
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003423/* *** Private Interface *** */
3424
3425/// EmitImageInfo - Emit the image info marker used to encode some module
3426/// level information.
3427///
3428/// See: <rdr://4810609&4810587&4810587>
3429/// struct IMAGE_INFO {
3430/// unsigned version;
3431/// unsigned flags;
3432/// };
3433enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003434 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003435 eImageInfo_GarbageCollected = (1 << 1),
3436 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003437 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3438
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003439 // A flag indicating that the module has no instances of a @synthesize of a
3440 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003441 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003442};
3443
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003444void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003445 unsigned version = 0; // Version is unused?
3446 unsigned flags = 0;
3447
3448 // FIXME: Fix and continue?
3449 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3450 flags |= eImageInfo_GarbageCollected;
3451 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3452 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003454 // We never allow @synthesize of a superclass property.
3455 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003456
Chris Lattner77b89b82010-06-27 07:15:29 +00003457 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3458
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003459 // Emitted as int[2];
3460 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003461 llvm::ConstantInt::get(Int32Ty, version),
3462 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003463 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003464 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003465
3466 const char *Section;
3467 if (ObjCABI == 1)
3468 Section = "__OBJC, __image_info,regular";
3469 else
3470 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003471 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003472 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003473 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003474 Section,
3475 0,
3476 true);
3477 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003478}
3479
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003480
3481// struct objc_module {
3482// unsigned long version;
3483// unsigned long size;
3484// const char *name;
3485// Symtab symtab;
3486// };
3487
3488// FIXME: Get from somewhere
3489static const int ModuleVersion = 7;
3490
3491void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003492 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003494 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003495 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3496 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003497 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003498 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003499 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003500 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003501 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003502 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003503 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003504}
3505
3506llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003507 unsigned NumClasses = DefinedClasses.size();
3508 unsigned NumCategories = DefinedCategories.size();
3509
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003510 // Return null if no symbols were defined.
3511 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003512 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003513
3514 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003515 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003516 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003517 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3518 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003519
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003520 // The runtime expects exactly the list of defined classes followed
3521 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003522 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003523 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003524 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003525 ObjCTypes.Int8PtrTy);
3526 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003528 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003529 ObjCTypes.Int8PtrTy);
3530
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003531 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003532 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003533 NumClasses + NumCategories),
3534 Symbols);
3535
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003536 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003537
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003538 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003539 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3540 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003541 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003542 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003543}
3544
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003545llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003546 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003547 LazySymbols.insert(ID->getIdentifier());
3548
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003549 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003550
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003551 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003553 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003554 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003555 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003556 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3557 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003558 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003559 }
3560
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003561 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003562}
3563
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003564llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3565 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003566 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003567
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003568 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003570 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003571 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003572 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003573 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3574 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003575 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003576 }
3577
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003578 if (lvalue)
3579 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003580 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003581}
3582
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003583llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003584 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003585
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003586 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003588 llvm::ConstantArray::get(VMContext,
3589 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003590 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003591 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003592
Owen Andersona1cf15f2009-07-14 23:10:40 +00003593 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003594}
3595
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003596llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3597 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3598 I = MethodDefinitions.find(MD);
3599 if (I != MethodDefinitions.end())
3600 return I->second;
3601
3602 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3603 // MD isn't emitted yet because it comes from PCH.
3604 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3605 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3606 return MethodDefinitions[MD];
3607 }
3608
3609 return NULL;
3610}
3611
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003612/// GetIvarLayoutName - Returns a unique constant for the given
3613/// ivar layout bitmap.
3614llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003615 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003616 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003617}
3618
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003619void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003620 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003621 bool ForStrongLayout,
3622 bool &HasUnion) {
3623 const RecordDecl *RD = RT->getDecl();
3624 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003625 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003626 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003627 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003628 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003629
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003630 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3631 ForStrongLayout, HasUnion);
3632}
3633
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003634void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003635 const llvm::StructLayout *Layout,
3636 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003637 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003638 unsigned int BytePos, bool ForStrongLayout,
3639 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 bool IsUnion = (RD && RD->isUnion());
3641 uint64_t MaxUnionIvarSize = 0;
3642 uint64_t MaxSkippedUnionIvarSize = 0;
3643 FieldDecl *MaxField = 0;
3644 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003645 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003646 uint64_t MaxFieldOffset = 0;
3647 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003648 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003649
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003650 if (RecFields.empty())
3651 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003652 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3653 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3654
Chris Lattnerf1690852009-03-31 08:48:01 +00003655 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003656 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003657 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003658 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003659 // Note that 'i' here is actually the field index inside RD of Field,
3660 // although this dependency is hidden.
3661 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3662 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003663 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003664 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003665
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003666 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003667 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003668 LastFieldBitfieldOrUnnamed = Field;
3669 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003670 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003671 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003672
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003673 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003674 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003675 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003676 if (FQT->isUnionType())
3677 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003679 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003680 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003681 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003682 continue;
3683 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003684
Chris Lattnerf1690852009-03-31 08:48:01 +00003685 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003686 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003687 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003688 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003689 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003690 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003691 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3692 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003693 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003694 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003695 FQT = CArray->getElementType();
3696 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003697
3698 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003699 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003700 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003701 int OldIndex = IvarsInfo.size() - 1;
3702 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003703
Ted Kremenek6217b802009-07-29 21:53:49 +00003704 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003705 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003706 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003707
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003708 // Replicate layout information for each array element. Note that
3709 // one element is already done.
3710 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003711 for (int FirstIndex = IvarsInfo.size() - 1,
3712 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003713 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003714 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3715 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3716 IvarsInfo[i].ivar_size));
3717 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3718 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3719 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003720 }
3721 continue;
3722 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003723 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003724 // At this point, we are done with Record/Union and array there of.
3725 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003726 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003727
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003728 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003729 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3730 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003731 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003732 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003733 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003734 MaxUnionIvarSize = UnionIvarSize;
3735 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003736 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003737 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003738 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003739 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003740 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003742 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003743 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3744 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003745 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003746 // FIXME: Why the asymmetry? We divide by word size in bits on other
3747 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003748 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003749 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003750 MaxSkippedUnionIvarSize = UnionIvarSize;
3751 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003752 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003753 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003754 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003755 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003756 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003757 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003758 }
3759 }
3760 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003761
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003762 if (LastFieldBitfieldOrUnnamed) {
3763 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3764 // Last field was a bitfield. Must update skip info.
3765 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3766 uint64_t BitFieldSize =
3767 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3768 GC_IVAR skivar;
3769 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3770 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3771 + ((BitFieldSize % ByteSizeInBits) != 0);
3772 SkipIvars.push_back(skivar);
3773 } else {
3774 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3775 // Last field was unnamed. Must update skip info.
3776 unsigned FieldSize
3777 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3778 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3779 FieldSize / ByteSizeInBits));
3780 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003781 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003782
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003783 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003784 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003785 MaxUnionIvarSize));
3786 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003787 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003788 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003789}
3790
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003791/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3792/// the computations and returning the layout bitmap (for ivar or blocks) in
3793/// the given argument BitMap string container. Routine reads
3794/// two containers, IvarsInfo and SkipIvars which are assumed to be
3795/// filled already by the caller.
3796llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003797 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003798 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003799
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003800 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003801 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003802 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003803 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003804 if (IvarsInfo[0].ivar_bytepos == 0) {
3805 WordsToSkip = 0;
3806 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003807 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003808 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3809 WordsToScan = IvarsInfo[0].ivar_size;
3810 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003811 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003812 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003813 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003814 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003815 // consecutive 'scanned' object pointers.
3816 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003817 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003818 // Skip over 'gc'able object pointer which lay over each other.
3819 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3820 continue;
3821 // Must skip over 1 or more words. We save current skip/scan values
3822 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003823 SKIP_SCAN SkScan;
3824 SkScan.skip = WordsToSkip;
3825 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003826 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003827
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003828 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003829 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3830 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003831 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003832 WordsToSkip = 0;
3833 WordsToScan = IvarsInfo[i].ivar_size;
3834 }
3835 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003836 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003837 SKIP_SCAN SkScan;
3838 SkScan.skip = WordsToSkip;
3839 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003840 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003841 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003842
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003843 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003844 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003845 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003846 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003847 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003849 IvarsInfo[LastIndex].ivar_bytepos +
3850 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003851 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003852 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003853 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003854 SKIP_SCAN SkScan;
3855 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3856 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003857 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003858 }
3859 }
3860 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3861 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003862 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003863 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003864 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3865 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3866 // 0xM0 followed by 0x0N detected.
3867 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3868 for (int j = i+1; j < SkipScan; j++)
3869 SkipScanIvars[j] = SkipScanIvars[j+1];
3870 --SkipScan;
3871 }
3872 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003873
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003874 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003875 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003876 unsigned char byte;
3877 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3878 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3879 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3880 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003881
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003882 // first skip big.
3883 for (unsigned int ix = 0; ix < skip_big; ix++)
3884 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003885
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003886 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003887 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003888 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003889 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003890 byte |= 0xf;
3891 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003892 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003893 byte |= scan_small;
3894 scan_small = 0;
3895 }
3896 BitMap += byte;
3897 }
3898 // next scan big
3899 for (unsigned int ix = 0; ix < scan_big; ix++)
3900 BitMap += (unsigned char)(0x0f);
3901 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003902 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003903 byte = scan_small;
3904 BitMap += byte;
3905 }
3906 }
3907 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003908 unsigned char zero = 0;
3909 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003910
3911 llvm::GlobalVariable * Entry =
3912 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3913 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3914 "__TEXT,__cstring,cstring_literals",
3915 1, true);
3916 return getConstantGEP(VMContext, Entry, 0, 0);
3917}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003918
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003919/// BuildIvarLayout - Builds ivar layout bitmap for the class
3920/// implementation for the __strong or __weak case.
3921/// The layout map displays which words in ivar list must be skipped
3922/// and which must be scanned by GC (see below). String is built of bytes.
3923/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3924/// of words to skip and right nibble is count of words to scan. So, each
3925/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3926/// represented by a 0x00 byte which also ends the string.
3927/// 1. when ForStrongLayout is true, following ivars are scanned:
3928/// - id, Class
3929/// - object *
3930/// - __strong anything
3931///
3932/// 2. When ForStrongLayout is false, following ivars are scanned:
3933/// - __weak anything
3934///
3935llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3936 const ObjCImplementationDecl *OMD,
3937 bool ForStrongLayout) {
3938 bool hasUnion = false;
3939
3940 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3941 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3942 return llvm::Constant::getNullValue(PtrTy);
3943
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003944 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003945 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003946 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003947
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003948 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003949 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3950 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3951
3952 if (RecFields.empty())
3953 return llvm::Constant::getNullValue(PtrTy);
3954
3955 SkipIvars.clear();
3956 IvarsInfo.clear();
3957
3958 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3959 if (IvarsInfo.empty())
3960 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003961 // Sort on byte position in case we encounterred a union nested in
3962 // the ivar list.
3963 if (hasUnion && !IvarsInfo.empty())
3964 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3965 if (hasUnion && !SkipIvars.empty())
3966 std::sort(SkipIvars.begin(), SkipIvars.end());
3967
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003968 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003969 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003970
3971 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003972 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003973 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003974 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003975 const unsigned char *s = (unsigned char*)BitMap.c_str();
3976 for (unsigned i = 0; i < BitMap.size(); i++)
3977 if (!(s[i] & 0xf0))
3978 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3979 else
3980 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3981 printf("\n");
3982 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003983 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003984}
3985
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003986llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003987 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3988
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003989 // FIXME: Avoid std::string copying.
3990 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003992 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003993 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003994 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003995
Owen Andersona1cf15f2009-07-14 23:10:40 +00003996 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003997}
3998
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003999// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004000llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004001 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4002}
4003
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004004llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004005 std::string TypeStr;
4006 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4007
4008 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004009
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004010 if (!Entry)
4011 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004012 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004013 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004014 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004015
Owen Andersona1cf15f2009-07-14 23:10:40 +00004016 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004017}
4018
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004019llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004020 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00004021 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4022 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00004023
4024 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4025
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004026 if (!Entry)
4027 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004028 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004029 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004030 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004031
Owen Andersona1cf15f2009-07-14 23:10:40 +00004032 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004033}
4034
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004035// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004036llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004037 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004039 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004040 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004041 llvm::ConstantArray::get(VMContext,
4042 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004043 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004044 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004045
Owen Andersona1cf15f2009-07-14 23:10:40 +00004046 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004047}
4048
4049// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004050// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004051llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4053 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004054 std::string TypeStr;
4055 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004056 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4057}
4058
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004059void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004060 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004061 llvm::SmallVectorImpl<char> &Name) {
4062 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004063 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004064 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4065 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004066 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004067 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004068 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004069 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004070}
4071
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004072void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004073 EmitModuleInfo();
4074
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004075 // Emit the dummy bodies for any protocols which were referenced but
4076 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004077 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004078 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4079 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004080 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004081
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004082 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004083 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004084 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004085 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004086 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004087 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004088 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004089 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004090 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004091 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004092 }
4093
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004094 // Add assembler directives to add lazy undefined symbol references
4095 // for classes which are referenced but not defined. This is
4096 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004097 //
4098 // FIXME: It would be nice if we had an LLVM construct for this.
4099 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4100 llvm::SmallString<256> Asm;
4101 Asm += CGM.getModule().getModuleInlineAsm();
4102 if (!Asm.empty() && Asm.back() != '\n')
4103 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004104
Daniel Dunbar33063492009-09-07 00:20:42 +00004105 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004106 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4107 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004108 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4109 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004110 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004111 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004112 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004113 }
4114
4115 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4116 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4117 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4118 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004119
Daniel Dunbar33063492009-09-07 00:20:42 +00004120 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004121 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004122}
4123
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004125 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004126 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004127 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004128 ObjCABI = 2;
4129}
4130
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004131/* *** */
4132
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004133ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004134 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004135 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4136 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004137
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004138 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004139 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004140 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004141 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004142 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004143
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004144 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004145 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004146 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004147
Mike Stumpf5408fe2009-05-16 07:57:57 +00004148 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4149 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004150 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004151 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004153 // I'm not sure I like this. The implicit coordination is a bit
4154 // gross. We should solve this in a reasonable fashion because this
4155 // is a pretty common task (match some runtime data structure with
4156 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004157
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004158 // FIXME: This is leaked.
4159 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004160
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004161 // struct _objc_super {
4162 // id self;
4163 // Class cls;
4164 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004165 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004166 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004167 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004168 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004169 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004170 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004172 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004173 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004175 SuperCTy = Ctx.getTagDeclType(RD);
4176 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004178 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004179 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4180
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004181 // struct _prop_t {
4182 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004183 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004184 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004185 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004186 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004187 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004188
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004189 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004190 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004191 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004192 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004193 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004194 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004195 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004196 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004197 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004199 PropertyListTy);
4200 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004202
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004203 // struct _objc_method {
4204 // SEL _cmd;
4205 // char *method_type;
4206 // char *_imp;
4207 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004208 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004209 Int8PtrTy,
4210 Int8PtrTy,
4211 NULL);
4212 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004213
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004214 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004215 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004216 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004217 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004218}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004219
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004221 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004222 // struct _objc_method_description {
4223 // SEL name;
4224 // char *types;
4225 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004226 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004227 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004228 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004229 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004230 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004231 MethodDescriptionTy);
4232
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004233 // struct _objc_method_description_list {
4234 // int count;
4235 // struct _objc_method_description[1];
4236 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004237 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004238 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004239 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004240 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004241 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004242 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004243
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004244 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004245 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004246 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004247
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004248 // Protocol description structures
4249
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004250 // struct _objc_protocol_extension {
4251 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4252 // struct _objc_method_description_list *optional_instance_methods;
4253 // struct _objc_method_description_list *optional_class_methods;
4254 // struct _objc_property_list *instance_properties;
4255 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004256 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004257 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004258 MethodDescriptionListPtrTy,
4259 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004260 PropertyListPtrTy,
4261 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004262 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004263 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004265 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004266 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004267
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004268 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004269
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004270 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4271 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004272
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004273 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004274 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004275 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004276 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004277 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004278 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004279 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4280
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004281 // struct _objc_protocol {
4282 // struct _objc_protocol_extension *isa;
4283 // char *protocol_name;
4284 // struct _objc_protocol **_objc_protocol_list;
4285 // struct _objc_method_description_list *instance_methods;
4286 // struct _objc_method_description_list *class_methods;
4287 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004288 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004290 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004291 MethodDescriptionListPtrTy,
4292 MethodDescriptionListPtrTy,
4293 NULL);
4294 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4295
4296 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004297 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004298 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004299 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004300 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004301
4302 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004303 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004304 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004305
4306 // Class description structures
4307
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004308 // struct _objc_ivar {
4309 // char *ivar_name;
4310 // char *ivar_type;
4311 // int ivar_offset;
4312 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004313 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314 Int8PtrTy,
4315 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316 NULL);
4317 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4318
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004319 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004320 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004321 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004322 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004324 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004325 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004326 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004327 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004328
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004329 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004331 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004332 Int8PtrTy,
4333 PropertyListPtrTy,
4334 NULL);
4335 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004336 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004337
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004338 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004339
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004340 // struct _objc_class {
4341 // Class isa;
4342 // Class super_class;
4343 // char *name;
4344 // long version;
4345 // long info;
4346 // long instance_size;
4347 // struct _objc_ivar_list *ivars;
4348 // struct _objc_method_list *methods;
4349 // struct _objc_cache *cache;
4350 // struct _objc_protocol_list *protocols;
4351 // char *ivar_layout;
4352 // struct _objc_class_ext *ext;
4353 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004354 T = llvm::StructType::get(VMContext,
4355 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004356 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004357 Int8PtrTy,
4358 LongTy,
4359 LongTy,
4360 LongTy,
4361 IvarListPtrTy,
4362 MethodListPtrTy,
4363 CachePtrTy,
4364 ProtocolListPtrTy,
4365 Int8PtrTy,
4366 ClassExtensionPtrTy,
4367 NULL);
4368 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004370 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4371 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004372 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004373
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004374 // struct _objc_category {
4375 // char *category_name;
4376 // char *class_name;
4377 // struct _objc_method_list *instance_method;
4378 // struct _objc_method_list *class_method;
4379 // uint32_t size; // sizeof(struct _objc_category)
4380 // struct _objc_property_list *instance_properties;// category's @property
4381 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004382 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004383 Int8PtrTy,
4384 MethodListPtrTy,
4385 MethodListPtrTy,
4386 ProtocolListPtrTy,
4387 IntTy,
4388 PropertyListPtrTy,
4389 NULL);
4390 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4391
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004392 // Global metadata structures
4393
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004394 // struct _objc_symtab {
4395 // long sel_ref_cnt;
4396 // SEL *refs;
4397 // short cls_def_cnt;
4398 // short cat_def_cnt;
4399 // char *defs[cls_def_cnt + cat_def_cnt];
4400 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004401 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004402 SelectorPtrTy,
4403 ShortTy,
4404 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004405 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004406 NULL);
4407 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004408 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004409
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004410 // struct _objc_module {
4411 // long version;
4412 // long size; // sizeof(struct _objc_module)
4413 // char *name;
4414 // struct _objc_symtab* symtab;
4415 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004416 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004417 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004418 LongTy,
4419 Int8PtrTy,
4420 SymtabPtrTy,
4421 NULL);
4422 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004423
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004424
Mike Stumpf5408fe2009-05-16 07:57:57 +00004425 // FIXME: This is the size of the setjmp buffer and should be target
4426 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004427 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004428
Anders Carlsson124526b2008-09-09 10:10:21 +00004429 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004430 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004431 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432
4433 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004434 llvm::StructType::get(VMContext,
4435 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4436 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004437 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004439 ExceptionDataTy);
4440
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004441}
4442
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004443ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004444 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004445 // struct _method_list_t {
4446 // uint32_t entsize; // sizeof(struct _objc_method)
4447 // uint32_t method_count;
4448 // struct _objc_method method_list[method_count];
4449 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004450 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004451 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004452 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004453 NULL);
4454 CGM.getModule().addTypeName("struct.__method_list_t",
4455 MethodListnfABITy);
4456 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004457 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004459 // struct _protocol_t {
4460 // id isa; // NULL
4461 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004462 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004463 // const struct method_list_t * const instance_methods;
4464 // const struct method_list_t * const class_methods;
4465 // const struct method_list_t *optionalInstanceMethods;
4466 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004467 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004468 // const uint32_t size; // sizeof(struct _protocol_t)
4469 // const uint32_t flags; // = 0
4470 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004471
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004472 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004473 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474
Owen Anderson47a434f2009-08-05 23:18:46 +00004475 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004476 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004477 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004478 ProtocolListTyHolder),
4479 MethodListnfABIPtrTy,
4480 MethodListnfABIPtrTy,
4481 MethodListnfABIPtrTy,
4482 MethodListnfABIPtrTy,
4483 PropertyListPtrTy,
4484 IntTy,
4485 IntTy,
4486 NULL);
4487 CGM.getModule().addTypeName("struct._protocol_t",
4488 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004489
4490 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004491 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
Fariborz Jahanianda320092009-01-29 19:24:30 +00004493 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004494 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004495 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004496 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004497 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004498 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004499 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004500 NULL);
4501 CGM.getModule().addTypeName("struct._objc_protocol_list",
4502 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004503 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004504 ProtocolListnfABITy);
4505
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004506 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004507 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004508
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004509 // struct _ivar_t {
4510 // unsigned long int *offset; // pointer to ivar offset location
4511 // char *name;
4512 // char *type;
4513 // uint32_t alignment;
4514 // uint32_t size;
4515 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004516 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004517 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004518 Int8PtrTy,
4519 Int8PtrTy,
4520 IntTy,
4521 IntTy,
4522 NULL);
4523 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004525 // struct _ivar_list_t {
4526 // uint32 entsize; // sizeof(struct _ivar_t)
4527 // uint32 count;
4528 // struct _iver_t list[count];
4529 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004530 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004531 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004532 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004533 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004534 NULL);
4535 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
Owen Anderson96e0fc72009-07-29 22:16:19 +00004537 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004538
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004539 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004540 // uint32_t const flags;
4541 // uint32_t const instanceStart;
4542 // uint32_t const instanceSize;
4543 // uint32_t const reserved; // only when building for 64bit targets
4544 // const uint8_t * const ivarLayout;
4545 // const char *const name;
4546 // const struct _method_list_t * const baseMethods;
4547 // const struct _objc_protocol_list *const baseProtocols;
4548 // const struct _ivar_list_t *const ivars;
4549 // const uint8_t * const weakIvarLayout;
4550 // const struct _prop_list_t * const properties;
4551 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004553 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004554 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004555 IntTy,
4556 IntTy,
4557 Int8PtrTy,
4558 Int8PtrTy,
4559 MethodListnfABIPtrTy,
4560 ProtocolListnfABIPtrTy,
4561 IvarListnfABIPtrTy,
4562 Int8PtrTy,
4563 PropertyListPtrTy,
4564 NULL);
4565 CGM.getModule().addTypeName("struct._class_ro_t",
4566 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004568 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4569 std::vector<const llvm::Type*> Params;
4570 Params.push_back(ObjectPtrTy);
4571 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004572 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4574
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004575 // struct _class_t {
4576 // struct _class_t *isa;
4577 // struct _class_t * const superclass;
4578 // void *cache;
4579 // IMP *vtable;
4580 // struct class_ro_t *ro;
4581 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004582
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004583 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004584 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004585 llvm::StructType::get(VMContext,
4586 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587 llvm::PointerType::getUnqual(ClassTyHolder),
4588 CachePtrTy,
4589 llvm::PointerType::getUnqual(ImpnfABITy),
4590 llvm::PointerType::getUnqual(ClassRonfABITy),
4591 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004592 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4593
4594 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004595 ClassnfABITy);
4596
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004597 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004598 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004600 // struct _category_t {
4601 // const char * const name;
4602 // struct _class_t *const cls;
4603 // const struct _method_list_t * const instance_methods;
4604 // const struct _method_list_t * const class_methods;
4605 // const struct _protocol_list_t * const protocols;
4606 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004607 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004608 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004609 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004610 MethodListnfABIPtrTy,
4611 MethodListnfABIPtrTy,
4612 ProtocolListnfABIPtrTy,
4613 PropertyListPtrTy,
4614 NULL);
4615 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004616
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004617 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004618 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4619 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004621 // MessageRefTy - LLVM for:
4622 // struct _message_ref_t {
4623 // IMP messenger;
4624 // SEL name;
4625 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004626
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004627 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004628 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004629 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004630 SourceLocation(),
4631 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004632 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004633 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004634 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004635 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004636 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004637
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004638 MessageRefCTy = Ctx.getTagDeclType(RD);
4639 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4640 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004641
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004642 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004643 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004645 // SuperMessageRefTy - LLVM for:
4646 // struct _super_message_ref_t {
4647 // SUPER_IMP messenger;
4648 // SEL name;
4649 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004650 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004651 SelectorPtrTy,
4652 NULL);
4653 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004654
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004655 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004656 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4657
Daniel Dunbare588b992009-03-01 04:46:24 +00004658
4659 // struct objc_typeinfo {
4660 // const void** vtable; // objc_ehtype_vtable + 2
4661 // const char* name; // c++ typeinfo string
4662 // Class cls;
4663 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004664 EHTypeTy = llvm::StructType::get(VMContext,
4665 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004666 Int8PtrTy,
4667 ClassnfABIPtrTy,
4668 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004669 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004670 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004671}
4672
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004674 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004676 return NULL;
4677}
4678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004679void CGObjCNonFragileABIMac::AddModuleClassList(const
4680 std::vector<llvm::GlobalValue*>
4681 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004682 const char *SymbolName,
4683 const char *SectionName) {
4684 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004685
Daniel Dunbar463b8762009-05-15 21:48:48 +00004686 if (!NumClasses)
4687 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004688
Daniel Dunbar463b8762009-05-15 21:48:48 +00004689 std::vector<llvm::Constant*> Symbols(NumClasses);
4690 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004691 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004692 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004693 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004694 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004695 NumClasses),
4696 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004697
Daniel Dunbar463b8762009-05-15 21:48:48 +00004698 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004699 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004700 llvm::GlobalValue::InternalLinkage,
4701 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004702 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004703 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004704 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004705 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004706}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004708void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4709 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710
Daniel Dunbar463b8762009-05-15 21:48:48 +00004711 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004712 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004713 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004714 "\01L_OBJC_LABEL_CLASS_$",
4715 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004716
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004717 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4718 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4719 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4720 continue;
4721 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004722 }
4723
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004724 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4725 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4726 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4727 continue;
4728 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4729 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004730
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004731 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004732 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4733 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004734
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004735 // Build list of all implemented category addresses in array
4736 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004738 "\01L_OBJC_LABEL_CATEGORY_$",
4739 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004740 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004741 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4742 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004744 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004745}
4746
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004747/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004748/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004749/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004750/// message dispatch call for all the rest.
4751///
4752bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004753 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4754 default:
4755 assert(0 && "Invalid dispatch method!");
4756 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004757 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004758 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004759 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004760 case CodeGenOptions::Mixed:
4761 break;
4762 }
4763
4764 // If so, see whether this selector is in the white-list of things which must
4765 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004766 if (NonLegacyDispatchMethods.empty()) {
4767 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4768 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4769 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4770 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4771 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4772 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4773 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4774 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4775 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4776 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004777
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004778 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4779 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4780 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4781 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4782 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4783 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4784 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4785 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004786 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004787 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4789 &CGM.getContext().Idents.get("objects"),
4790 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004791 };
4792 NonLegacyDispatchMethods.insert(
4793 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004794 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004795
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004796 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004797}
4798
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004799// Metadata flags
4800enum MetaDataDlags {
4801 CLS = 0x0,
4802 CLS_META = 0x1,
4803 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004804 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004805 CLS_EXCEPTION = 0x20
4806};
4807/// BuildClassRoTInitializer - generate meta-data for:
4808/// struct _class_ro_t {
4809/// uint32_t const flags;
4810/// uint32_t const instanceStart;
4811/// uint32_t const instanceSize;
4812/// uint32_t const reserved; // only when building for 64bit targets
4813/// const uint8_t * const ivarLayout;
4814/// const char *const name;
4815/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004816/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004817/// const struct _ivar_list_t *const ivars;
4818/// const uint8_t * const weakIvarLayout;
4819/// const struct _prop_list_t * const properties;
4820/// }
4821///
4822llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004823 unsigned flags,
4824 unsigned InstanceStart,
4825 unsigned InstanceSize,
4826 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004827 std::string ClassName = ID->getNameAsString();
4828 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004829 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4830 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4831 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004832 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004833 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4834 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004835 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004836 // const struct _method_list_t * const baseMethods;
4837 std::vector<llvm::Constant*> Methods;
4838 std::string MethodListName("\01l_OBJC_$_");
4839 if (flags & CLS_META) {
4840 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004841 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004842 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004843 // Class methods should always be defined.
4844 Methods.push_back(GetMethodConstant(*i));
4845 }
4846 } else {
4847 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004849 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004850 // Instance methods should always be defined.
4851 Methods.push_back(GetMethodConstant(*i));
4852 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004854 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004855 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004857 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4858 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004859
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004860 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4861 if (llvm::Constant *C = GetMethodConstant(MD))
4862 Methods.push_back(C);
4863 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4864 if (llvm::Constant *C = GetMethodConstant(MD))
4865 Methods.push_back(C);
4866 }
4867 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004868 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004869 Values[ 5] = EmitMethodList(MethodListName,
4870 "__DATA, __objc_const", Methods);
4871
Fariborz Jahanianda320092009-01-29 19:24:30 +00004872 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4873 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004874 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004875 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004876 OID->all_referenced_protocol_begin(),
4877 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004879 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004880 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004881 else
4882 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004883 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4884 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004885 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004886 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004887 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004888 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4889 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004890 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004891 Values);
4892 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004893 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4894 llvm::GlobalValue::InternalLinkage,
4895 Init,
4896 (flags & CLS_META) ?
4897 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4898 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004899 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004900 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004901 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004902 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004903
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004904}
4905
4906/// BuildClassMetaData - This routine defines that to-level meta-data
4907/// for the given ClassName for:
4908/// struct _class_t {
4909/// struct _class_t *isa;
4910/// struct _class_t * const superclass;
4911/// void *cache;
4912/// IMP *vtable;
4913/// struct class_ro_t *ro;
4914/// }
4915///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004916llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004917 std::string &ClassName,
4918 llvm::Constant *IsAGV,
4919 llvm::Constant *SuperClassGV,
4920 llvm::Constant *ClassRoGV,
4921 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004922 std::vector<llvm::Constant*> Values(5);
4923 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004924 Values[1] = SuperClassGV;
4925 if (!Values[1])
4926 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004927 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4928 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4929 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004930 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004931 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004932 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4933 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004934 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004935 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004936 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004937 if (HiddenVisibility)
4938 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004939 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004940}
4941
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004942bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004943CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004944 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004945}
4946
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004947void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004948 uint32_t &InstanceStart,
4949 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004950 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004951 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004952
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004953 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004954 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004955
4956 // If there are no fields, the start is the same as the end.
4957 if (!RL.getFieldCount())
4958 InstanceStart = InstanceSize;
4959 else
4960 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004961}
4962
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004963void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4964 std::string ClassName = ID->getNameAsString();
4965 if (!ObjCEmptyCacheVar) {
4966 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004967 CGM.getModule(),
4968 ObjCTypes.CacheTy,
4969 false,
4970 llvm::GlobalValue::ExternalLinkage,
4971 0,
4972 "_objc_empty_cache");
4973
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004974 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 CGM.getModule(),
4976 ObjCTypes.ImpnfABITy,
4977 false,
4978 llvm::GlobalValue::ExternalLinkage,
4979 0,
4980 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004981 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004982 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004983 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004984 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004985 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004986 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004987 uint32_t InstanceSize = InstanceStart;
4988 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004989 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4990 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004991
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004992 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993
4994 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004995 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004996 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004997 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004998 if (ID->getNumIvarInitializers())
4999 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005000 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005001 // class is root
5002 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005003 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005004 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005005 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005006 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005007 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5008 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5009 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005010 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005011 if (Root->hasAttr<WeakImportAttr>())
5012 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005013 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005015 ObjCMetaClassName +
5016 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005017 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005018 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5019 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005020 }
5021 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5022 InstanceStart,
5023 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005024 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005026 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5027 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005028 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005029
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005030 // Metadata for the class
5031 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005032 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005033 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005034 if (ID->getNumIvarInitializers())
5035 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005036
Douglas Gregor68584ed2009-06-18 16:11:24 +00005037 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005038 flags |= CLS_EXCEPTION;
5039
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005040 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005041 flags |= CLS_ROOT;
5042 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005043 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005044 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005045 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005046 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005047 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005048 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5049 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005050 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005051 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005052 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005053 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005055 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005056
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005057 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005058 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005059 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5060 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005061 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005062
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005063 // Determine if this class is also "non-lazy".
5064 if (ImplementationIsNonLazy(ID))
5065 DefinedNonLazyClasses.push_back(ClassMD);
5066
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005067 // Force the definition of the EHType if necessary.
5068 if (flags & CLS_EXCEPTION)
5069 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005070}
5071
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005072/// GenerateProtocolRef - This routine is called to generate code for
5073/// a protocol reference expression; as in:
5074/// @code
5075/// @protocol(Proto1);
5076/// @endcode
5077/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5078/// which will hold address of the protocol meta-data.
5079///
5080llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081 const ObjCProtocolDecl *PD) {
5082
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005083 // This routine is called for @protocol only. So, we must build definition
5084 // of protocol's meta-data (not a reference to it!)
5085 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005086 llvm::Constant *Init =
5087 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5088 ObjCTypes.ExternalProtocolPtrTy);
5089
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005090 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005091 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005092
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005093 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5094 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005095 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005096 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005097 CGM.getModule(),
5098 Init->getType(), false,
5099 llvm::GlobalValue::WeakAnyLinkage,
5100 Init,
5101 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005102 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5103 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005104 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005105 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005106}
5107
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005108/// GenerateCategory - Build metadata for a category implementation.
5109/// struct _category_t {
5110/// const char * const name;
5111/// struct _class_t *const cls;
5112/// const struct _method_list_t * const instance_methods;
5113/// const struct _method_list_t * const class_methods;
5114/// const struct _protocol_list_t * const protocols;
5115/// const struct _prop_list_t * const properties;
5116/// }
5117///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005118void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005119 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005120 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005121 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5122 "_$_" + OCD->getNameAsString());
5123 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005124 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005125
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005126 std::vector<llvm::Constant*> Values(6);
5127 Values[0] = GetClassName(OCD->getIdentifier());
5128 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005129 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005130 if (Interface->hasAttr<WeakImportAttr>())
5131 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5132
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005133 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005134 std::vector<llvm::Constant*> Methods;
5135 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005137 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005138
5139 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005140 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005141 // Instance methods should always be defined.
5142 Methods.push_back(GetMethodConstant(*i));
5143 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144
5145 Values[2] = EmitMethodList(MethodListName,
5146 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005147 Methods);
5148
5149 MethodListName = Prefix;
5150 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5151 OCD->getNameAsString();
5152 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005153 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005154 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005155 // Class methods should always be defined.
5156 Methods.push_back(GetMethodConstant(*i));
5157 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005158
5159 Values[3] = EmitMethodList(MethodListName,
5160 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005161 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005162 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005163 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005164 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005165 llvm::SmallString<256> ExtName;
5166 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5167 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005168 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005169 + Interface->getName() + "_$_"
5170 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005171 Category->protocol_begin(),
5172 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005173 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5174 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005175 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005176 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5177 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005178 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005179
5180 llvm::Constant *Init =
5181 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005182 Values);
5183 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005184 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005185 false,
5186 llvm::GlobalValue::InternalLinkage,
5187 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005188 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005189 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005190 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005191 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005192 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005193 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005194
5195 // Determine if this category is also "non-lazy".
5196 if (ImplementationIsNonLazy(OCD))
5197 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005198}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005199
5200/// GetMethodConstant - Return a struct objc_method constant for the
5201/// given method if it has been defined. The result is null if the
5202/// method has not been defined. The return value has type MethodPtrTy.
5203llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005204 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005205 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005206 if (!Fn)
5207 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005208
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005209 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005210 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005211 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005213 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005214 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005215 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005216}
5217
5218/// EmitMethodList - Build meta-data for method declarations
5219/// struct _method_list_t {
5220/// uint32_t entsize; // sizeof(struct _objc_method)
5221/// uint32_t method_count;
5222/// struct _objc_method method_list[method_count];
5223/// }
5224///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005225llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5226 const char *Section,
5227 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005228 // Return null for empty list.
5229 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005230 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005231
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005232 std::vector<llvm::Constant*> Values(3);
5233 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005234 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005235 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005236 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005237 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005238 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005239 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005240 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005241 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005243 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005244 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005245 llvm::GlobalValue::InternalLinkage,
5246 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005247 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005248 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005249 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005250 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005251 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005252 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005253 ObjCTypes.MethodListnfABIPtrTy);
5254}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005255
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005256/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5257/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005258llvm::GlobalVariable *
5259CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5260 const ObjCIvarDecl *Ivar) {
5261 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005262 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005263 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005264 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005265 CGM.getModule().getGlobalVariable(Name);
5266 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005267 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005268 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005269 false,
5270 llvm::GlobalValue::ExternalLinkage,
5271 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005272 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005273 return IvarOffsetGV;
5274}
5275
Daniel Dunbare83be122010-04-02 21:14:02 +00005276llvm::Constant *
5277CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5278 const ObjCIvarDecl *Ivar,
5279 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005280 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005281 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005282 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005283 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005284 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005285
Mike Stumpf5408fe2009-05-16 07:57:57 +00005286 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5287 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005288 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5289 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005290 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005291 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005292 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005293 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005294 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005295 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005296}
5297
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005298/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005299/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005300/// IvarListnfABIPtrTy.
5301/// struct _ivar_t {
5302/// unsigned long int *offset; // pointer to ivar offset location
5303/// char *name;
5304/// char *type;
5305/// uint32_t alignment;
5306/// uint32_t size;
5307/// }
5308/// struct _ivar_list_t {
5309/// uint32 entsize; // sizeof(struct _ivar_t)
5310/// uint32 count;
5311/// struct _iver_t list[count];
5312/// }
5313///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005314
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005315llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316 const ObjCImplementationDecl *ID) {
5317
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005318 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005319
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005320 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5321 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005323 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324
Daniel Dunbar91636d62009-04-20 00:33:43 +00005325 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005326 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005327 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005328
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005329 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5330 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005331 // Ignore unnamed bit-fields.
5332 if (!IVD->getDeclName())
5333 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005335 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005336 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5337 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005338 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005339 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005340 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005341 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005343 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005344 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005345 // NOTE. Size of a bitfield does not match gcc's, because of the
5346 // way bitfields are treated special in each. But I am told that
5347 // 'size' for bitfield ivars is ignored by the runtime so it does
5348 // not matter. If it matters, there is enough info to get the
5349 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005350 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005351 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005352 }
5353 // Return null for empty list.
5354 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005355 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005356 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005357 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005358 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5359 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005360 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005361 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005362 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005363 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005364 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5365 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005366 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005367 llvm::GlobalValue::InternalLinkage,
5368 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005370 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005371 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005372 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005373
Chris Lattnerad64e022009-07-17 23:57:13 +00005374 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005375 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376}
5377
5378llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005379 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005381
Fariborz Jahanianda320092009-01-29 19:24:30 +00005382 if (!Entry) {
5383 // We use the initializer as a marker of whether this is a forward
5384 // reference or not. At module finalization we add the empty
5385 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386 Entry =
5387 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5388 llvm::GlobalValue::ExternalLinkage,
5389 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005390 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005393
Fariborz Jahanianda320092009-01-29 19:24:30 +00005394 return Entry;
5395}
5396
5397/// GetOrEmitProtocol - Generate the protocol meta-data:
5398/// @code
5399/// struct _protocol_t {
5400/// id isa; // NULL
5401/// const char * const protocol_name;
5402/// const struct _protocol_list_t * protocol_list; // super protocols
5403/// const struct method_list_t * const instance_methods;
5404/// const struct method_list_t * const class_methods;
5405/// const struct method_list_t *optionalInstanceMethods;
5406/// const struct method_list_t *optionalClassMethods;
5407/// const struct _prop_list_t * properties;
5408/// const uint32_t size; // sizeof(struct _protocol_t)
5409/// const uint32_t flags; // = 0
5410/// }
5411/// @endcode
5412///
5413
5414llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005415 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417
Fariborz Jahanianda320092009-01-29 19:24:30 +00005418 // Early exit if a defining object has already been generated.
5419 if (Entry && Entry->hasInitializer())
5420 return Entry;
5421
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 // Construct method lists.
5423 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5424 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005426 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005427 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005428 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005429 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5430 OptInstanceMethods.push_back(C);
5431 } else {
5432 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005433 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435
5436 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005437 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005439 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005440 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5441 OptClassMethods.push_back(C);
5442 } else {
5443 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005445 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Fariborz Jahanianda320092009-01-29 19:24:30 +00005447 std::vector<llvm::Constant*> Values(10);
5448 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005449 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005450 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005451 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5452 PD->protocol_begin(),
5453 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005455 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005456 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 "__DATA, __objc_const",
5458 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005460 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005461 "__DATA, __objc_const",
5462 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005463 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005464 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 "__DATA, __objc_const",
5466 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005468 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005469 "__DATA, __objc_const",
5470 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005471 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005472 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005474 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005475 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005476 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005477 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005478 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005479
Fariborz Jahanianda320092009-01-29 19:24:30 +00005480 if (Entry) {
5481 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005482 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005483 Entry->setInitializer(Init);
5484 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005485 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005486 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5487 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5488 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005489 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005490 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005492 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005493 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005494 CGM.AddUsedGlobal(Entry);
5495
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005496 // Use this protocol meta-data to build protocol list table in section
5497 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005498 llvm::GlobalVariable *PTGV =
5499 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5500 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5501 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005502 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005503 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005504 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005505 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005506 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005507 return Entry;
5508}
5509
5510/// EmitProtocolList - Generate protocol list meta-data:
5511/// @code
5512/// struct _protocol_list_t {
5513/// long protocol_count; // Note, this is 32/64 bit
5514/// struct _protocol_t[protocol_count];
5515/// }
5516/// @endcode
5517///
5518llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005519CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5520 ObjCProtocolDecl::protocol_iterator begin,
5521 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005522 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005523
Fariborz Jahanianda320092009-01-29 19:24:30 +00005524 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005526 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005527
Daniel Dunbar948e2582009-02-15 07:36:20 +00005528 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005529 llvm::SmallString<256> TmpName;
5530 Name.toVector(TmpName);
5531 llvm::GlobalVariable *GV =
5532 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005533 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005534 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535
Daniel Dunbar948e2582009-02-15 07:36:20 +00005536 for (; begin != end; ++begin)
5537 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5538
Fariborz Jahanianda320092009-01-29 19:24:30 +00005539 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005540 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541 ObjCTypes.ProtocolnfABIPtrTy));
5542
Fariborz Jahanianda320092009-01-29 19:24:30 +00005543 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005544 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005545 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005547 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005548 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005549 ProtocolRefs.size()),
5550 ProtocolRefs);
5551
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005552 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005553 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005554 llvm::GlobalValue::InternalLinkage,
5555 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005556 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005557 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005558 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005559 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005560 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005561 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005562 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005563}
5564
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005565/// GetMethodDescriptionConstant - This routine build following meta-data:
5566/// struct _objc_method {
5567/// SEL _cmd;
5568/// char *method_type;
5569/// char *_imp;
5570/// }
5571
5572llvm::Constant *
5573CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5574 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005575 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5577 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005578 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005579 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005580 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005581 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005582}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005583
5584/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5585/// This code gen. amounts to generating code for:
5586/// @code
5587/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5588/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005590LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005591 CodeGen::CodeGenFunction &CGF,
5592 QualType ObjectTy,
5593 llvm::Value *BaseValue,
5594 const ObjCIvarDecl *Ivar,
5595 unsigned CVRQualifiers) {
5596 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005597 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5598 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005599}
5600
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005601llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005602 CodeGen::CodeGenFunction &CGF,
5603 const ObjCInterfaceDecl *Interface,
5604 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005605 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005606}
5607
Fariborz Jahanian46551122009-02-04 00:22:57 +00005608CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005610 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611 QualType ResultType,
5612 Selector Sel,
5613 llvm::Value *Receiver,
5614 QualType Arg0Ty,
5615 bool IsSuper,
5616 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005617 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5618 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005619 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005620 llvm::Value *Arg0 = Receiver;
5621 if (!IsSuper)
5622 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005623
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005624 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005625 // FIXME. This is too much work to get the ABI-specific result type needed to
5626 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005627 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005628 = Types.getFunctionInfo(ResultType, CallArgList(),
5629 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005630 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005631 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005632 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCalla1bcc4d2011-02-26 09:12:15 +00005633 CGF.EmitNullInitialization(Return.getValue(), ResultType);
Chris Lattner9b7d6702010-08-18 16:09:06 +00005634 if (IsSuper) {
5635 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5636 Name += "objc_msgSendSuper2_stret_fixup";
5637 } else {
5638 Fn = ObjCTypes.getMessageSendStretFixupFn();
5639 Name += "objc_msgSend_stret_fixup";
5640 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005641 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5642 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5643 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005644 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005645 if (IsSuper) {
5646 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5647 Name += "objc_msgSendSuper2_fixup";
5648 } else {
5649 Fn = ObjCTypes.getMessageSendFixupFn();
5650 Name += "objc_msgSend_fixup";
5651 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005652 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005653 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005654 Name += '_';
5655 std::string SelName(Sel.getAsString());
5656 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005657 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005658 if (SelName[i] == ':')
5659 SelName[i] = '_';
5660 Name += SelName;
5661 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5662 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005663 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005664 std::vector<llvm::Constant*> Values(2);
5665 Values[0] = Fn;
5666 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005667 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005668 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005669 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005670 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005671 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005672 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005673 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005674 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005675 }
5676 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005677
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005678 CallArgList ActualArgs;
5679 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005681 ObjCTypes.MessageRefCPtrTy));
5682 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005683 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005684 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005685 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5686 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005687 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005688 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005689 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005690 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005691}
5692
5693/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005694CodeGen::RValue
5695CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005696 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005697 QualType ResultType,
5698 Selector Sel,
5699 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005700 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005701 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005702 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005703 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005704 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5705 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005707 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005708 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709 Receiver, CGF.getContext().getObjCIdType(),
5710 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005711}
5712
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005713llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005714CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005715 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5716
Daniel Dunbardfff2302009-03-02 05:18:14 +00005717 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005718 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005719 false, llvm::GlobalValue::ExternalLinkage,
5720 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005721 }
5722
5723 return GV;
5724}
5725
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5727 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005728 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005729
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005730 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005731 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005732 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005733 Entry =
5734 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005735 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005736 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005737 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005738 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005739 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005740 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005741 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005742 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005743 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005744
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005745 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005746}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005747
Daniel Dunbar11394522009-04-18 08:51:00 +00005748llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005749CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005750 const ObjCInterfaceDecl *ID) {
5751 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752
Daniel Dunbar11394522009-04-18 08:51:00 +00005753 if (!Entry) {
5754 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5755 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005756 Entry =
5757 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005758 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005760 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005761 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005762 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005764 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005765 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005766 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005767
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005768 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005769}
5770
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005771/// EmitMetaClassRef - Return a Value * of the address of _class_t
5772/// meta-data
5773///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005774llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5775 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005776 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5777 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005778 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005779
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005780 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005781 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005782 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005783 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005784 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005785 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005786 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005787 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005788 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005789 ObjCTypes.ClassnfABIPtrTy));
5790
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005791 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005792 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005793
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005794 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005795}
5796
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005797/// GetClass - Return a reference to the class for the given interface
5798/// decl.
5799llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5800 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005801 if (ID->hasAttr<WeakImportAttr>()) {
5802 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5803 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5804 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5805 }
5806
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005807 return EmitClassRef(Builder, ID);
5808}
5809
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005810/// Generates a message send where the super is the receiver. This is
5811/// a message send to self with special delivery semantics indicating
5812/// which class's method should be called.
5813CodeGen::RValue
5814CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005815 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005816 QualType ResultType,
5817 Selector Sel,
5818 const ObjCInterfaceDecl *Class,
5819 bool isCategoryImpl,
5820 llvm::Value *Receiver,
5821 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005822 const CodeGen::CallArgList &CallArgs,
5823 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005824 // ...
5825 // Create and init a super structure; this is a (receiver, class)
5826 // pair we will pass to objc_msgSendSuper.
5827 llvm::Value *ObjCSuper =
5828 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005829
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005830 llvm::Value *ReceiverAsObject =
5831 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5832 CGF.Builder.CreateStore(ReceiverAsObject,
5833 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005835 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005836 llvm::Value *Target;
5837 if (IsClassMessage) {
5838 if (isCategoryImpl) {
5839 // Message sent to "super' in a class method defined in
5840 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005841 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005842 Target = CGF.Builder.CreateStructGEP(Target, 0);
5843 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005844 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005845 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005846 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005847 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005848
Mike Stumpf5408fe2009-05-16 07:57:57 +00005849 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5850 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005851 const llvm::Type *ClassTy =
5852 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5853 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5854 CGF.Builder.CreateStore(Target,
5855 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005856
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005857 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005858 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5859 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005860 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005861 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005862 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005863 ObjCSuper, ObjCTypes.SuperPtrCTy,
5864 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005865}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005866
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005867llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005868 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005869 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005871 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005872 llvm::Constant *Casted =
5873 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5874 ObjCTypes.SelectorPtrTy);
5875 Entry =
5876 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5877 llvm::GlobalValue::InternalLinkage,
5878 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005879 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005880 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005881 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005882
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005883 if (lval)
5884 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005885 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005886}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005887/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005888/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005889///
5890void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005891 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005892 llvm::Value *dst,
5893 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005894 const llvm::Type * SrcTy = src->getType();
5895 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005896 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005897 assert(Size <= 8 && "does not support size > 8");
5898 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5899 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005900 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5901 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005902 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5903 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005904 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5905 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005906 return;
5907}
5908
5909/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5910/// objc_assign_strongCast (id src, id *dst)
5911///
5912void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005913 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005914 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005915 const llvm::Type * SrcTy = src->getType();
5916 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005917 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005918 assert(Size <= 8 && "does not support size > 8");
5919 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005920 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005921 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5922 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005923 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5924 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005925 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005926 src, dst, "weakassign");
5927 return;
5928}
5929
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005930void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005931 CodeGen::CodeGenFunction &CGF,
5932 llvm::Value *DestPtr,
5933 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005934 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005935 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5936 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005937 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005938 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005939 return;
5940}
5941
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005942/// EmitObjCWeakRead - Code gen for loading value of a __weak
5943/// object: objc_read_weak (id *src)
5944///
5945llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005946 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005947 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005948 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5950 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005951 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005952 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005953 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005954 return read_weak;
5955}
5956
5957/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5958/// objc_assign_weak (id src, id *dst)
5959///
5960void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005961 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005962 const llvm::Type * SrcTy = src->getType();
5963 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005964 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005965 assert(Size <= 8 && "does not support size > 8");
5966 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5967 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005968 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5969 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005970 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5971 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005972 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005973 src, dst, "weakassign");
5974 return;
5975}
5976
5977/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5978/// objc_assign_global (id src, id *dst)
5979///
5980void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005981 llvm::Value *src, llvm::Value *dst,
5982 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005983 const llvm::Type * SrcTy = src->getType();
5984 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005985 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005986 assert(Size <= 8 && "does not support size > 8");
5987 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5988 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005989 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5990 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005991 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5992 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005993 if (!threadlocal)
5994 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5995 src, dst, "globalassign");
5996 else
5997 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5998 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005999 return;
6000}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006001
John McCall740e8072010-07-21 00:41:47 +00006002namespace {
John McCall1f0fca52010-07-21 07:22:38 +00006003 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00006004 llvm::Value *SyncExitFn;
6005 llvm::Value *SyncArg;
6006 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6007 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6008
6009 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6010 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6011 }
6012 };
6013}
6014
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006015void
John McCallf1549f62010-07-06 01:34:17 +00006016CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6017 const ObjCAtSynchronizedStmt &S) {
6018 // Evaluate the lock operand. This should dominate the cleanup.
6019 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006020
John McCallf1549f62010-07-06 01:34:17 +00006021 // Acquire the lock.
6022 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6023 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6024 ->setDoesNotThrow();
6025
6026 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006027 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6028 ObjCTypes.getSyncExitFn(),
6029 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006030
John McCallf1549f62010-07-06 01:34:17 +00006031 // Emit the body of the statement.
6032 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006033
John McCallf1549f62010-07-06 01:34:17 +00006034 // Pop the lock-release cleanup.
6035 CGF.PopCleanupBlock();
6036}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006037
John McCallf1549f62010-07-06 01:34:17 +00006038namespace {
6039 struct CatchHandler {
6040 const VarDecl *Variable;
6041 const Stmt *Body;
6042 llvm::BasicBlock *Block;
6043 llvm::Value *TypeInfo;
6044 };
John McCall8e3f8612010-07-13 22:12:14 +00006045
John McCall1f0fca52010-07-21 07:22:38 +00006046 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006047 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6048 MightThrow(MightThrow), Fn(Fn) {}
6049 bool MightThrow;
6050 llvm::Value *Fn;
6051
6052 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6053 if (!MightThrow) {
6054 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6055 return;
6056 }
6057
6058 CGF.EmitCallOrInvoke(Fn, 0, 0);
6059 }
6060 };
John McCallf1549f62010-07-06 01:34:17 +00006061}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006062
John McCall5a180392010-07-24 00:37:23 +00006063llvm::Constant *
6064CGObjCNonFragileABIMac::GetEHType(QualType T) {
6065 // There's a particular fixed type info for 'id'.
6066 if (T->isObjCIdType() ||
6067 T->isObjCQualifiedIdType()) {
6068 llvm::Constant *IDEHType =
6069 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6070 if (!IDEHType)
6071 IDEHType =
6072 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6073 false,
6074 llvm::GlobalValue::ExternalLinkage,
6075 0, "OBJC_EHTYPE_id");
6076 return IDEHType;
6077 }
6078
6079 // All other types should be Objective-C interface pointer types.
6080 const ObjCObjectPointerType *PT =
6081 T->getAs<ObjCObjectPointerType>();
6082 assert(PT && "Invalid @catch type.");
6083 const ObjCInterfaceType *IT = PT->getInterfaceType();
6084 assert(IT && "Invalid @catch type.");
6085 return GetInterfaceEHType(IT->getDecl(), false);
6086}
6087
John McCallf1549f62010-07-06 01:34:17 +00006088void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6089 const ObjCAtTryStmt &S) {
6090 // Jump destination for falling out of catch bodies.
6091 CodeGenFunction::JumpDest Cont;
6092 if (S.getNumCatchStmts())
6093 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006094
John McCallf1549f62010-07-06 01:34:17 +00006095 CodeGenFunction::FinallyInfo FinallyInfo;
6096 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6097 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6098 ObjCTypes.getObjCBeginCatchFn(),
6099 ObjCTypes.getObjCEndCatchFn(),
6100 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006101
John McCallf1549f62010-07-06 01:34:17 +00006102 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006103
John McCallf1549f62010-07-06 01:34:17 +00006104 // Enter the catch, if there is one.
6105 if (S.getNumCatchStmts()) {
6106 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6107 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006108 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006109
John McCallf1549f62010-07-06 01:34:17 +00006110 Handlers.push_back(CatchHandler());
6111 CatchHandler &Handler = Handlers.back();
6112 Handler.Variable = CatchDecl;
6113 Handler.Body = CatchStmt->getCatchBody();
6114 Handler.Block = CGF.createBasicBlock("catch");
6115
6116 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006117 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006118 Handler.TypeInfo = 0; // catch-all
6119 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006120 break;
6121 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006122
John McCall5a180392010-07-24 00:37:23 +00006123 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006124 }
John McCallf1549f62010-07-06 01:34:17 +00006125
6126 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6127 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6128 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006129 }
John McCallf1549f62010-07-06 01:34:17 +00006130
6131 // Emit the try body.
6132 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006133
John McCallf1549f62010-07-06 01:34:17 +00006134 // Leave the try.
6135 if (S.getNumCatchStmts())
6136 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006137
John McCallf1549f62010-07-06 01:34:17 +00006138 // Remember where we were.
6139 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006140
John McCallf1549f62010-07-06 01:34:17 +00006141 // Emit the handlers.
6142 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6143 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006144
John McCallf1549f62010-07-06 01:34:17 +00006145 CGF.EmitBlock(Handler.Block);
6146 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006147
John McCallf1549f62010-07-06 01:34:17 +00006148 // Enter the catch.
6149 llvm::CallInst *Exn =
6150 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6151 "exn.adjusted");
6152 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006153
John McCallf1549f62010-07-06 01:34:17 +00006154 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006155 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006156 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6157 EndCatchMightThrow,
6158 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006159
John McCallf1549f62010-07-06 01:34:17 +00006160 // Bind the catch parameter if it exists.
6161 if (const VarDecl *CatchParam = Handler.Variable) {
6162 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6163 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006164
John McCallb6bbcc92010-10-15 04:57:14 +00006165 CGF.EmitAutoVarDecl(*CatchParam);
John McCallf1549f62010-07-06 01:34:17 +00006166 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006167 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006168
John McCallf1549f62010-07-06 01:34:17 +00006169 CGF.ObjCEHValueStack.push_back(Exn);
6170 CGF.EmitStmt(Handler.Body);
6171 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006172
John McCallf1549f62010-07-06 01:34:17 +00006173 // Leave the earlier cleanup.
6174 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006175
John McCallf1549f62010-07-06 01:34:17 +00006176 CGF.EmitBranchThroughCleanup(Cont);
6177 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006178
John McCallf1549f62010-07-06 01:34:17 +00006179 // Go back to the try-statement fallthrough.
6180 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006181
John McCallf1549f62010-07-06 01:34:17 +00006182 // Pop out of the normal cleanup on the finally.
6183 if (S.getFinallyStmt())
6184 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006185
John McCallff8e1152010-07-23 21:56:41 +00006186 if (Cont.isValid())
6187 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006188}
6189
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006190/// EmitThrowStmt - Generate code for a throw statement.
6191void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6192 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006193 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006194 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006195 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6196 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00006197 llvm::Value *Args[] = { Exception };
6198 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6199 Args, Args+1)
6200 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006201 } else {
John McCall7ec404c2010-10-16 08:21:07 +00006202 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6203 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006204 }
6205
John McCall7ec404c2010-10-16 08:21:07 +00006206 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006207 CGF.Builder.ClearInsertionPoint();
6208}
Daniel Dunbare588b992009-03-01 04:46:24 +00006209
John McCall5a180392010-07-24 00:37:23 +00006210llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006211CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006212 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006213 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006214
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006215 // If we don't need a definition, return the entry if found or check
6216 // if we use an external reference.
6217 if (!ForDefinition) {
6218 if (Entry)
6219 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006220
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006221 // If this type (or a super class) has the __objc_exception__
6222 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006223 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006224 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006225 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006226 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006227 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006228 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006229 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006230 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006231
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006232 // Otherwise we need to either make a new entry or fill in the
6233 // initializer.
6234 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006235 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006236 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006237 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006238 CGM.getModule().getGlobalVariable(VTableName);
6239 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006240 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6241 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006242 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006243 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006244
Chris Lattner77b89b82010-06-27 07:15:29 +00006245 llvm::Value *VTableIdx =
6246 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006247
6248 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006249 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006250 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006251 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006252 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006253 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006254
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006255 if (Entry) {
6256 Entry->setInitializer(Init);
6257 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006258 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006259 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006260 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006261 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006262 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006263 }
6264
John McCall1fb0caa2010-10-22 21:05:15 +00006265 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006266 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006267 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6268 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006269
6270 if (ForDefinition) {
6271 Entry->setSection("__DATA,__objc_const");
6272 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6273 } else {
6274 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6275 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006276
6277 return Entry;
6278}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006279
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006280/* *** */
6281
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006282CodeGen::CGObjCRuntime *
6283CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006284 return new CGObjCMac(CGM);
6285}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006286
6287CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006288CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006289 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006290}