blob: be7c79cbb5638164396115217bb49dc35d98c78c [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);
134 uint64_t TypeSizeInBits = RL.getSize();
135 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 Dunbardbc933702008-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)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001642 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001643 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001644 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1645 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1646 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001647 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001648 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001649 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001650 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001651 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001652 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001653}
1654
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001655static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1656 if (FQT.isObjCGCStrong())
1657 return Qualifiers::Strong;
1658
1659 if (FQT.isObjCGCWeak())
1660 return Qualifiers::Weak;
1661
1662 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1663 return Qualifiers::Strong;
1664
1665 if (const PointerType *PT = FQT->getAs<PointerType>())
1666 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1667
1668 return Qualifiers::GCNone;
1669}
1670
John McCall6b5a61b2011-02-07 10:33:21 +00001671llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1672 const CGBlockInfo &blockInfo) {
1673 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001674 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001675
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001676 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall6b5a61b2011-02-07 10:33:21 +00001677 return nullPtr;
1678
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001679 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001680 SkipIvars.clear();
1681 IvarsInfo.clear();
1682 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1683 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1684
Fariborz Jahanian81979822010-09-09 00:21:45 +00001685 // __isa is the first field in block descriptor and must assume by runtime's
1686 // convention that it is GC'able.
1687 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001688
1689 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1690
1691 // Calculate the basic layout of the block structure.
1692 const llvm::StructLayout *layout =
1693 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1694
1695 // Ignore the optional 'this' capture: C++ objects are not assumed
1696 // to be GC'ed.
1697
1698 // Walk the captured variables.
1699 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1700 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1701 const VarDecl *variable = ci->getVariable();
1702 QualType type = variable->getType();
1703
1704 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1705
1706 // Ignore constant captures.
1707 if (capture.isConstant()) continue;
1708
1709 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1710
1711 // __block variables are passed by their descriptor address.
1712 if (ci->isByRef()) {
1713 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001714 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001715 }
1716
1717 assert(!type->isArrayType() && "array variable should not be caught");
1718 if (const RecordType *record = type->getAs<RecordType>()) {
1719 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001720 continue;
1721 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001722
John McCall6b5a61b2011-02-07 10:33:21 +00001723 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1724 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1725
1726 if (GCAttr == Qualifiers::Strong)
1727 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1728 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001729 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001730 SkipIvars.push_back(GC_IVAR(fieldOffset,
1731 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001732 }
1733
1734 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001735 return nullPtr;
1736
1737 // Sort on byte position; captures might not be allocated in order,
1738 // and unions can do funny things.
1739 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1740 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001741
1742 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001743 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001744 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1745 printf("\n block variable layout for block: ");
1746 const unsigned char *s = (unsigned char*)BitMap.c_str();
1747 for (unsigned i = 0; i < BitMap.size(); i++)
1748 if (!(s[i] & 0xf0))
1749 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1750 else
1751 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1752 printf("\n");
1753 }
1754
1755 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001756}
1757
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001758llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001759 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001760 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001761 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001762 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1763
Owen Anderson3c4972d2009-07-29 18:54:39 +00001764 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001765 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001766}
1767
Fariborz Jahanianda320092009-01-29 19:24:30 +00001768void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001769 // FIXME: We shouldn't need this, the protocol decl should contain enough
1770 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001771 DefinedProtocols.insert(PD->getIdentifier());
1772
1773 // If we have generated a forward reference to this protocol, emit
1774 // it now. Otherwise do nothing, the protocol objects are lazily
1775 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001776 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001777 GetOrEmitProtocol(PD);
1778}
1779
Fariborz Jahanianda320092009-01-29 19:24:30 +00001780llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001781 if (DefinedProtocols.count(PD->getIdentifier()))
1782 return GetOrEmitProtocol(PD);
1783 return GetOrEmitProtocolRef(PD);
1784}
1785
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001786/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001787// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1788struct _objc_protocol {
1789struct _objc_protocol_extension *isa;
1790char *protocol_name;
1791struct _objc_protocol_list *protocol_list;
1792struct _objc__method_prototype_list *instance_methods;
1793struct _objc__method_prototype_list *class_methods
1794};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001795
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001796See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001797*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001798llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1799 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1800
1801 // Early exit if a defining object has already been generated.
1802 if (Entry && Entry->hasInitializer())
1803 return Entry;
1804
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001805 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001806 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001807 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1808
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001809 // Construct method lists.
1810 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1811 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001812 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001813 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001814 ObjCMethodDecl *MD = *i;
1815 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1816 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1817 OptInstanceMethods.push_back(C);
1818 } else {
1819 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001820 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001821 }
1822
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001823 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001824 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001825 ObjCMethodDecl *MD = *i;
1826 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1827 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1828 OptClassMethods.push_back(C);
1829 } else {
1830 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001831 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001832 }
1833
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001834 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001835 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001836 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001837 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001838 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001839 PD->protocol_begin(),
1840 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001841 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001842 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1844 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001845 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001846 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001847 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1848 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001849 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001850 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001851
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001852 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001853 // Already created, fix the linkage and update the initializer.
1854 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001855 Entry->setInitializer(Init);
1856 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001857 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001858 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001859 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001860 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001861 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001862 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863 // FIXME: Is this necessary? Why only for protocol?
1864 Entry->setAlignment(4);
1865 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001866 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001867
1868 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869}
1870
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001871llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001872 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1873
1874 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001875 // We use the initializer as a marker of whether this is a forward
1876 // reference or not. At module finalization we add the empty
1877 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001878 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001879 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001880 llvm::GlobalValue::ExternalLinkage,
1881 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001882 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001883 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 // FIXME: Is this necessary? Why only for protocol?
1885 Entry->setAlignment(4);
1886 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001887
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001888 return Entry;
1889}
1890
1891/*
1892 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001893 uint32_t size;
1894 struct objc_method_description_list *optional_instance_methods;
1895 struct objc_method_description_list *optional_class_methods;
1896 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001897 };
1898*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001899llvm::Constant *
1900CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1901 const ConstantVector &OptInstanceMethods,
1902 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001903 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001904 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001905 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001906 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 Values[1] =
1908 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001909 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001910 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1911 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001912 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001913 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001914 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1915 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001916 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001917 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001918
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001919 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001920 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001921 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001922 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001924 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001925 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001926
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001927 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001928 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001929 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001930 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001931}
1932
1933/*
1934 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001935 struct objc_protocol_list *next;
1936 long count;
1937 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001938 };
1939*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001940llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001941CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001942 ObjCProtocolDecl::protocol_iterator begin,
1943 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001944 std::vector<llvm::Constant*> ProtocolRefs;
1945
Daniel Dunbardbc933702008-08-21 21:57:41 +00001946 for (; begin != end; ++begin)
1947 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001948
1949 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001951 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001952
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001953 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001954 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001955
1956 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001957 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001958 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001959 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001960 ProtocolRefs.size() - 1);
1961 Values[2] =
1962 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1963 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001964 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001965
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001966 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001967 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001968 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001969 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001970 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001971}
1972
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001973void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1974 std::vector<llvm::Constant*> &Properties,
1975 const Decl *Container,
1976 const ObjCProtocolDecl *PROTO,
1977 const ObjCCommonTypesHelper &ObjCTypes) {
1978 std::vector<llvm::Constant*> Prop(2);
1979 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1980 E = PROTO->protocol_end(); P != E; ++P)
1981 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1982 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1983 E = PROTO->prop_end(); I != E; ++I) {
1984 const ObjCPropertyDecl *PD = *I;
1985 if (!PropertySet.insert(PD->getIdentifier()))
1986 continue;
1987 Prop[0] = GetPropertyName(PD->getIdentifier());
1988 Prop[1] = GetPropertyTypeString(PD, Container);
1989 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1990 }
1991}
1992
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001993/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001994 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001995 const char * const name;
1996 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001997 };
1998
1999 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002000 uint32_t entsize; // sizeof (struct _objc_property)
2001 uint32_t prop_count;
2002 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002003 };
2004*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002005llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002006 const Decl *Container,
2007 const ObjCContainerDecl *OCD,
2008 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002009 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002010 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002011 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2012 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002013 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002014 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002015 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002016 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002017 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002018 Prop));
2019 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002020 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002021 for (ObjCInterfaceDecl::all_protocol_iterator
2022 P = OID->all_referenced_protocol_begin(),
2023 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002024 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2025 ObjCTypes);
2026 }
2027 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2028 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2029 E = CD->protocol_end(); P != E; ++P)
2030 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2031 ObjCTypes);
2032 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002033
2034 // Return null for empty list.
2035 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002036 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002037
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002038 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002039 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002040 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002041 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2042 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002043 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002044 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002045 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002046 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002047
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002048 llvm::GlobalVariable *GV =
2049 CreateMetadataVar(Name, Init,
2050 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002051 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002052 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002053 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002054 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002055}
2056
2057/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002058 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002059 int count;
2060 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002061 };
2062*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002063llvm::Constant *
2064CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2065 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002066 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002067 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2068 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002069 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002070 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002071 Desc);
2072}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002073
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002074llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002075 const char *Section,
2076 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002077 // Return null for empty list.
2078 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002079 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002080
2081 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002082 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002083 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002084 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002085 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002086 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002087
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002088 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002089 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002090 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002091}
2092
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002093/*
2094 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002095 char *category_name;
2096 char *class_name;
2097 struct _objc_method_list *instance_methods;
2098 struct _objc_method_list *class_methods;
2099 struct _objc_protocol_list *protocols;
2100 uint32_t size; // <rdar://4585769>
2101 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002102 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002104void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002105 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002106
Mike Stumpf5408fe2009-05-16 07:57:57 +00002107 // FIXME: This is poor design, the OCD should have a pointer to the category
2108 // decl. Additionally, note that Category can be null for the @implementation
2109 // w/o an @interface case. Sema should just create one for us as it does for
2110 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002111 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002112 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002113 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002114
2115 llvm::SmallString<256> ExtName;
2116 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2117 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002118
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002119 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002121 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002122 // Instance methods should always be defined.
2123 InstanceMethods.push_back(GetMethodConstant(*i));
2124 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002125 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002126 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002127 // Class methods should always be defined.
2128 ClassMethods.push_back(GetMethodConstant(*i));
2129 }
2130
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002131 std::vector<llvm::Constant*> Values(7);
2132 Values[0] = GetClassName(OCD->getIdentifier());
2133 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002134 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002135 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002136 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002137 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002138 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002139 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002140 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002141 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002142 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002143 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002144 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002145 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002146 Category->protocol_begin(),
2147 Category->protocol_end());
2148 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002149 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002150 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002151 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002152
2153 // If there is no category @interface then there can be no properties.
2154 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002155 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002156 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002157 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002158 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002159 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002160
Owen Anderson08e25242009-07-27 22:29:56 +00002161 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002162 Values);
2163
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002164 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002165 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002166 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002167 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002168 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002169 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002170}
2171
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002172// FIXME: Get from somewhere?
2173enum ClassFlags {
2174 eClassFlags_Factory = 0x00001,
2175 eClassFlags_Meta = 0x00002,
2176 // <rdr://5142207>
2177 eClassFlags_HasCXXStructors = 0x02000,
2178 eClassFlags_Hidden = 0x20000,
2179 eClassFlags_ABI2_Hidden = 0x00010,
2180 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2181};
2182
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002183/*
2184 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002185 Class isa;
2186 Class super_class;
2187 const char *name;
2188 long version;
2189 long info;
2190 long instance_size;
2191 struct _objc_ivar_list *ivars;
2192 struct _objc_method_list *methods;
2193 struct _objc_cache *cache;
2194 struct _objc_protocol_list *protocols;
2195 // Objective-C 1.0 extensions (<rdr://4585769>)
2196 const char *ivar_layout;
2197 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002198 };
2199
2200 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002201*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002202void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002203 DefinedSymbols.insert(ID->getIdentifier());
2204
Chris Lattner8ec03f52008-11-24 03:54:41 +00002205 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002207 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002209 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002210 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002211 Interface->all_referenced_protocol_begin(),
2212 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002213 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002214 if (ID->getNumIvarInitializers())
2215 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002216 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002217 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002218
2219 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002220 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 Flags |= eClassFlags_Hidden;
2222
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002223 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002224 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002225 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002226 // Instance methods should always be defined.
2227 InstanceMethods.push_back(GetMethodConstant(*i));
2228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002229 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002230 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002231 // Class methods should always be defined.
2232 ClassMethods.push_back(GetMethodConstant(*i));
2233 }
2234
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002235 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002236 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002237 ObjCPropertyImplDecl *PID = *i;
2238
2239 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2240 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2241
2242 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2243 if (llvm::Constant *C = GetMethodConstant(MD))
2244 InstanceMethods.push_back(C);
2245 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2246 if (llvm::Constant *C = GetMethodConstant(MD))
2247 InstanceMethods.push_back(C);
2248 }
2249 }
2250
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002252 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002253 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002254 // Record a reference to the super class.
2255 LazySymbols.insert(Super->getIdentifier());
2256
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002257 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002258 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002259 ObjCTypes.ClassPtrTy);
2260 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002261 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002262 }
2263 Values[ 2] = GetClassName(ID->getIdentifier());
2264 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002265 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2266 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2267 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002268 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002269 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002270 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002271 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002272 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002274 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002275 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002276 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002278 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002279 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002280 std::string Name("\01L_OBJC_CLASS_");
2281 Name += ClassName;
2282 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2283 // Check for a forward reference.
2284 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2285 if (GV) {
2286 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2287 "Forward metaclass reference has incorrect type.");
2288 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2289 GV->setInitializer(Init);
2290 GV->setSection(Section);
2291 GV->setAlignment(4);
2292 CGM.AddUsedGlobal(GV);
2293 }
2294 else
2295 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 DefinedClasses.push_back(GV);
2297}
2298
2299llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2300 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002301 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002302 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002303 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002304
John McCall1fb0caa2010-10-22 21:05:15 +00002305 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002307
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308 std::vector<llvm::Constant*> Values(12);
2309 // The isa for the metaclass is the root of the hierarchy.
2310 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2311 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2312 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002313 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002314 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002316 // The super class for the metaclass is emitted as the name of the
2317 // super class. The runtime fixes this up to point to the
2318 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002319 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002320 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002321 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002322 ObjCTypes.ClassPtrTy);
2323 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002324 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325 }
2326 Values[ 2] = GetClassName(ID->getIdentifier());
2327 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002328 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2329 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2330 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002331 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002332 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002333 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002334 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002335 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002337 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002338 Values[ 9] = Protocols;
2339 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002340 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002341 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002342 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002343 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002344 Values);
2345
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002346 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002347 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002348
2349 // Check for a forward reference.
2350 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2351 if (GV) {
2352 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2353 "Forward metaclass reference has incorrect type.");
2354 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2355 GV->setInitializer(Init);
2356 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002357 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002358 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002359 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002360 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002361 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002362 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002363 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002364
2365 return GV;
2366}
2367
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002368llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002369 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002370
Mike Stumpf5408fe2009-05-16 07:57:57 +00002371 // FIXME: Should we look these up somewhere other than the module. Its a bit
2372 // silly since we only generate these while processing an implementation, so
2373 // exactly one pointer would work if know when we entered/exitted an
2374 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002375
2376 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002377 // Previously, metaclass with internal linkage may have been defined.
2378 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002379 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2380 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002381 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2382 "Forward metaclass reference has incorrect type.");
2383 return GV;
2384 } else {
2385 // Generate as an external reference to keep a consistent
2386 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002387 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002388 llvm::GlobalValue::ExternalLinkage,
2389 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002390 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002391 }
2392}
2393
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002394llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2395 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2396
2397 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2398 true)) {
2399 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2400 "Forward class metadata reference has incorrect type.");
2401 return GV;
2402 } else {
2403 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2404 llvm::GlobalValue::ExternalLinkage,
2405 0,
2406 Name);
2407 }
2408}
2409
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002410/*
2411 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412 uint32_t size;
2413 const char *weak_ivar_layout;
2414 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002415 };
2416*/
2417llvm::Constant *
2418CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002419 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002420 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421
2422 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002423 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002424 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002425 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002426 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427
2428 // Return null if no extension bits are used.
2429 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002430 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002432 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002433 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002434 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002435 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002436 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002437}
2438
2439/*
2440 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002441 char *ivar_name;
2442 char *ivar_type;
2443 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 };
2445
2446 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002447 int ivar_count;
2448 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002449 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002450*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002452 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453 std::vector<llvm::Constant*> Ivars, Ivar(3);
2454
2455 // When emitting the root class GCC emits ivar entries for the
2456 // actual class structure. It is not clear if we need to follow this
2457 // behavior; for now lets try and get away with not doing it. If so,
2458 // the cleanest solution would be to make up an ObjCInterfaceDecl
2459 // for the class.
2460 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002461 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002462
2463 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002464 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002465
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002466 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002467 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002469 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2470 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002471 // Ignore unnamed bit-fields.
2472 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002473 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002474 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2475 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002476 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002477 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002478 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 }
2480
2481 // Return null for empty list.
2482 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002483 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
2485 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002486 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002487 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002489 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002490 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002491
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002492 llvm::GlobalVariable *GV;
2493 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002494 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002495 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002496 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002497 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002498 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002499 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002500 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002501 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002502}
2503
2504/*
2505 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002506 SEL method_name;
2507 char *method_types;
2508 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002509 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002510
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002511 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002512 struct objc_method_list *obsolete;
2513 int count;
2514 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002515 };
2516*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002517
2518/// GetMethodConstant - Return a struct objc_method constant for the
2519/// given method if it has been defined. The result is null if the
2520/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002521llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002522 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002523 if (!Fn)
2524 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002525
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002526 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002527 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002528 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002529 ObjCTypes.SelectorPtrTy);
2530 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002531 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002532 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002533}
2534
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002535llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002536 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002537 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002538 // Return null for empty list.
2539 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002540 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002541
2542 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002543 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002544 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002545 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002546 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002547 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002548 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002549
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002550 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002551 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002552 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002553}
2554
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002555llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002556 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002557 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002558 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002559
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002560 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002561 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002562 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002563 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002564 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002565 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002566 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002567 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002568 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002569
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002570 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002571}
2572
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002573llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002574CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002575 llvm::Constant *Init,
2576 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002577 unsigned Align,
2578 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002579 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002580 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002581 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002582 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002583 if (Section)
2584 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002585 if (Align)
2586 GV->setAlignment(Align);
2587 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002588 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002589 return GV;
2590}
2591
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002592llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002593 // Abuse this interface function as a place to finalize.
2594 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002595 return NULL;
2596}
2597
Chris Lattner74391b42009-03-22 21:03:39 +00002598llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002599 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002600}
2601
Chris Lattner74391b42009-03-22 21:03:39 +00002602llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002603 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002604}
2605
David Chisnall8fac25d2010-12-26 22:13:16 +00002606llvm::Constant *CGObjCMac::GetGetStructFunction() {
2607 return ObjCTypes.getCopyStructFn();
2608}
2609llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002610 return ObjCTypes.getCopyStructFn();
2611}
2612
Chris Lattner74391b42009-03-22 21:03:39 +00002613llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002614 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002615}
2616
John McCallf1549f62010-07-06 01:34:17 +00002617void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2618 return EmitTryOrSynchronizedStmt(CGF, S);
2619}
2620
2621void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2622 const ObjCAtSynchronizedStmt &S) {
2623 return EmitTryOrSynchronizedStmt(CGF, S);
2624}
2625
John McCallcc505292010-07-21 06:59:36 +00002626namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002627 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002628 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002629 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002630 llvm::Value *CallTryExitVar;
2631 llvm::Value *ExceptionData;
2632 ObjCTypesHelper &ObjCTypes;
2633 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002634 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002635 llvm::Value *CallTryExitVar,
2636 llvm::Value *ExceptionData,
2637 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002638 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002639 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2640
2641 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2642 // Check whether we need to call objc_exception_try_exit.
2643 // In optimized code, this branch will always be folded.
2644 llvm::BasicBlock *FinallyCallExit =
2645 CGF.createBasicBlock("finally.call_exit");
2646 llvm::BasicBlock *FinallyNoCallExit =
2647 CGF.createBasicBlock("finally.no_call_exit");
2648 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2649 FinallyCallExit, FinallyNoCallExit);
2650
2651 CGF.EmitBlock(FinallyCallExit);
2652 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2653 ->setDoesNotThrow();
2654
2655 CGF.EmitBlock(FinallyNoCallExit);
2656
2657 if (isa<ObjCAtTryStmt>(S)) {
2658 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002659 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2660 // Save the current cleanup destination in case there's
2661 // control flow inside the finally statement.
2662 llvm::Value *CurCleanupDest =
2663 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2664
John McCallcc505292010-07-21 06:59:36 +00002665 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2666
John McCalld96a8e72010-08-11 00:16:14 +00002667 if (CGF.HaveInsertPoint()) {
2668 CGF.Builder.CreateStore(CurCleanupDest,
2669 CGF.getNormalCleanupDestSlot());
2670 } else {
2671 // Currently, the end of the cleanup must always exist.
2672 CGF.EnsureInsertPoint();
2673 }
2674 }
John McCallcc505292010-07-21 06:59:36 +00002675 } else {
2676 // Emit objc_sync_exit(expr); as finally's sole statement for
2677 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002678 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002679 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2680 ->setDoesNotThrow();
2681 }
2682 }
2683 };
John McCall87bb5822010-07-31 23:20:56 +00002684
2685 class FragileHazards {
2686 CodeGenFunction &CGF;
2687 llvm::SmallVector<llvm::Value*, 20> Locals;
2688 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2689
2690 llvm::InlineAsm *ReadHazard;
2691 llvm::InlineAsm *WriteHazard;
2692
2693 llvm::FunctionType *GetAsmFnType();
2694
2695 void collectLocals();
2696 void emitReadHazard(CGBuilderTy &Builder);
2697
2698 public:
2699 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002700
John McCall87bb5822010-07-31 23:20:56 +00002701 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002702 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002703 };
2704}
2705
2706/// Create the fragile-ABI read and write hazards based on the current
2707/// state of the function, which is presumed to be immediately prior
2708/// to a @try block. These hazards are used to maintain correct
2709/// semantics in the face of optimization and the fragile ABI's
2710/// cavalier use of setjmp/longjmp.
2711FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2712 collectLocals();
2713
2714 if (Locals.empty()) return;
2715
2716 // Collect all the blocks in the function.
2717 for (llvm::Function::iterator
2718 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2719 BlocksBeforeTry.insert(&*I);
2720
2721 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2722
2723 // Create a read hazard for the allocas. This inhibits dead-store
2724 // optimizations and forces the values to memory. This hazard is
2725 // inserted before any 'throwing' calls in the protected scope to
2726 // reflect the possibility that the variables might be read from the
2727 // catch block if the call throws.
2728 {
2729 std::string Constraint;
2730 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2731 if (I) Constraint += ',';
2732 Constraint += "*m";
2733 }
2734
2735 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2736 }
2737
2738 // Create a write hazard for the allocas. This inhibits folding
2739 // loads across the hazard. This hazard is inserted at the
2740 // beginning of the catch path to reflect the possibility that the
2741 // variables might have been written within the protected scope.
2742 {
2743 std::string Constraint;
2744 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2745 if (I) Constraint += ',';
2746 Constraint += "=*m";
2747 }
2748
2749 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2750 }
2751}
2752
2753/// Emit a write hazard at the current location.
2754void FragileHazards::emitWriteHazard() {
2755 if (Locals.empty()) return;
2756
2757 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2758 ->setDoesNotThrow();
2759}
2760
John McCall87bb5822010-07-31 23:20:56 +00002761void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2762 assert(!Locals.empty());
2763 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2764 ->setDoesNotThrow();
2765}
2766
2767/// Emit read hazards in all the protected blocks, i.e. all the blocks
2768/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002769void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002770 if (Locals.empty()) return;
2771
2772 CGBuilderTy Builder(CGF.getLLVMContext());
2773
2774 // Iterate through all blocks, skipping those prior to the try.
2775 for (llvm::Function::iterator
2776 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2777 llvm::BasicBlock &BB = *FI;
2778 if (BlocksBeforeTry.count(&BB)) continue;
2779
2780 // Walk through all the calls in the block.
2781 for (llvm::BasicBlock::iterator
2782 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2783 llvm::Instruction &I = *BI;
2784
2785 // Ignore instructions that aren't non-intrinsic calls.
2786 // These are the only calls that can possibly call longjmp.
2787 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2788 if (isa<llvm::IntrinsicInst>(I))
2789 continue;
2790
2791 // Ignore call sites marked nounwind. This may be questionable,
2792 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2793 llvm::CallSite CS(&I);
2794 if (CS.doesNotThrow()) continue;
2795
John McCall0b251722010-08-04 05:59:32 +00002796 // Insert a read hazard before the call. This will ensure that
2797 // any writes to the locals are performed before making the
2798 // call. If the call throws, then this is sufficient to
2799 // guarantee correctness as long as it doesn't also write to any
2800 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002801 Builder.SetInsertPoint(&BB, BI);
2802 emitReadHazard(Builder);
2803 }
2804 }
2805}
2806
2807static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2808 if (V) S.insert(V);
2809}
2810
2811void FragileHazards::collectLocals() {
2812 // Compute a set of allocas to ignore.
2813 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2814 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2815 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2816 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2817
2818 // Collect all the allocas currently in the function. This is
2819 // probably way too aggressive.
2820 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2821 for (llvm::BasicBlock::iterator
2822 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2823 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2824 Locals.push_back(&*I);
2825}
2826
2827llvm::FunctionType *FragileHazards::GetAsmFnType() {
2828 std::vector<const llvm::Type *> Tys(Locals.size());
2829 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2830 Tys[I] = Locals[I]->getType();
2831 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002832}
2833
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002834/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002835
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002836 Objective-C setjmp-longjmp (sjlj) Exception Handling
2837 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002838
John McCallf1549f62010-07-06 01:34:17 +00002839 A catch buffer is a setjmp buffer plus:
2840 - a pointer to the exception that was caught
2841 - a pointer to the previous exception data buffer
2842 - two pointers of reserved storage
2843 Therefore catch buffers form a stack, with a pointer to the top
2844 of the stack kept in thread-local storage.
2845
2846 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2847 objc_exception_try_exit pops the given catch buffer, which is
2848 required to be the top of the EH stack.
2849 objc_exception_throw pops the top of the EH stack, writes the
2850 thrown exception into the appropriate field, and longjmps
2851 to the setjmp buffer. It crashes the process (with a printf
2852 and an abort()) if there are no catch buffers on the stack.
2853 objc_exception_extract just reads the exception pointer out of the
2854 catch buffer.
2855
2856 There's no reason an implementation couldn't use a light-weight
2857 setjmp here --- something like __builtin_setjmp, but API-compatible
2858 with the heavyweight setjmp. This will be more important if we ever
2859 want to implement correct ObjC/C++ exception interactions for the
2860 fragile ABI.
2861
2862 Note that for this use of setjmp/longjmp to be correct, we may need
2863 to mark some local variables volatile: if a non-volatile local
2864 variable is modified between the setjmp and the longjmp, it has
2865 indeterminate value. For the purposes of LLVM IR, it may be
2866 sufficient to make loads and stores within the @try (to variables
2867 declared outside the @try) volatile. This is necessary for
2868 optimized correctness, but is not currently being done; this is
2869 being tracked as rdar://problem/8160285
2870
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002871 The basic framework for a @try-catch-finally is as follows:
2872 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002873 objc_exception_data d;
2874 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002875 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002876
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002877 objc_exception_try_enter(&d);
2878 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002879 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002880 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002881 // exception path
2882 id _caught = objc_exception_extract(&d);
2883
2884 // enter new try scope for handlers
2885 if (!setjmp(d.jmp_buf)) {
2886 ... match exception and execute catch blocks ...
2887
2888 // fell off end, rethrow.
2889 _rethrow = _caught;
2890 ... jump-through-finally to finally_rethrow ...
2891 } else {
2892 // exception in catch block
2893 _rethrow = objc_exception_extract(&d);
2894 _call_try_exit = false;
2895 ... jump-through-finally to finally_rethrow ...
2896 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002897 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002898 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002899
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002901 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002902 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002903
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002904 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002905 ... dispatch to finally destination ...
2906
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002907 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002908 objc_exception_throw(_rethrow);
2909
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002910 finally_end:
2911 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002912
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002913 This framework differs slightly from the one gcc uses, in that gcc
2914 uses _rethrow to determine if objc_exception_try_exit should be called
2915 and if the object should be rethrown. This breaks in the face of
2916 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002917
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002918 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002919
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002920 - If there are no catch blocks, then we avoid emitting the second
2921 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002922
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002923 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2924 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002925
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002926 - FIXME: If there is no @finally block we can do a few more
2927 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002928
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002929 Rethrows and Jumps-Through-Finally
2930 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002931
John McCallf1549f62010-07-06 01:34:17 +00002932 '@throw;' is supported by pushing the currently-caught exception
2933 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002934
John McCallf1549f62010-07-06 01:34:17 +00002935 Branches through the @finally block are handled with an ordinary
2936 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2937 exceptions are not compatible with C++ exceptions, and this is
2938 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002939
John McCallf1549f62010-07-06 01:34:17 +00002940 @synchronized(expr) { stmt; } is emitted as if it were:
2941 id synch_value = expr;
2942 objc_sync_enter(synch_value);
2943 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002944*/
2945
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002946void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2947 const Stmt &S) {
2948 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002949
2950 // A destination for the fall-through edges of the catch handlers to
2951 // jump to.
2952 CodeGenFunction::JumpDest FinallyEnd =
2953 CGF.getJumpDestInCurrentScope("finally.end");
2954
2955 // A destination for the rethrow edge of the catch handlers to jump
2956 // to.
2957 CodeGenFunction::JumpDest FinallyRethrow =
2958 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002959
Daniel Dunbar1c566672009-02-24 01:43:46 +00002960 // For @synchronized, call objc_sync_enter(sync.expr). The
2961 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002962 // @synchronized. We can't avoid a temp here because we need the
2963 // value to be preserved. If the backend ever does liveness
2964 // correctly after setjmp, this will be unnecessary.
2965 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002966 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002967 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002968 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2969 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002970 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2971 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002972
2973 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2974 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002975 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002976
John McCall0b251722010-08-04 05:59:32 +00002977 // Allocate memory for the setjmp buffer. This needs to be kept
2978 // live throughout the try and catch blocks.
2979 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2980 "exceptiondata.ptr");
2981
John McCall87bb5822010-07-31 23:20:56 +00002982 // Create the fragile hazards. Note that this will not capture any
2983 // of the allocas required for exception processing, but will
2984 // capture the current basic block (which extends all the way to the
2985 // setjmp call) as "before the @try".
2986 FragileHazards Hazards(CGF);
2987
John McCallf1549f62010-07-06 01:34:17 +00002988 // Create a flag indicating whether the cleanup needs to call
2989 // objc_exception_try_exit. This is true except when
2990 // - no catches match and we're branching through the cleanup
2991 // just to rethrow the exception, or
2992 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002993 // The setjmp-safety rule here is that we should always store to this
2994 // variable in a place that dominates the branch through the cleanup
2995 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002996 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002997 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002998
John McCall9e2213d2010-10-04 23:42:51 +00002999 // A slot containing the exception to rethrow. Only needed when we
3000 // have both a @catch and a @finally.
3001 llvm::Value *PropagatingExnVar = 0;
3002
John McCallf1549f62010-07-06 01:34:17 +00003003 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003004 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003005 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003006 CallTryExitVar,
3007 ExceptionData,
3008 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003009
3010 // Enter a try block:
3011 // - Call objc_exception_try_enter to push ExceptionData on top of
3012 // the EH stack.
3013 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3014 ->setDoesNotThrow();
3015
3016 // - Call setjmp on the exception data buffer.
3017 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3018 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3019 llvm::Value *SetJmpBuffer =
3020 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3021 llvm::CallInst *SetJmpResult =
3022 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3023 SetJmpResult->setDoesNotThrow();
3024
3025 // If setjmp returned 0, enter the protected block; otherwise,
3026 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003027 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3028 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003029 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003030 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3031 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003032
John McCallf1549f62010-07-06 01:34:17 +00003033 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003034 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003035 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003036 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003037 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003038
3039 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003040
John McCallf1549f62010-07-06 01:34:17 +00003041 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003042 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003043
John McCall87bb5822010-07-31 23:20:56 +00003044 // Don't optimize loads of the in-scope locals across this point.
3045 Hazards.emitWriteHazard();
3046
John McCallf1549f62010-07-06 01:34:17 +00003047 // For a @synchronized (or a @try with no catches), just branch
3048 // through the cleanup to the rethrow block.
3049 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3050 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003051 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003052 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003053
3054 // Otherwise, we have to match against the caught exceptions.
3055 } else {
John McCall0b251722010-08-04 05:59:32 +00003056 // Retrieve the exception object. We may emit multiple blocks but
3057 // nothing can cross this so the value is already in SSA form.
3058 llvm::CallInst *Caught =
3059 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3060 ExceptionData, "caught");
3061 Caught->setDoesNotThrow();
3062
John McCallf1549f62010-07-06 01:34:17 +00003063 // Push the exception to rethrow onto the EH value stack for the
3064 // benefit of any @throws in the handlers.
3065 CGF.ObjCEHValueStack.push_back(Caught);
3066
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003067 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003068
John McCall0b251722010-08-04 05:59:32 +00003069 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003070
John McCall0b251722010-08-04 05:59:32 +00003071 llvm::BasicBlock *CatchBlock = 0;
3072 llvm::BasicBlock *CatchHandler = 0;
3073 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003074 // Save the currently-propagating exception before
3075 // objc_exception_try_enter clears the exception slot.
3076 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3077 "propagating_exception");
3078 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3079
John McCall0b251722010-08-04 05:59:32 +00003080 // Enter a new exception try block (in case a @catch block
3081 // throws an exception).
3082 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3083 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003084
John McCall0b251722010-08-04 05:59:32 +00003085 llvm::CallInst *SetJmpResult =
3086 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3087 "setjmp.result");
3088 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003089
John McCall0b251722010-08-04 05:59:32 +00003090 llvm::Value *Threw =
3091 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3092
3093 CatchBlock = CGF.createBasicBlock("catch");
3094 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3095 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3096
3097 CGF.EmitBlock(CatchBlock);
3098 }
3099
3100 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003101
Daniel Dunbar55e40722008-09-27 07:03:52 +00003102 // Handle catch list. As a special case we check if everything is
3103 // matched and avoid generating code for falling off the end if
3104 // so.
3105 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003106 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3107 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003108
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003109 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003110 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003111
Anders Carlsson80f25672008-09-09 17:59:25 +00003112 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003113 if (!CatchParam) {
3114 AllMatched = true;
3115 } else {
John McCall183700f2009-09-21 23:43:11 +00003116 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003117
John McCallf1549f62010-07-06 01:34:17 +00003118 // catch(id e) always matches under this ABI, since only
3119 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003120 // FIXME: For the time being we also match id<X>; this should
3121 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003122 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003123 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003124 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003125
John McCallf1549f62010-07-06 01:34:17 +00003126 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003127 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003128 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3129
Anders Carlssondde0a942008-09-11 09:15:33 +00003130 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003131 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003132 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003133
3134 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003135 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003137
Anders Carlssondde0a942008-09-11 09:15:33 +00003138 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003139
3140 // The scope of the catch variable ends right here.
3141 CatchVarCleanups.ForceCleanup();
3142
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003143 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003144 break;
3145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003146
Steve Naroff14108da2009-07-10 23:34:53 +00003147 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003148 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003149
3150 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003151 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3152 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003153
3154 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003155 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003156
John McCallf1549f62010-07-06 01:34:17 +00003157 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003158 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3159 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003160 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003161
John McCallf1549f62010-07-06 01:34:17 +00003162 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3163 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003164
3165 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003166 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003167
Anders Carlsson80f25672008-09-09 17:59:25 +00003168 // Emit the @catch block.
3169 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003170
3171 // Collect any cleanups for the catch variable. The scope lasts until
3172 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003173 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003174
John McCallb6bbcc92010-10-15 04:57:14 +00003175 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003176 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003177
John McCallf1549f62010-07-06 01:34:17 +00003178 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003179 llvm::Value *Tmp =
3180 CGF.Builder.CreateBitCast(Caught,
3181 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003182 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003183 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003184
Anders Carlssondde0a942008-09-11 09:15:33 +00003185 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003186
3187 // We're done with the catch variable.
3188 CatchVarCleanups.ForceCleanup();
3189
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003190 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003191
Anders Carlsson80f25672008-09-09 17:59:25 +00003192 CGF.EmitBlock(NextCatchBlock);
3193 }
3194
John McCallf1549f62010-07-06 01:34:17 +00003195 CGF.ObjCEHValueStack.pop_back();
3196
John McCall0b251722010-08-04 05:59:32 +00003197 // If nothing wanted anything to do with the caught exception,
3198 // kill the extract call.
3199 if (Caught->use_empty())
3200 Caught->eraseFromParent();
3201
3202 if (!AllMatched)
3203 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3204
3205 if (HasFinally) {
3206 // Emit the exception handler for the @catch blocks.
3207 CGF.EmitBlock(CatchHandler);
3208
3209 // In theory we might now need a write hazard, but actually it's
3210 // unnecessary because there's no local-accessing code between
3211 // the try's write hazard and here.
3212 //Hazards.emitWriteHazard();
3213
John McCall9e2213d2010-10-04 23:42:51 +00003214 // Extract the new exception and save it to the
3215 // propagating-exception slot.
3216 assert(PropagatingExnVar);
3217 llvm::CallInst *NewCaught =
3218 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3219 ExceptionData, "caught");
3220 NewCaught->setDoesNotThrow();
3221 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3222
John McCall0b251722010-08-04 05:59:32 +00003223 // Don't pop the catch handler; the throw already did.
3224 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003225 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003226 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003227 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003228
John McCall87bb5822010-07-31 23:20:56 +00003229 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003230 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003231
John McCallf1549f62010-07-06 01:34:17 +00003232 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003233 CGF.Builder.restoreIP(TryFallthroughIP);
3234 if (CGF.HaveInsertPoint())
3235 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003236 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003237 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003238
John McCallf1549f62010-07-06 01:34:17 +00003239 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003240 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003241 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003242 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003243 // If we have a propagating-exception variable, check it.
3244 llvm::Value *PropagatingExn;
3245 if (PropagatingExnVar) {
3246 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003247
John McCall9e2213d2010-10-04 23:42:51 +00003248 // Otherwise, just look in the buffer for the exception to throw.
3249 } else {
3250 llvm::CallInst *Caught =
3251 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3252 ExceptionData);
3253 Caught->setDoesNotThrow();
3254 PropagatingExn = Caught;
3255 }
3256
3257 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003258 ->setDoesNotThrow();
3259 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003260 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003261
John McCall87bb5822010-07-31 23:20:56 +00003262 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003263}
3264
3265void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003266 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003267 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003268
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003269 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3270 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003271 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003272 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3273 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003274 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003275 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003276 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003277 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003278
John McCallf1549f62010-07-06 01:34:17 +00003279 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3280 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003281 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003282
3283 // Clear the insertion point to indicate we are in unreachable code.
3284 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003285}
3286
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003287/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003288/// object: objc_read_weak (id *src)
3289///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003290llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003291 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003292 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003293 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3294 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3295 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003296 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003297 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003298 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003299 return read_weak;
3300}
3301
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003302/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3303/// objc_assign_weak (id src, id *dst)
3304///
3305void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003306 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003307 const llvm::Type * SrcTy = src->getType();
3308 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003309 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003310 assert(Size <= 8 && "does not support size > 8");
3311 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003312 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003313 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3314 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003315 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3316 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003317 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003318 src, dst, "weakassign");
3319 return;
3320}
3321
Fariborz Jahanian58626502008-11-19 00:59:10 +00003322/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3323/// objc_assign_global (id src, id *dst)
3324///
3325void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003326 llvm::Value *src, llvm::Value *dst,
3327 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003328 const llvm::Type * SrcTy = src->getType();
3329 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003330 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003331 assert(Size <= 8 && "does not support size > 8");
3332 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003333 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003334 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3335 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003336 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3337 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003338 if (!threadlocal)
3339 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3340 src, dst, "globalassign");
3341 else
3342 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3343 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003344 return;
3345}
3346
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003347/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003348/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003349///
3350void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003351 llvm::Value *src, llvm::Value *dst,
3352 llvm::Value *ivarOffset) {
3353 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003354 const llvm::Type * SrcTy = src->getType();
3355 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003356 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003357 assert(Size <= 8 && "does not support size > 8");
3358 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003359 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003360 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3361 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003362 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3363 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003364 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3365 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003366 return;
3367}
3368
Fariborz Jahanian58626502008-11-19 00:59:10 +00003369/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3370/// objc_assign_strongCast (id src, id *dst)
3371///
3372void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003373 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003374 const llvm::Type * SrcTy = src->getType();
3375 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003376 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003377 assert(Size <= 8 && "does not support size > 8");
3378 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003379 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003380 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3381 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003382 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3383 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003384 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003385 src, dst, "weakassign");
3386 return;
3387}
3388
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003389void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003390 llvm::Value *DestPtr,
3391 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003392 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003393 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3394 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003395 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003396 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003397 return;
3398}
3399
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003400/// EmitObjCValueForIvar - Code Gen for ivar reference.
3401///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003402LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3403 QualType ObjectTy,
3404 llvm::Value *BaseValue,
3405 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003406 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003407 const ObjCInterfaceDecl *ID =
3408 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003409 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3410 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003411}
3412
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003413llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003414 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003415 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003416 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003417 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003418 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3419 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003420}
3421
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003422/* *** Private Interface *** */
3423
3424/// EmitImageInfo - Emit the image info marker used to encode some module
3425/// level information.
3426///
3427/// See: <rdr://4810609&4810587&4810587>
3428/// struct IMAGE_INFO {
3429/// unsigned version;
3430/// unsigned flags;
3431/// };
3432enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003433 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003434 eImageInfo_GarbageCollected = (1 << 1),
3435 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003436 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3437
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003438 // A flag indicating that the module has no instances of a @synthesize of a
3439 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003440 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003441};
3442
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003443void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003444 unsigned version = 0; // Version is unused?
3445 unsigned flags = 0;
3446
3447 // FIXME: Fix and continue?
3448 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3449 flags |= eImageInfo_GarbageCollected;
3450 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3451 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003452
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003453 // We never allow @synthesize of a superclass property.
3454 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003455
Chris Lattner77b89b82010-06-27 07:15:29 +00003456 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3457
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003458 // Emitted as int[2];
3459 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003460 llvm::ConstantInt::get(Int32Ty, version),
3461 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003462 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003463 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003464
3465 const char *Section;
3466 if (ObjCABI == 1)
3467 Section = "__OBJC, __image_info,regular";
3468 else
3469 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003470 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003471 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003472 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003473 Section,
3474 0,
3475 true);
3476 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003477}
3478
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003479
3480// struct objc_module {
3481// unsigned long version;
3482// unsigned long size;
3483// const char *name;
3484// Symtab symtab;
3485// };
3486
3487// FIXME: Get from somewhere
3488static const int ModuleVersion = 7;
3489
3490void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003491 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003492
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003493 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003494 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3495 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003496 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003497 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003498 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003499 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003500 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003501 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003502 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003503}
3504
3505llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003506 unsigned NumClasses = DefinedClasses.size();
3507 unsigned NumCategories = DefinedCategories.size();
3508
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003509 // Return null if no symbols were defined.
3510 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003511 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003512
3513 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003514 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003515 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003516 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3517 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003518
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003519 // The runtime expects exactly the list of defined classes followed
3520 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003521 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003522 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003523 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003524 ObjCTypes.Int8PtrTy);
3525 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003526 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003527 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003528 ObjCTypes.Int8PtrTy);
3529
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003530 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003531 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003532 NumClasses + NumCategories),
3533 Symbols);
3534
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003535 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003536
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003537 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003538 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3539 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003540 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003541 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003542}
3543
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003544llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003545 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003546 LazySymbols.insert(ID->getIdentifier());
3547
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003548 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003549
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003550 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003551 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003552 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003553 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003554 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003555 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3556 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003557 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003558 }
3559
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003560 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003561}
3562
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003563llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3564 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003565 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003566
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003567 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003568 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003569 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003570 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003572 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3573 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003574 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003575 }
3576
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003577 if (lvalue)
3578 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003579 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003580}
3581
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003582llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003583 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003584
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003585 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003586 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003587 llvm::ConstantArray::get(VMContext,
3588 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003589 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003590 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003591
Owen Andersona1cf15f2009-07-14 23:10:40 +00003592 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003593}
3594
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003595llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3596 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3597 I = MethodDefinitions.find(MD);
3598 if (I != MethodDefinitions.end())
3599 return I->second;
3600
3601 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3602 // MD isn't emitted yet because it comes from PCH.
3603 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3604 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3605 return MethodDefinitions[MD];
3606 }
3607
3608 return NULL;
3609}
3610
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003611/// GetIvarLayoutName - Returns a unique constant for the given
3612/// ivar layout bitmap.
3613llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003614 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003615 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003616}
3617
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003618void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003619 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003620 bool ForStrongLayout,
3621 bool &HasUnion) {
3622 const RecordDecl *RD = RT->getDecl();
3623 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003624 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003625 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003626 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003627 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003628
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003629 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3630 ForStrongLayout, HasUnion);
3631}
3632
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003633void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003634 const llvm::StructLayout *Layout,
3635 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003636 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003637 unsigned int BytePos, bool ForStrongLayout,
3638 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003639 bool IsUnion = (RD && RD->isUnion());
3640 uint64_t MaxUnionIvarSize = 0;
3641 uint64_t MaxSkippedUnionIvarSize = 0;
3642 FieldDecl *MaxField = 0;
3643 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003644 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003645 uint64_t MaxFieldOffset = 0;
3646 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003647 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003648
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003649 if (RecFields.empty())
3650 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003651 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3652 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3653
Chris Lattnerf1690852009-03-31 08:48:01 +00003654 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003655 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003656 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003657 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003658 // Note that 'i' here is actually the field index inside RD of Field,
3659 // although this dependency is hidden.
3660 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3661 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003662 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003663 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003664
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003665 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003666 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003667 LastFieldBitfieldOrUnnamed = Field;
3668 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003669 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003670 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003671
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003672 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003673 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003674 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003675 if (FQT->isUnionType())
3676 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003677
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003678 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003679 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003680 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003681 continue;
3682 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003683
Chris Lattnerf1690852009-03-31 08:48:01 +00003684 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003686 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003687 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003688 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003689 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003690 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3691 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003692 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003693 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003694 FQT = CArray->getElementType();
3695 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003696
3697 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003698 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003699 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003700 int OldIndex = IvarsInfo.size() - 1;
3701 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003702
Ted Kremenek6217b802009-07-29 21:53:49 +00003703 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003704 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003705 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003706
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003707 // Replicate layout information for each array element. Note that
3708 // one element is already done.
3709 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003710 for (int FirstIndex = IvarsInfo.size() - 1,
3711 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003712 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003713 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3714 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3715 IvarsInfo[i].ivar_size));
3716 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3717 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3718 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003719 }
3720 continue;
3721 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003722 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003723 // At this point, we are done with Record/Union and array there of.
3724 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003725 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003726
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003727 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003728 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3729 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003730 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003731 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003732 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003733 MaxUnionIvarSize = UnionIvarSize;
3734 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003735 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003736 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003737 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003738 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003739 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003740 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003741 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003742 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3743 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003744 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003745 // FIXME: Why the asymmetry? We divide by word size in bits on other
3746 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003747 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003748 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003749 MaxSkippedUnionIvarSize = UnionIvarSize;
3750 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003751 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003752 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003753 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003754 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003755 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003756 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003757 }
3758 }
3759 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003760
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003761 if (LastFieldBitfieldOrUnnamed) {
3762 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3763 // Last field was a bitfield. Must update skip info.
3764 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3765 uint64_t BitFieldSize =
3766 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3767 GC_IVAR skivar;
3768 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3769 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3770 + ((BitFieldSize % ByteSizeInBits) != 0);
3771 SkipIvars.push_back(skivar);
3772 } else {
3773 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3774 // Last field was unnamed. Must update skip info.
3775 unsigned FieldSize
3776 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3777 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3778 FieldSize / ByteSizeInBits));
3779 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003780 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003781
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003782 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003783 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003784 MaxUnionIvarSize));
3785 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003786 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003787 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003788}
3789
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003790/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3791/// the computations and returning the layout bitmap (for ivar or blocks) in
3792/// the given argument BitMap string container. Routine reads
3793/// two containers, IvarsInfo and SkipIvars which are assumed to be
3794/// filled already by the caller.
3795llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003796 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003797 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003798
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003799 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003800 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003801 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003802 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003803 if (IvarsInfo[0].ivar_bytepos == 0) {
3804 WordsToSkip = 0;
3805 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003806 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003807 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3808 WordsToScan = IvarsInfo[0].ivar_size;
3809 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003810 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003811 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003812 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003813 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003814 // consecutive 'scanned' object pointers.
3815 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003816 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003817 // Skip over 'gc'able object pointer which lay over each other.
3818 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3819 continue;
3820 // Must skip over 1 or more words. We save current skip/scan values
3821 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003822 SKIP_SCAN SkScan;
3823 SkScan.skip = WordsToSkip;
3824 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003825 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003826
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003827 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003828 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3829 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003830 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003831 WordsToSkip = 0;
3832 WordsToScan = IvarsInfo[i].ivar_size;
3833 }
3834 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003835 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003836 SKIP_SCAN SkScan;
3837 SkScan.skip = WordsToSkip;
3838 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003839 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003840 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003841
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003842 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003843 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003844 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003845 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003846 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003847 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003848 IvarsInfo[LastIndex].ivar_bytepos +
3849 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003850 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003851 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003852 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003853 SKIP_SCAN SkScan;
3854 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3855 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003856 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003857 }
3858 }
3859 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3860 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003861 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003862 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003863 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3864 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3865 // 0xM0 followed by 0x0N detected.
3866 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3867 for (int j = i+1; j < SkipScan; j++)
3868 SkipScanIvars[j] = SkipScanIvars[j+1];
3869 --SkipScan;
3870 }
3871 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003872
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003873 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003874 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003875 unsigned char byte;
3876 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3877 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3878 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3879 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003880
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003881 // first skip big.
3882 for (unsigned int ix = 0; ix < skip_big; ix++)
3883 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003884
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003885 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003886 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003887 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003888 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003889 byte |= 0xf;
3890 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003891 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003892 byte |= scan_small;
3893 scan_small = 0;
3894 }
3895 BitMap += byte;
3896 }
3897 // next scan big
3898 for (unsigned int ix = 0; ix < scan_big; ix++)
3899 BitMap += (unsigned char)(0x0f);
3900 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003901 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003902 byte = scan_small;
3903 BitMap += byte;
3904 }
3905 }
3906 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003907 unsigned char zero = 0;
3908 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003909
3910 llvm::GlobalVariable * Entry =
3911 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3912 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3913 "__TEXT,__cstring,cstring_literals",
3914 1, true);
3915 return getConstantGEP(VMContext, Entry, 0, 0);
3916}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003917
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003918/// BuildIvarLayout - Builds ivar layout bitmap for the class
3919/// implementation for the __strong or __weak case.
3920/// The layout map displays which words in ivar list must be skipped
3921/// and which must be scanned by GC (see below). String is built of bytes.
3922/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3923/// of words to skip and right nibble is count of words to scan. So, each
3924/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3925/// represented by a 0x00 byte which also ends the string.
3926/// 1. when ForStrongLayout is true, following ivars are scanned:
3927/// - id, Class
3928/// - object *
3929/// - __strong anything
3930///
3931/// 2. When ForStrongLayout is false, following ivars are scanned:
3932/// - __weak anything
3933///
3934llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3935 const ObjCImplementationDecl *OMD,
3936 bool ForStrongLayout) {
3937 bool hasUnion = false;
3938
3939 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3940 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3941 return llvm::Constant::getNullValue(PtrTy);
3942
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003943 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003944 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003945 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003946
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003947 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003948 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3949 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3950
3951 if (RecFields.empty())
3952 return llvm::Constant::getNullValue(PtrTy);
3953
3954 SkipIvars.clear();
3955 IvarsInfo.clear();
3956
3957 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3958 if (IvarsInfo.empty())
3959 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003960 // Sort on byte position in case we encounterred a union nested in
3961 // the ivar list.
3962 if (hasUnion && !IvarsInfo.empty())
3963 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3964 if (hasUnion && !SkipIvars.empty())
3965 std::sort(SkipIvars.begin(), SkipIvars.end());
3966
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003967 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003968 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003969
3970 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003971 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003972 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003973 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003974 const unsigned char *s = (unsigned char*)BitMap.c_str();
3975 for (unsigned i = 0; i < BitMap.size(); i++)
3976 if (!(s[i] & 0xf0))
3977 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3978 else
3979 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3980 printf("\n");
3981 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003982 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003983}
3984
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003985llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003986 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3987
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003988 // FIXME: Avoid std::string copying.
3989 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003990 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003991 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003992 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003993 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003994
Owen Andersona1cf15f2009-07-14 23:10:40 +00003995 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003996}
3997
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003998// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003999llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004000 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4001}
4002
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004003llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004004 std::string TypeStr;
4005 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4006
4007 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004008
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004009 if (!Entry)
4010 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004011 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004012 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004013 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004014
Owen Andersona1cf15f2009-07-14 23:10:40 +00004015 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004016}
4017
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004018llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004019 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00004020 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4021 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00004022
4023 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4024
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004025 if (!Entry)
4026 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004027 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004028 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004029 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004030
Owen Andersona1cf15f2009-07-14 23:10:40 +00004031 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004032}
4033
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004034// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004035llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004036 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004037
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004038 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004039 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004040 llvm::ConstantArray::get(VMContext,
4041 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004042 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004043 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004044
Owen Andersona1cf15f2009-07-14 23:10:40 +00004045 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004046}
4047
4048// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004049// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004050llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004051CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4052 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004053 std::string TypeStr;
4054 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004055 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4056}
4057
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004058void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004059 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004060 llvm::SmallVectorImpl<char> &Name) {
4061 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004062 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004063 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4064 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004065 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004066 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004067 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004068 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004069}
4070
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004071void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004072 EmitModuleInfo();
4073
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004074 // Emit the dummy bodies for any protocols which were referenced but
4075 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004076 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004077 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4078 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004079 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004080
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004081 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004082 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004083 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004084 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004085 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004086 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004087 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004088 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004089 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004090 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004091 }
4092
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004093 // Add assembler directives to add lazy undefined symbol references
4094 // for classes which are referenced but not defined. This is
4095 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004096 //
4097 // FIXME: It would be nice if we had an LLVM construct for this.
4098 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4099 llvm::SmallString<256> Asm;
4100 Asm += CGM.getModule().getModuleInlineAsm();
4101 if (!Asm.empty() && Asm.back() != '\n')
4102 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004103
Daniel Dunbar33063492009-09-07 00:20:42 +00004104 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004105 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4106 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004107 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4108 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004109 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004110 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004111 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004112 }
4113
4114 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4115 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4116 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4117 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004118
Daniel Dunbar33063492009-09-07 00:20:42 +00004119 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004120 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004121}
4122
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004123CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004124 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004125 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004126 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004127 ObjCABI = 2;
4128}
4129
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004130/* *** */
4131
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004132ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004133 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004134 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4135 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004136
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004137 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004138 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004139 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004140 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004141 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004142
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004143 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004144 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004145 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004146
Mike Stumpf5408fe2009-05-16 07:57:57 +00004147 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4148 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004149 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004150 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004151
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004152 // I'm not sure I like this. The implicit coordination is a bit
4153 // gross. We should solve this in a reasonable fashion because this
4154 // is a pretty common task (match some runtime data structure with
4155 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004156
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004157 // FIXME: This is leaked.
4158 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004159
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004160 // struct _objc_super {
4161 // id self;
4162 // Class cls;
4163 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004164 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004165 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004166 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004168 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004169 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004170 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004171 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004172 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004174 SuperCTy = Ctx.getTagDeclType(RD);
4175 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004177 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004178 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4179
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004180 // struct _prop_t {
4181 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004183 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004184 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004185 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004186 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004188 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004189 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004190 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004191 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004192 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004193 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004194 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004195 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004196 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004197 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004198 PropertyListTy);
4199 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004200 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004202 // struct _objc_method {
4203 // SEL _cmd;
4204 // char *method_type;
4205 // char *_imp;
4206 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004207 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004208 Int8PtrTy,
4209 Int8PtrTy,
4210 NULL);
4211 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004213 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004214 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004215 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004216 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004217}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004218
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004219ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004220 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004221 // struct _objc_method_description {
4222 // SEL name;
4223 // char *types;
4224 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004226 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004227 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004228 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230 MethodDescriptionTy);
4231
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004232 // struct _objc_method_description_list {
4233 // int count;
4234 // struct _objc_method_description[1];
4235 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004236 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004237 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004238 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004239 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004240 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004241 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004242
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004243 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004244 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004245 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004246
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004247 // Protocol description structures
4248
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004249 // struct _objc_protocol_extension {
4250 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4251 // struct _objc_method_description_list *optional_instance_methods;
4252 // struct _objc_method_description_list *optional_class_methods;
4253 // struct _objc_property_list *instance_properties;
4254 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004255 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004256 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004257 MethodDescriptionListPtrTy,
4258 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004259 PropertyListPtrTy,
4260 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004261 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004262 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004263
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004264 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004265 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004266
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004267 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004268
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004269 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4270 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004271
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004272 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004273 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004274 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004275 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004276 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004277 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004278 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4279
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004280 // struct _objc_protocol {
4281 // struct _objc_protocol_extension *isa;
4282 // char *protocol_name;
4283 // struct _objc_protocol **_objc_protocol_list;
4284 // struct _objc_method_description_list *instance_methods;
4285 // struct _objc_method_description_list *class_methods;
4286 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004287 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004288 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004289 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004290 MethodDescriptionListPtrTy,
4291 MethodDescriptionListPtrTy,
4292 NULL);
4293 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4294
4295 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004296 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004297 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004298 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004299 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004300
4301 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004302 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004303 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004304
4305 // Class description structures
4306
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004307 // struct _objc_ivar {
4308 // char *ivar_name;
4309 // char *ivar_type;
4310 // int ivar_offset;
4311 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004312 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004313 Int8PtrTy,
4314 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004315 NULL);
4316 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4317
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004318 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004319 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004320 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004321 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004322
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004323 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004324 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004325 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004326 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004327
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004328 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004329 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004330 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004331 Int8PtrTy,
4332 PropertyListPtrTy,
4333 NULL);
4334 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004335 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004336
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004337 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004338
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004339 // struct _objc_class {
4340 // Class isa;
4341 // Class super_class;
4342 // char *name;
4343 // long version;
4344 // long info;
4345 // long instance_size;
4346 // struct _objc_ivar_list *ivars;
4347 // struct _objc_method_list *methods;
4348 // struct _objc_cache *cache;
4349 // struct _objc_protocol_list *protocols;
4350 // char *ivar_layout;
4351 // struct _objc_class_ext *ext;
4352 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004353 T = llvm::StructType::get(VMContext,
4354 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004355 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004356 Int8PtrTy,
4357 LongTy,
4358 LongTy,
4359 LongTy,
4360 IvarListPtrTy,
4361 MethodListPtrTy,
4362 CachePtrTy,
4363 ProtocolListPtrTy,
4364 Int8PtrTy,
4365 ClassExtensionPtrTy,
4366 NULL);
4367 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004368
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004369 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4370 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004371 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004372
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004373 // struct _objc_category {
4374 // char *category_name;
4375 // char *class_name;
4376 // struct _objc_method_list *instance_method;
4377 // struct _objc_method_list *class_method;
4378 // uint32_t size; // sizeof(struct _objc_category)
4379 // struct _objc_property_list *instance_properties;// category's @property
4380 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004381 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004382 Int8PtrTy,
4383 MethodListPtrTy,
4384 MethodListPtrTy,
4385 ProtocolListPtrTy,
4386 IntTy,
4387 PropertyListPtrTy,
4388 NULL);
4389 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4390
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004391 // Global metadata structures
4392
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004393 // struct _objc_symtab {
4394 // long sel_ref_cnt;
4395 // SEL *refs;
4396 // short cls_def_cnt;
4397 // short cat_def_cnt;
4398 // char *defs[cls_def_cnt + cat_def_cnt];
4399 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004400 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004401 SelectorPtrTy,
4402 ShortTy,
4403 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004404 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004405 NULL);
4406 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004407 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004408
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004409 // struct _objc_module {
4410 // long version;
4411 // long size; // sizeof(struct _objc_module)
4412 // char *name;
4413 // struct _objc_symtab* symtab;
4414 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004415 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004416 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004417 LongTy,
4418 Int8PtrTy,
4419 SymtabPtrTy,
4420 NULL);
4421 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004422
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423
Mike Stumpf5408fe2009-05-16 07:57:57 +00004424 // FIXME: This is the size of the setjmp buffer and should be target
4425 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004426 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004427
Anders Carlsson124526b2008-09-09 10:10:21 +00004428 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004429 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004430 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004431
4432 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004433 llvm::StructType::get(VMContext,
4434 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4435 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004436 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004437 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004438 ExceptionDataTy);
4439
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004440}
4441
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004442ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004443 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004444 // struct _method_list_t {
4445 // uint32_t entsize; // sizeof(struct _objc_method)
4446 // uint32_t method_count;
4447 // struct _objc_method method_list[method_count];
4448 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004449 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004450 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004451 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004452 NULL);
4453 CGM.getModule().addTypeName("struct.__method_list_t",
4454 MethodListnfABITy);
4455 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004456 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004457
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004458 // struct _protocol_t {
4459 // id isa; // NULL
4460 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004461 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004462 // const struct method_list_t * const instance_methods;
4463 // const struct method_list_t * const class_methods;
4464 // const struct method_list_t *optionalInstanceMethods;
4465 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004466 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004467 // const uint32_t size; // sizeof(struct _protocol_t)
4468 // const uint32_t flags; // = 0
4469 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004470
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004471 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004472 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Owen Anderson47a434f2009-08-05 23:18:46 +00004474 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004475 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004476 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004477 ProtocolListTyHolder),
4478 MethodListnfABIPtrTy,
4479 MethodListnfABIPtrTy,
4480 MethodListnfABIPtrTy,
4481 MethodListnfABIPtrTy,
4482 PropertyListPtrTy,
4483 IntTy,
4484 IntTy,
4485 NULL);
4486 CGM.getModule().addTypeName("struct._protocol_t",
4487 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004488
4489 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004490 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004491
Fariborz Jahanianda320092009-01-29 19:24:30 +00004492 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004493 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004494 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004495 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004496 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004497 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004498 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004499 NULL);
4500 CGM.getModule().addTypeName("struct._objc_protocol_list",
4501 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004502 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004503 ProtocolListnfABITy);
4504
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004505 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004506 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004507
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004508 // struct _ivar_t {
4509 // unsigned long int *offset; // pointer to ivar offset location
4510 // char *name;
4511 // char *type;
4512 // uint32_t alignment;
4513 // uint32_t size;
4514 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004515 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004516 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004517 Int8PtrTy,
4518 Int8PtrTy,
4519 IntTy,
4520 IntTy,
4521 NULL);
4522 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004523
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004524 // struct _ivar_list_t {
4525 // uint32 entsize; // sizeof(struct _ivar_t)
4526 // uint32 count;
4527 // struct _iver_t list[count];
4528 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004529 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004530 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004531 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004532 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004533 NULL);
4534 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004535
Owen Anderson96e0fc72009-07-29 22:16:19 +00004536 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004537
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004538 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004539 // uint32_t const flags;
4540 // uint32_t const instanceStart;
4541 // uint32_t const instanceSize;
4542 // uint32_t const reserved; // only when building for 64bit targets
4543 // const uint8_t * const ivarLayout;
4544 // const char *const name;
4545 // const struct _method_list_t * const baseMethods;
4546 // const struct _objc_protocol_list *const baseProtocols;
4547 // const struct _ivar_list_t *const ivars;
4548 // const uint8_t * const weakIvarLayout;
4549 // const struct _prop_list_t * const properties;
4550 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004552 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004553 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004554 IntTy,
4555 IntTy,
4556 Int8PtrTy,
4557 Int8PtrTy,
4558 MethodListnfABIPtrTy,
4559 ProtocolListnfABIPtrTy,
4560 IvarListnfABIPtrTy,
4561 Int8PtrTy,
4562 PropertyListPtrTy,
4563 NULL);
4564 CGM.getModule().addTypeName("struct._class_ro_t",
4565 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004567 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4568 std::vector<const llvm::Type*> Params;
4569 Params.push_back(ObjectPtrTy);
4570 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004571 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004572 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4573
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004574 // struct _class_t {
4575 // struct _class_t *isa;
4576 // struct _class_t * const superclass;
4577 // void *cache;
4578 // IMP *vtable;
4579 // struct class_ro_t *ro;
4580 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004581
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004582 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004583 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004584 llvm::StructType::get(VMContext,
4585 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586 llvm::PointerType::getUnqual(ClassTyHolder),
4587 CachePtrTy,
4588 llvm::PointerType::getUnqual(ImpnfABITy),
4589 llvm::PointerType::getUnqual(ClassRonfABITy),
4590 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004591 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4592
4593 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594 ClassnfABITy);
4595
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004596 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004597 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004598
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004599 // struct _category_t {
4600 // const char * const name;
4601 // struct _class_t *const cls;
4602 // const struct _method_list_t * const instance_methods;
4603 // const struct _method_list_t * const class_methods;
4604 // const struct _protocol_list_t * const protocols;
4605 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004606 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004607 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004608 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004609 MethodListnfABIPtrTy,
4610 MethodListnfABIPtrTy,
4611 ProtocolListnfABIPtrTy,
4612 PropertyListPtrTy,
4613 NULL);
4614 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004615
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004616 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004617 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4618 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004619
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004620 // MessageRefTy - LLVM for:
4621 // struct _message_ref_t {
4622 // IMP messenger;
4623 // SEL name;
4624 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004626 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004627 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004628 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004629 SourceLocation(),
4630 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004631 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004632 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004633 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004634 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004635 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004636
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004637 MessageRefCTy = Ctx.getTagDeclType(RD);
4638 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4639 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004640
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004641 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004642 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004643
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004644 // SuperMessageRefTy - LLVM for:
4645 // struct _super_message_ref_t {
4646 // SUPER_IMP messenger;
4647 // SEL name;
4648 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004649 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004650 SelectorPtrTy,
4651 NULL);
4652 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004653
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004654 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004655 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4656
Daniel Dunbare588b992009-03-01 04:46:24 +00004657
4658 // struct objc_typeinfo {
4659 // const void** vtable; // objc_ehtype_vtable + 2
4660 // const char* name; // c++ typeinfo string
4661 // Class cls;
4662 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004663 EHTypeTy = llvm::StructType::get(VMContext,
4664 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004665 Int8PtrTy,
4666 ClassnfABIPtrTy,
4667 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004668 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004669 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004670}
4671
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004672llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004673 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004674
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004675 return NULL;
4676}
4677
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004678void CGObjCNonFragileABIMac::AddModuleClassList(const
4679 std::vector<llvm::GlobalValue*>
4680 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004681 const char *SymbolName,
4682 const char *SectionName) {
4683 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004684
Daniel Dunbar463b8762009-05-15 21:48:48 +00004685 if (!NumClasses)
4686 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004687
Daniel Dunbar463b8762009-05-15 21:48:48 +00004688 std::vector<llvm::Constant*> Symbols(NumClasses);
4689 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004690 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004691 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004693 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004694 NumClasses),
4695 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004696
Daniel Dunbar463b8762009-05-15 21:48:48 +00004697 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004698 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004699 llvm::GlobalValue::InternalLinkage,
4700 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004701 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004702 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004703 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004704 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004705}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004706
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004707void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4708 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004709
Daniel Dunbar463b8762009-05-15 21:48:48 +00004710 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004711 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004712 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004713 "\01L_OBJC_LABEL_CLASS_$",
4714 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004715
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004716 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4717 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4718 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4719 continue;
4720 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004721 }
4722
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004723 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4724 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4725 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4726 continue;
4727 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4728 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004729
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004730 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004731 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4732 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004733
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004734 // Build list of all implemented category addresses in array
4735 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004736 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004737 "\01L_OBJC_LABEL_CATEGORY_$",
4738 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004739 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004740 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4741 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004742
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004743 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004744}
4745
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004746/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004747/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004748/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004749/// message dispatch call for all the rest.
4750///
4751bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004752 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4753 default:
4754 assert(0 && "Invalid dispatch method!");
4755 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004756 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004757 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004758 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004759 case CodeGenOptions::Mixed:
4760 break;
4761 }
4762
4763 // If so, see whether this selector is in the white-list of things which must
4764 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004765 if (NonLegacyDispatchMethods.empty()) {
4766 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4767 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4768 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4769 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4770 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4771 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4772 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4773 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4774 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4775 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004777 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4778 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4779 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4780 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4781 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4782 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4783 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4784 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004786 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004787 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4788 &CGM.getContext().Idents.get("objects"),
4789 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004790 };
4791 NonLegacyDispatchMethods.insert(
4792 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004793 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004794
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004795 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004796}
4797
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004798// Metadata flags
4799enum MetaDataDlags {
4800 CLS = 0x0,
4801 CLS_META = 0x1,
4802 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004803 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004804 CLS_EXCEPTION = 0x20
4805};
4806/// BuildClassRoTInitializer - generate meta-data for:
4807/// struct _class_ro_t {
4808/// uint32_t const flags;
4809/// uint32_t const instanceStart;
4810/// uint32_t const instanceSize;
4811/// uint32_t const reserved; // only when building for 64bit targets
4812/// const uint8_t * const ivarLayout;
4813/// const char *const name;
4814/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004815/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004816/// const struct _ivar_list_t *const ivars;
4817/// const uint8_t * const weakIvarLayout;
4818/// const struct _prop_list_t * const properties;
4819/// }
4820///
4821llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004822 unsigned flags,
4823 unsigned InstanceStart,
4824 unsigned InstanceSize,
4825 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004826 std::string ClassName = ID->getNameAsString();
4827 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004828 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4829 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4830 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004831 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4833 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004834 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004835 // const struct _method_list_t * const baseMethods;
4836 std::vector<llvm::Constant*> Methods;
4837 std::string MethodListName("\01l_OBJC_$_");
4838 if (flags & CLS_META) {
4839 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004840 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004841 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004842 // Class methods should always be defined.
4843 Methods.push_back(GetMethodConstant(*i));
4844 }
4845 } else {
4846 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004847 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004848 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004849 // Instance methods should always be defined.
4850 Methods.push_back(GetMethodConstant(*i));
4851 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004852 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004853 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004854 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004855
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004856 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4857 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004858
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004859 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4860 if (llvm::Constant *C = GetMethodConstant(MD))
4861 Methods.push_back(C);
4862 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4863 if (llvm::Constant *C = GetMethodConstant(MD))
4864 Methods.push_back(C);
4865 }
4866 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004867 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868 Values[ 5] = EmitMethodList(MethodListName,
4869 "__DATA, __objc_const", Methods);
4870
Fariborz Jahanianda320092009-01-29 19:24:30 +00004871 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4872 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004873 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004874 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004875 OID->all_referenced_protocol_begin(),
4876 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004878 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004879 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004880 else
4881 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4883 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004884 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004885 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004886 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004887 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4888 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004889 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004890 Values);
4891 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004892 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4893 llvm::GlobalValue::InternalLinkage,
4894 Init,
4895 (flags & CLS_META) ?
4896 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4897 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004898 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004899 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004900 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004901 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004902
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004903}
4904
4905/// BuildClassMetaData - This routine defines that to-level meta-data
4906/// for the given ClassName for:
4907/// struct _class_t {
4908/// struct _class_t *isa;
4909/// struct _class_t * const superclass;
4910/// void *cache;
4911/// IMP *vtable;
4912/// struct class_ro_t *ro;
4913/// }
4914///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004915llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004916 std::string &ClassName,
4917 llvm::Constant *IsAGV,
4918 llvm::Constant *SuperClassGV,
4919 llvm::Constant *ClassRoGV,
4920 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004921 std::vector<llvm::Constant*> Values(5);
4922 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923 Values[1] = SuperClassGV;
4924 if (!Values[1])
4925 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004926 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4927 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4928 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004929 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004930 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004931 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4932 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004933 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004934 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004935 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004936 if (HiddenVisibility)
4937 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004938 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004939}
4940
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004941bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004942CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004943 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004944}
4945
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004946void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004947 uint32_t &InstanceStart,
4948 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004949 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004950 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004951
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004952 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004953 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004954
4955 // If there are no fields, the start is the same as the end.
4956 if (!RL.getFieldCount())
4957 InstanceStart = InstanceSize;
4958 else
4959 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004960}
4961
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004962void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4963 std::string ClassName = ID->getNameAsString();
4964 if (!ObjCEmptyCacheVar) {
4965 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004966 CGM.getModule(),
4967 ObjCTypes.CacheTy,
4968 false,
4969 llvm::GlobalValue::ExternalLinkage,
4970 0,
4971 "_objc_empty_cache");
4972
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004973 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004974 CGM.getModule(),
4975 ObjCTypes.ImpnfABITy,
4976 false,
4977 llvm::GlobalValue::ExternalLinkage,
4978 0,
4979 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004980 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004981 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004982 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004983 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004984 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004985 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004986 uint32_t InstanceSize = InstanceStart;
4987 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004988 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4989 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004990
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004991 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004992
4993 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004994 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004995 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004996 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004997 if (ID->getNumIvarInitializers())
4998 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004999 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005000 // class is root
5001 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005002 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005003 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005004 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005005 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005006 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5007 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5008 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005009 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005010 if (Root->hasAttr<WeakImportAttr>())
5011 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005012 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005014 ObjCMetaClassName +
5015 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005016 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005017 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5018 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005019 }
5020 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5021 InstanceStart,
5022 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005023 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005024 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005025 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5026 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005027 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005028
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005029 // Metadata for the class
5030 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005031 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005032 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005033 if (ID->getNumIvarInitializers())
5034 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005035
Douglas Gregor68584ed2009-06-18 16:11:24 +00005036 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005037 flags |= CLS_EXCEPTION;
5038
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005039 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005040 flags |= CLS_ROOT;
5041 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005042 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005043 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005044 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005045 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005046 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005047 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5048 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005049 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005050 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005051 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005052 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005054 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005056 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005057 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005058 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5059 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005060 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005061
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005062 // Determine if this class is also "non-lazy".
5063 if (ImplementationIsNonLazy(ID))
5064 DefinedNonLazyClasses.push_back(ClassMD);
5065
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005066 // Force the definition of the EHType if necessary.
5067 if (flags & CLS_EXCEPTION)
5068 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005069}
5070
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005071/// GenerateProtocolRef - This routine is called to generate code for
5072/// a protocol reference expression; as in:
5073/// @code
5074/// @protocol(Proto1);
5075/// @endcode
5076/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5077/// which will hold address of the protocol meta-data.
5078///
5079llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005080 const ObjCProtocolDecl *PD) {
5081
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005082 // This routine is called for @protocol only. So, we must build definition
5083 // of protocol's meta-data (not a reference to it!)
5084 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085 llvm::Constant *Init =
5086 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5087 ObjCTypes.ExternalProtocolPtrTy);
5088
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005089 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005090 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005091
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005092 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5093 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005094 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005095 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096 CGM.getModule(),
5097 Init->getType(), false,
5098 llvm::GlobalValue::WeakAnyLinkage,
5099 Init,
5100 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005101 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5102 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005103 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005104 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005105}
5106
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005107/// GenerateCategory - Build metadata for a category implementation.
5108/// struct _category_t {
5109/// const char * const name;
5110/// struct _class_t *const cls;
5111/// const struct _method_list_t * const instance_methods;
5112/// const struct _method_list_t * const class_methods;
5113/// const struct _protocol_list_t * const protocols;
5114/// const struct _prop_list_t * const properties;
5115/// }
5116///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005117void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005118 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005119 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005120 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5121 "_$_" + OCD->getNameAsString());
5122 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005123 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005124
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005125 std::vector<llvm::Constant*> Values(6);
5126 Values[0] = GetClassName(OCD->getIdentifier());
5127 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005128 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005129 if (Interface->hasAttr<WeakImportAttr>())
5130 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5131
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005132 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005133 std::vector<llvm::Constant*> Methods;
5134 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005135 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005136 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137
5138 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005139 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005140 // Instance methods should always be defined.
5141 Methods.push_back(GetMethodConstant(*i));
5142 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005143
5144 Values[2] = EmitMethodList(MethodListName,
5145 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005146 Methods);
5147
5148 MethodListName = Prefix;
5149 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5150 OCD->getNameAsString();
5151 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005152 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005153 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005154 // Class methods should always be defined.
5155 Methods.push_back(GetMethodConstant(*i));
5156 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005157
5158 Values[3] = EmitMethodList(MethodListName,
5159 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005160 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005161 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005162 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005163 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005164 llvm::SmallString<256> ExtName;
5165 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5166 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005167 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005168 + Interface->getName() + "_$_"
5169 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005170 Category->protocol_begin(),
5171 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005172 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5173 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005174 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005175 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5176 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005177 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005178
5179 llvm::Constant *Init =
5180 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005181 Values);
5182 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005183 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005184 false,
5185 llvm::GlobalValue::InternalLinkage,
5186 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005187 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005188 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005189 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005190 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005191 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005192 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005193
5194 // Determine if this category is also "non-lazy".
5195 if (ImplementationIsNonLazy(OCD))
5196 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005197}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005198
5199/// GetMethodConstant - Return a struct objc_method constant for the
5200/// given method if it has been defined. The result is null if the
5201/// method has not been defined. The return value has type MethodPtrTy.
5202llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005203 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005204 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005205 if (!Fn)
5206 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005207
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005208 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005209 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005210 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005211 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005212 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005213 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005214 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005215}
5216
5217/// EmitMethodList - Build meta-data for method declarations
5218/// struct _method_list_t {
5219/// uint32_t entsize; // sizeof(struct _objc_method)
5220/// uint32_t method_count;
5221/// struct _objc_method method_list[method_count];
5222/// }
5223///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005224llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5225 const char *Section,
5226 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005227 // Return null for empty list.
5228 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005229 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005231 std::vector<llvm::Constant*> Values(3);
5232 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005233 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005234 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005235 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005236 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005237 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005238 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005239 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005240 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005241
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005242 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005243 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005244 llvm::GlobalValue::InternalLinkage,
5245 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005246 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005247 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005248 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005249 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005250 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005251 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005252 ObjCTypes.MethodListnfABIPtrTy);
5253}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005254
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005255/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5256/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005257llvm::GlobalVariable *
5258CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5259 const ObjCIvarDecl *Ivar) {
5260 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005261 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005262 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005263 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005264 CGM.getModule().getGlobalVariable(Name);
5265 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005266 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005267 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005268 false,
5269 llvm::GlobalValue::ExternalLinkage,
5270 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005271 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005272 return IvarOffsetGV;
5273}
5274
Daniel Dunbare83be122010-04-02 21:14:02 +00005275llvm::Constant *
5276CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5277 const ObjCIvarDecl *Ivar,
5278 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005279 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005281 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005282 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005283 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005284
Mike Stumpf5408fe2009-05-16 07:57:57 +00005285 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5286 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005287 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5288 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005289 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005290 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005291 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005292 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005293 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005294 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005295}
5296
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005297/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005298/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005299/// IvarListnfABIPtrTy.
5300/// struct _ivar_t {
5301/// unsigned long int *offset; // pointer to ivar offset location
5302/// char *name;
5303/// char *type;
5304/// uint32_t alignment;
5305/// uint32_t size;
5306/// }
5307/// struct _ivar_list_t {
5308/// uint32 entsize; // sizeof(struct _ivar_t)
5309/// uint32 count;
5310/// struct _iver_t list[count];
5311/// }
5312///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005313
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005314llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005315 const ObjCImplementationDecl *ID) {
5316
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005317 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005319 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5320 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005322 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005323
Daniel Dunbar91636d62009-04-20 00:33:43 +00005324 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005325 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005326 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005327
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005328 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5329 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005330 // Ignore unnamed bit-fields.
5331 if (!IVD->getDeclName())
5332 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005333 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005334 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005335 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5336 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005337 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005338 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005339 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005340 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005341 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005342 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005343 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005344 // NOTE. Size of a bitfield does not match gcc's, because of the
5345 // way bitfields are treated special in each. But I am told that
5346 // 'size' for bitfield ivars is ignored by the runtime so it does
5347 // not matter. If it matters, there is enough info to get the
5348 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005349 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005350 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005351 }
5352 // Return null for empty list.
5353 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005354 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005355 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005356 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005357 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5358 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005359 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005360 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005361 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005362 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005363 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5364 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005365 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005366 llvm::GlobalValue::InternalLinkage,
5367 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005368 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005369 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005370 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005371 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372
Chris Lattnerad64e022009-07-17 23:57:13 +00005373 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005374 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005375}
5376
5377llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005380
Fariborz Jahanianda320092009-01-29 19:24:30 +00005381 if (!Entry) {
5382 // We use the initializer as a marker of whether this is a forward
5383 // reference or not. At module finalization we add the empty
5384 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005385 Entry =
5386 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5387 llvm::GlobalValue::ExternalLinkage,
5388 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005389 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005390 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005392
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 return Entry;
5394}
5395
5396/// GetOrEmitProtocol - Generate the protocol meta-data:
5397/// @code
5398/// struct _protocol_t {
5399/// id isa; // NULL
5400/// const char * const protocol_name;
5401/// const struct _protocol_list_t * protocol_list; // super protocols
5402/// const struct method_list_t * const instance_methods;
5403/// const struct method_list_t * const class_methods;
5404/// const struct method_list_t *optionalInstanceMethods;
5405/// const struct method_list_t *optionalClassMethods;
5406/// const struct _prop_list_t * properties;
5407/// const uint32_t size; // sizeof(struct _protocol_t)
5408/// const uint32_t flags; // = 0
5409/// }
5410/// @endcode
5411///
5412
5413llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005414 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005416
Fariborz Jahanianda320092009-01-29 19:24:30 +00005417 // Early exit if a defining object has already been generated.
5418 if (Entry && Entry->hasInitializer())
5419 return Entry;
5420
Fariborz Jahanianda320092009-01-29 19:24:30 +00005421 // Construct method lists.
5422 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5423 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005424 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005425 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005426 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005427 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005428 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5429 OptInstanceMethods.push_back(C);
5430 } else {
5431 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005433 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005434
5435 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005436 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005437 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005438 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005439 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5440 OptClassMethods.push_back(C);
5441 } else {
5442 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005444 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005445
Fariborz Jahanianda320092009-01-29 19:24:30 +00005446 std::vector<llvm::Constant*> Values(10);
5447 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005448 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005449 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005450 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5451 PD->protocol_begin(),
5452 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005453
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005454 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005455 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005456 "__DATA, __objc_const",
5457 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005458 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005459 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005460 "__DATA, __objc_const",
5461 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005462 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005463 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005464 "__DATA, __objc_const",
5465 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005466 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005467 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005468 "__DATA, __objc_const",
5469 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005470 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005471 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005472 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005473 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005474 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005475 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005476 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005477 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478
Fariborz Jahanianda320092009-01-29 19:24:30 +00005479 if (Entry) {
5480 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005481 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005482 Entry->setInitializer(Init);
5483 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005485 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5486 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5487 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005488 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005489 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005490 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005492 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005493 CGM.AddUsedGlobal(Entry);
5494
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005495 // Use this protocol meta-data to build protocol list table in section
5496 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005497 llvm::GlobalVariable *PTGV =
5498 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5499 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5500 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005501 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005502 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005503 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005504 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005505 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005506 return Entry;
5507}
5508
5509/// EmitProtocolList - Generate protocol list meta-data:
5510/// @code
5511/// struct _protocol_list_t {
5512/// long protocol_count; // Note, this is 32/64 bit
5513/// struct _protocol_t[protocol_count];
5514/// }
5515/// @endcode
5516///
5517llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005518CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5519 ObjCProtocolDecl::protocol_iterator begin,
5520 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005521 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005522
Fariborz Jahanianda320092009-01-29 19:24:30 +00005523 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005524 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005525 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005526
Daniel Dunbar948e2582009-02-15 07:36:20 +00005527 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005528 llvm::SmallString<256> TmpName;
5529 Name.toVector(TmpName);
5530 llvm::GlobalVariable *GV =
5531 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005532 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005533 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005534
Daniel Dunbar948e2582009-02-15 07:36:20 +00005535 for (; begin != end; ++begin)
5536 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5537
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005539 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005540 ObjCTypes.ProtocolnfABIPtrTy));
5541
Fariborz Jahanianda320092009-01-29 19:24:30 +00005542 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005543 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005544 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005545 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005546 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005547 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005548 ProtocolRefs.size()),
5549 ProtocolRefs);
5550
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005551 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005552 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005553 llvm::GlobalValue::InternalLinkage,
5554 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005555 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005556 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005557 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005558 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005559 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005561 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005562}
5563
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005564/// GetMethodDescriptionConstant - This routine build following meta-data:
5565/// struct _objc_method {
5566/// SEL _cmd;
5567/// char *method_type;
5568/// char *_imp;
5569/// }
5570
5571llvm::Constant *
5572CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5573 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005574 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005575 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5576 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005577 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005578 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005579 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005580 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005581}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005582
5583/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5584/// This code gen. amounts to generating code for:
5585/// @code
5586/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5587/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005588///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005589LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005590 CodeGen::CodeGenFunction &CGF,
5591 QualType ObjectTy,
5592 llvm::Value *BaseValue,
5593 const ObjCIvarDecl *Ivar,
5594 unsigned CVRQualifiers) {
5595 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005596 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5597 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005598}
5599
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005600llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005601 CodeGen::CodeGenFunction &CGF,
5602 const ObjCInterfaceDecl *Interface,
5603 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005604 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005605}
5606
Fariborz Jahanian46551122009-02-04 00:22:57 +00005607CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005608 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005609 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005610 QualType ResultType,
5611 Selector Sel,
5612 llvm::Value *Receiver,
5613 QualType Arg0Ty,
5614 bool IsSuper,
5615 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005616 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5617 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005618 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005619 llvm::Value *Arg0 = Receiver;
5620 if (!IsSuper)
5621 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005622
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005623 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005624 // FIXME. This is too much work to get the ABI-specific result type needed to
5625 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005626 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005627 = Types.getFunctionInfo(ResultType, CallArgList(),
5628 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005629 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005630 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005631 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005632 if (IsSuper) {
5633 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5634 Name += "objc_msgSendSuper2_stret_fixup";
5635 } else {
5636 Fn = ObjCTypes.getMessageSendStretFixupFn();
5637 Name += "objc_msgSend_stret_fixup";
5638 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005639 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5640 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5641 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005642 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005643 if (IsSuper) {
5644 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5645 Name += "objc_msgSendSuper2_fixup";
5646 } else {
5647 Fn = ObjCTypes.getMessageSendFixupFn();
5648 Name += "objc_msgSend_fixup";
5649 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005650 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005651 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005652 Name += '_';
5653 std::string SelName(Sel.getAsString());
5654 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005655 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005656 if (SelName[i] == ':')
5657 SelName[i] = '_';
5658 Name += SelName;
5659 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5660 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005661 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005662 std::vector<llvm::Constant*> Values(2);
5663 Values[0] = Fn;
5664 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005665 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005666 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005667 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005668 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005669 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005670 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005671 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005672 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005673 }
5674 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005676 CallArgList ActualArgs;
5677 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005678 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005679 ObjCTypes.MessageRefCPtrTy));
5680 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005681 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005682 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005683 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5684 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005685 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005686 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005687 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005688 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005689}
5690
5691/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005692CodeGen::RValue
5693CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005694 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005695 QualType ResultType,
5696 Selector Sel,
5697 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005698 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005699 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005700 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005701 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005702 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5703 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005704 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005705 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005706 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 Receiver, CGF.getContext().getObjCIdType(),
5708 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005709}
5710
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005711llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005712CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005713 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5714
Daniel Dunbardfff2302009-03-02 05:18:14 +00005715 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005717 false, llvm::GlobalValue::ExternalLinkage,
5718 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005719 }
5720
5721 return GV;
5722}
5723
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005724llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5725 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005726 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005727
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005728 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005729 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005730 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005731 Entry =
5732 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005733 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005735 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005736 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005737 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005738 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005739 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005740 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005742
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005743 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005744}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005745
Daniel Dunbar11394522009-04-18 08:51:00 +00005746llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005747CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005748 const ObjCInterfaceDecl *ID) {
5749 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005750
Daniel Dunbar11394522009-04-18 08:51:00 +00005751 if (!Entry) {
5752 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5753 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005754 Entry =
5755 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005756 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005757 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005758 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005759 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005760 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005761 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005762 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005763 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005764 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005765
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005766 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005767}
5768
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005769/// EmitMetaClassRef - Return a Value * of the address of _class_t
5770/// meta-data
5771///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005772llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5773 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005774 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5775 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005776 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005777
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005778 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005779 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005780 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005781 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005782 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005783 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005784 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005785 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005786 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005787 ObjCTypes.ClassnfABIPtrTy));
5788
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005789 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005790 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005791
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005792 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005793}
5794
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005795/// GetClass - Return a reference to the class for the given interface
5796/// decl.
5797llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5798 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005799 if (ID->hasAttr<WeakImportAttr>()) {
5800 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5801 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5802 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5803 }
5804
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005805 return EmitClassRef(Builder, ID);
5806}
5807
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005808/// Generates a message send where the super is the receiver. This is
5809/// a message send to self with special delivery semantics indicating
5810/// which class's method should be called.
5811CodeGen::RValue
5812CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005813 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005814 QualType ResultType,
5815 Selector Sel,
5816 const ObjCInterfaceDecl *Class,
5817 bool isCategoryImpl,
5818 llvm::Value *Receiver,
5819 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005820 const CodeGen::CallArgList &CallArgs,
5821 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005822 // ...
5823 // Create and init a super structure; this is a (receiver, class)
5824 // pair we will pass to objc_msgSendSuper.
5825 llvm::Value *ObjCSuper =
5826 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005827
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005828 llvm::Value *ReceiverAsObject =
5829 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5830 CGF.Builder.CreateStore(ReceiverAsObject,
5831 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005833 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005834 llvm::Value *Target;
5835 if (IsClassMessage) {
5836 if (isCategoryImpl) {
5837 // Message sent to "super' in a class method defined in
5838 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005839 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005840 Target = CGF.Builder.CreateStructGEP(Target, 0);
5841 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005842 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005843 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005844 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005845 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005846
Mike Stumpf5408fe2009-05-16 07:57:57 +00005847 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5848 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005849 const llvm::Type *ClassTy =
5850 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5851 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5852 CGF.Builder.CreateStore(Target,
5853 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005854
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005855 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005856 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5857 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005858 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005859 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005860 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005861 ObjCSuper, ObjCTypes.SuperPtrCTy,
5862 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005863}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005864
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005865llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005866 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005867 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005868
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005869 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870 llvm::Constant *Casted =
5871 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5872 ObjCTypes.SelectorPtrTy);
5873 Entry =
5874 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5875 llvm::GlobalValue::InternalLinkage,
5876 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005877 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005878 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005879 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005880
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005881 if (lval)
5882 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005883 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005884}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005885/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005886/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005887///
5888void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005889 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005890 llvm::Value *dst,
5891 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005892 const llvm::Type * SrcTy = src->getType();
5893 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005894 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005895 assert(Size <= 8 && "does not support size > 8");
5896 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5897 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005898 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5899 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005900 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5901 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005902 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5903 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005904 return;
5905}
5906
5907/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5908/// objc_assign_strongCast (id src, id *dst)
5909///
5910void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005911 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005912 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005913 const llvm::Type * SrcTy = src->getType();
5914 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005915 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005916 assert(Size <= 8 && "does not support size > 8");
5917 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005918 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005919 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5920 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005921 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5922 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005923 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005924 src, dst, "weakassign");
5925 return;
5926}
5927
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005928void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005929 CodeGen::CodeGenFunction &CGF,
5930 llvm::Value *DestPtr,
5931 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005932 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005933 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5934 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005935 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005936 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005937 return;
5938}
5939
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005940/// EmitObjCWeakRead - Code gen for loading value of a __weak
5941/// object: objc_read_weak (id *src)
5942///
5943llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005944 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005945 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005946 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005947 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5948 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005949 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005950 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005951 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005952 return read_weak;
5953}
5954
5955/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5956/// objc_assign_weak (id src, id *dst)
5957///
5958void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005959 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005960 const llvm::Type * SrcTy = src->getType();
5961 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005962 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005963 assert(Size <= 8 && "does not support size > 8");
5964 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5965 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005966 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5967 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005968 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5969 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005970 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005971 src, dst, "weakassign");
5972 return;
5973}
5974
5975/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5976/// objc_assign_global (id src, id *dst)
5977///
5978void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005979 llvm::Value *src, llvm::Value *dst,
5980 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005981 const llvm::Type * SrcTy = src->getType();
5982 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005983 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005984 assert(Size <= 8 && "does not support size > 8");
5985 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5986 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005987 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5988 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005989 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5990 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005991 if (!threadlocal)
5992 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5993 src, dst, "globalassign");
5994 else
5995 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5996 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005997 return;
5998}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005999
John McCall740e8072010-07-21 00:41:47 +00006000namespace {
John McCall1f0fca52010-07-21 07:22:38 +00006001 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00006002 llvm::Value *SyncExitFn;
6003 llvm::Value *SyncArg;
6004 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6005 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6006
6007 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6008 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6009 }
6010 };
6011}
6012
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006013void
John McCallf1549f62010-07-06 01:34:17 +00006014CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6015 const ObjCAtSynchronizedStmt &S) {
6016 // Evaluate the lock operand. This should dominate the cleanup.
6017 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006018
John McCallf1549f62010-07-06 01:34:17 +00006019 // Acquire the lock.
6020 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6021 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6022 ->setDoesNotThrow();
6023
6024 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006025 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6026 ObjCTypes.getSyncExitFn(),
6027 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006028
John McCallf1549f62010-07-06 01:34:17 +00006029 // Emit the body of the statement.
6030 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006031
John McCallf1549f62010-07-06 01:34:17 +00006032 // Pop the lock-release cleanup.
6033 CGF.PopCleanupBlock();
6034}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006035
John McCallf1549f62010-07-06 01:34:17 +00006036namespace {
6037 struct CatchHandler {
6038 const VarDecl *Variable;
6039 const Stmt *Body;
6040 llvm::BasicBlock *Block;
6041 llvm::Value *TypeInfo;
6042 };
John McCall8e3f8612010-07-13 22:12:14 +00006043
John McCall1f0fca52010-07-21 07:22:38 +00006044 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006045 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6046 MightThrow(MightThrow), Fn(Fn) {}
6047 bool MightThrow;
6048 llvm::Value *Fn;
6049
6050 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6051 if (!MightThrow) {
6052 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6053 return;
6054 }
6055
6056 CGF.EmitCallOrInvoke(Fn, 0, 0);
6057 }
6058 };
John McCallf1549f62010-07-06 01:34:17 +00006059}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006060
John McCall5a180392010-07-24 00:37:23 +00006061llvm::Constant *
6062CGObjCNonFragileABIMac::GetEHType(QualType T) {
6063 // There's a particular fixed type info for 'id'.
6064 if (T->isObjCIdType() ||
6065 T->isObjCQualifiedIdType()) {
6066 llvm::Constant *IDEHType =
6067 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6068 if (!IDEHType)
6069 IDEHType =
6070 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6071 false,
6072 llvm::GlobalValue::ExternalLinkage,
6073 0, "OBJC_EHTYPE_id");
6074 return IDEHType;
6075 }
6076
6077 // All other types should be Objective-C interface pointer types.
6078 const ObjCObjectPointerType *PT =
6079 T->getAs<ObjCObjectPointerType>();
6080 assert(PT && "Invalid @catch type.");
6081 const ObjCInterfaceType *IT = PT->getInterfaceType();
6082 assert(IT && "Invalid @catch type.");
6083 return GetInterfaceEHType(IT->getDecl(), false);
6084}
6085
John McCallf1549f62010-07-06 01:34:17 +00006086void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6087 const ObjCAtTryStmt &S) {
6088 // Jump destination for falling out of catch bodies.
6089 CodeGenFunction::JumpDest Cont;
6090 if (S.getNumCatchStmts())
6091 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006092
John McCallf1549f62010-07-06 01:34:17 +00006093 CodeGenFunction::FinallyInfo FinallyInfo;
6094 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6095 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6096 ObjCTypes.getObjCBeginCatchFn(),
6097 ObjCTypes.getObjCEndCatchFn(),
6098 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006099
John McCallf1549f62010-07-06 01:34:17 +00006100 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006101
John McCallf1549f62010-07-06 01:34:17 +00006102 // Enter the catch, if there is one.
6103 if (S.getNumCatchStmts()) {
6104 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6105 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006106 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006107
John McCallf1549f62010-07-06 01:34:17 +00006108 Handlers.push_back(CatchHandler());
6109 CatchHandler &Handler = Handlers.back();
6110 Handler.Variable = CatchDecl;
6111 Handler.Body = CatchStmt->getCatchBody();
6112 Handler.Block = CGF.createBasicBlock("catch");
6113
6114 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006115 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006116 Handler.TypeInfo = 0; // catch-all
6117 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006118 break;
6119 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006120
John McCall5a180392010-07-24 00:37:23 +00006121 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006122 }
John McCallf1549f62010-07-06 01:34:17 +00006123
6124 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6125 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6126 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006127 }
John McCallf1549f62010-07-06 01:34:17 +00006128
6129 // Emit the try body.
6130 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006131
John McCallf1549f62010-07-06 01:34:17 +00006132 // Leave the try.
6133 if (S.getNumCatchStmts())
6134 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006135
John McCallf1549f62010-07-06 01:34:17 +00006136 // Remember where we were.
6137 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006138
John McCallf1549f62010-07-06 01:34:17 +00006139 // Emit the handlers.
6140 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6141 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006142
John McCallf1549f62010-07-06 01:34:17 +00006143 CGF.EmitBlock(Handler.Block);
6144 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006145
John McCallf1549f62010-07-06 01:34:17 +00006146 // Enter the catch.
6147 llvm::CallInst *Exn =
6148 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6149 "exn.adjusted");
6150 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006151
John McCallf1549f62010-07-06 01:34:17 +00006152 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006153 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006154 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6155 EndCatchMightThrow,
6156 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006157
John McCallf1549f62010-07-06 01:34:17 +00006158 // Bind the catch parameter if it exists.
6159 if (const VarDecl *CatchParam = Handler.Variable) {
6160 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6161 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006162
John McCallb6bbcc92010-10-15 04:57:14 +00006163 CGF.EmitAutoVarDecl(*CatchParam);
John McCallf1549f62010-07-06 01:34:17 +00006164 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006165 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006166
John McCallf1549f62010-07-06 01:34:17 +00006167 CGF.ObjCEHValueStack.push_back(Exn);
6168 CGF.EmitStmt(Handler.Body);
6169 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006170
John McCallf1549f62010-07-06 01:34:17 +00006171 // Leave the earlier cleanup.
6172 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006173
John McCallf1549f62010-07-06 01:34:17 +00006174 CGF.EmitBranchThroughCleanup(Cont);
6175 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006176
John McCallf1549f62010-07-06 01:34:17 +00006177 // Go back to the try-statement fallthrough.
6178 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006179
John McCallf1549f62010-07-06 01:34:17 +00006180 // Pop out of the normal cleanup on the finally.
6181 if (S.getFinallyStmt())
6182 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006183
John McCallff8e1152010-07-23 21:56:41 +00006184 if (Cont.isValid())
6185 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006186}
6187
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006188/// EmitThrowStmt - Generate code for a throw statement.
6189void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6190 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006191 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006192 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006193 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6194 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00006195 llvm::Value *Args[] = { Exception };
6196 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6197 Args, Args+1)
6198 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006199 } else {
John McCall7ec404c2010-10-16 08:21:07 +00006200 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6201 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006202 }
6203
John McCall7ec404c2010-10-16 08:21:07 +00006204 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006205 CGF.Builder.ClearInsertionPoint();
6206}
Daniel Dunbare588b992009-03-01 04:46:24 +00006207
John McCall5a180392010-07-24 00:37:23 +00006208llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006209CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006210 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006211 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006212
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006213 // If we don't need a definition, return the entry if found or check
6214 // if we use an external reference.
6215 if (!ForDefinition) {
6216 if (Entry)
6217 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006218
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006219 // If this type (or a super class) has the __objc_exception__
6220 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006221 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006222 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006223 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006224 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006225 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006226 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006227 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006229
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006230 // Otherwise we need to either make a new entry or fill in the
6231 // initializer.
6232 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006233 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006234 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006235 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006236 CGM.getModule().getGlobalVariable(VTableName);
6237 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006238 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6239 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006240 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006241 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006242
Chris Lattner77b89b82010-06-27 07:15:29 +00006243 llvm::Value *VTableIdx =
6244 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006245
6246 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006247 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006248 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006249 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006250 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006251 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006252
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006253 if (Entry) {
6254 Entry->setInitializer(Init);
6255 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006256 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006257 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006258 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006259 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006260 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006261 }
6262
John McCall1fb0caa2010-10-22 21:05:15 +00006263 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006264 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006265 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6266 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006267
6268 if (ForDefinition) {
6269 Entry->setSection("__DATA,__objc_const");
6270 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6271 } else {
6272 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6273 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006274
6275 return Entry;
6276}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006277
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006278/* *** */
6279
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006280CodeGen::CGObjCRuntime *
6281CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006282 return new CGObjCMac(CGM);
6283}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006284
6285CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006286CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006287 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006288}