blob: be7c79cbb5638164396115217bb49dc35d98c78c [file] [log] [blame]
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045// 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 Dunbar0cec95f2009-05-03 08:55:17 +000049static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
50 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000051 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000052 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000053 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000054
Daniel Dunbar7e5aba42010-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 Dunbar031d4d42010-04-02 15:43:29 +000058
Daniel Dunbar80b4eef2009-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 Dunbar59e476b2009-08-03 17:06:42 +000061 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-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 Dunbar8c7f9812010-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 Chisnalle8431a72010-11-03 16:12:44 +000081 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000082
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000083 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000084}
85
86uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
87 const ObjCInterfaceDecl *OID,
88 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-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 Dunbar9fd114d2009-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 Dunbar0cec95f2009-05-03 08:55:17 +0000104 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000105 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000106 QualType IvarTy = Ivar->getType();
107 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000108 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000109 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000110 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000111
Daniel Dunbarf166a522010-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 Dunbar961202372009-05-03 12:57:56 +0000117
Daniel Dunbara70fab82010-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 Dunbarb76c4cd2010-04-05 16:20:33 +0000139 uint64_t BitFieldSize =
140 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000141
Daniel Dunbardc406b82010-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 Dunbara70fab82010-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 Dunbardc406b82010-04-05 21:36:35 +0000151
Daniel Dunbar26d2c392010-08-21 04:05:54 +0000152 return LValue::MakeBitfield(V, *Info,
153 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000154}
155
156///
157
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000158namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000159
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000161
Daniel Dunbar59e476b2009-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 Dunbarb036db82008-08-13 03:21:16 +0000164
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000165class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000166protected:
167 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000168
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000176 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
177 Params, true),
178 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000179 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000180
Fariborz Jahanian5d5ed2d2009-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 Anderson41a75022009-08-13 21:57:51 +0000187 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000188 Params, true),
189 "objc_msgSend_stret");
190
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000191 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000192
Fariborz Jahanian5d5ed2d2009-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 Anderson41a75022009-08-13 21:57:51 +0000200 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
201 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000202 Params,
203 true),
204 "objc_msgSend_fpret");
205
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000206 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000207
Fariborz Jahanian5d5ed2d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000214 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000215 Params, true),
216 SuperName);
217 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000218
Fariborz Jahanian5d5ed2d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000225 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000226 Params, true),
227 SuperName);
228 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000229
Fariborz Jahanian5d5ed2d2009-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 Anderson170229f2009-07-14 23:10:40 +0000237 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000238 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000239 Params, true),
240 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000241 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000242
Fariborz Jahanian5d5ed2d2009-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 Anderson170229f2009-07-14 23:10:40 +0000250 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000251 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000252 Params, true),
253 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000254 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000255
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000260
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000265
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000266protected:
267 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000268
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000269public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000270 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000271 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Daniel Dunbar5d715592008-08-12 05:28:47 +0000273 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
274 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000275
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000276 /// PtrObjectPtrTy - LLVM type for id *
277 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000278
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000279 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000280 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000281 /// ProtocolPtrTy - LLVM type for external protocol handles
282 /// (typeof(Protocol))
283 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000284
Daniel Dunbarc722b852008-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 Dunbar59e476b2009-08-03 17:06:42 +0000289
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000290 /// SuperTy - LLVM type for struct objc_super.
291 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000292 /// SuperPtrTy - LLVM type for struct objc_super *.
293 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000294
Fariborz Jahanianb15a3d52009-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 Dunbar59e476b2009-08-03 17:06:42 +0000298
Fariborz Jahanianb15a3d52009-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 Dunbar59e476b2009-08-03 17:06:42 +0000304
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000305 // MethodTy - LLVM type for struct objc_method.
306 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000307
Fariborz Jahanian0232c052009-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 Dunbar59e476b2009-08-03 17:06:42 +0000312
Chris Lattnerce8754e2009-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 McCall2da83a32010-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 Lattnerce8754e2009-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 McCallab26cfa2010-02-05 21:31:56 +0000325 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000326 FunctionType::ExtInfo()),
327 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000328 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
329 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000330
Chris Lattnerce8754e2009-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 McCall2da83a32010-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 Lattnerce8754e2009-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 McCallab26cfa2010-02-05 21:31:56 +0000345 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000346 FunctionType::ExtInfo()),
347 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000348 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
349 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000350
Fariborz Jahanian5a8c2032010-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 Lattnerce8754e2009-04-22 02:44:54 +0000369 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000370 CodeGen::CodeGenTypes &Types = CGM.getTypes();
371 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000372 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000373 llvm::SmallVector<CanQualType,1> Params;
374 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000375 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000376 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000377 FunctionType::ExtInfo()),
378 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000379 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
380 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000382 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000387 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000388 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000390 }
391
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000392 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000398 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
400 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000401
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000402 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-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 Anderson170229f2009-07-14 23:10:40 +0000407 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000408 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
410 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000411
Fariborz Jahanian217af242010-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 Jahanianeee54df2009-01-22 00:37:21 +0000422 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000423 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000424 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000425 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
426 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000427 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000428 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000429 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000430 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
431 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000432
Fariborz Jahanian5f21d2f2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000439 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000440 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
441 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000442
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000443 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000444 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000445 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000446 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
447 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000448 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000449 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
451 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000452
453 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-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 Anderson41a75022009-08-13 21:57:51 +0000458 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000459 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
460 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000461
Fariborz Jahanian3336de12010-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 McCall17afe452010-10-16 08:21:07 +0000467 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000468 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
469 }
470
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000471 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-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 Anderson41a75022009-08-13 21:57:51 +0000476 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000477 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
478 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000479
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000480 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-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 Anderson41a75022009-08-13 21:57:51 +0000485 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000486 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
487 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000488
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000489 llvm::Constant *getSendFn(bool IsSuper) const {
490 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
491 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000492
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000493 llvm::Constant *getSendFn2(bool IsSuper) const {
494 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
495 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000496
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000497 llvm::Constant *getSendStretFn(bool IsSuper) const {
498 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
499 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000500
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000501 llvm::Constant *getSendStretFn2(bool IsSuper) const {
502 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
503 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000504
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000505 llvm::Constant *getSendFpretFn(bool IsSuper) const {
506 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
507 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000508
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000509 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
510 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
511 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000512
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000513 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
514 ~ObjCCommonTypesHelper(){}
515};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000516
Fariborz Jahanian279eda62009-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 Jahanian279eda62009-01-21 22:04:16 +0000520public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000521 /// SymtabTy - LLVM type for struct objc_symtab.
522 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000523 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
524 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000525 /// ModuleTy - LLVM type for struct objc_module.
526 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000527
Daniel Dunbarb036db82008-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 Dunbarb036db82008-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 Dunbar938a77f2008-08-22 20:34:54 +0000551 /// CategoryTy - LLVM type for struct objc_category.
552 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-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 Dunbar22d82ed2008-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 Dunbar22d82ed2008-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 Dunbar59e476b2009-08-03 17:06:42 +0000571
Anders Carlsson9ff22482008-09-09 10:10:21 +0000572 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
573 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000574
Anders Carlsson9ff22482008-09-09 10:10:21 +0000575 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000576 llvm::Constant *getExceptionTryEnterFn() {
577 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000578 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000579 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000580 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000581 Params, false),
582 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000583 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000584
585 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000586 llvm::Constant *getExceptionTryExitFn() {
587 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000588 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000589 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000590 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000591 Params, false),
592 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000593 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000594
595 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000596 llvm::Constant *getExceptionExtractFn() {
597 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000598 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
599 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000600 Params, false),
601 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Chris Lattnerc6406db2009-04-22 02:26:14 +0000603 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604
Anders Carlsson9ff22482008-09-09 10:10:21 +0000605 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000610 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000611 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000612 Params, false),
613 "objc_exception_match");
614
Chris Lattnerc6406db2009-04-22 02:26:14 +0000615 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000616
Anders Carlsson9ff22482008-09-09 10:10:21 +0000617 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000618 llvm::Constant *getSetJmpFn() {
619 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000620 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000621 return
Owen Anderson41a75022009-08-13 21:57:51 +0000622 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000623 Params, false),
624 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Chris Lattnerc6406db2009-04-22 02:26:14 +0000626 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000627
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000628public:
629 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000630 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000631};
632
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000633/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000634/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000635class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000636public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000638 // MethodListnfABITy - LLVM for struct _method_list_t
639 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000641 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
642 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000643
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000644 // ProtocolnfABITy = LLVM for struct _protocol_t
645 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000647 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
648 const llvm::Type *ProtocolnfABIPtrTy;
649
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000650 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
651 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000653 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
654 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000656 // ClassnfABITy - LLVM for struct _class_t
657 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Fariborz Jahanian71394042009-01-23 23:53:38 +0000659 // ClassnfABIPtrTy - LLVM for struct _class_t*
660 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000662 // IvarnfABITy - LLVM for struct _ivar_t
663 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000665 // IvarListnfABITy - LLVM for struct _ivar_list_t
666 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000668 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
669 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000671 // ClassRonfABITy - LLVM for struct _class_ro_t
672 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000674 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
675 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000676
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000677 // CategorynfABITy - LLVM for struct _category_t
678 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000679
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000680 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000681
Fariborz Jahanian82c72e12009-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 Jahaniane4dc35d2009-02-04 20:42:28 +0000688 // MessageRefCTy - clang type for struct _message_ref_t
689 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000690
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000691 // MessageRefPtrTy - LLVM for struct _message_ref_t*
692 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000693 // MessageRefCPtrTy - clang type for struct _message_ref_t*
694 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000696 // MessengerTy - Type of the messenger (shown as IMP above)
697 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000698
Fariborz Jahanian82c72e12009-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 Dunbar59e476b2009-08-03 17:06:42 +0000705
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000706 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
707 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000708
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000714 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000715 Params, true),
716 "objc_msgSend_fixup");
717 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000718
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000724 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000725 Params, true),
726 "objc_msgSend_fpret_fixup");
727 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000734 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000735 Params, true),
736 "objc_msgSend_stret_fixup");
737 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000738
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000739 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000740 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000745 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000746 Params, true),
747 "objc_msgSendSuper2_fixup");
748 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000750 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000751 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000756 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000757 Params, true),
758 "objc_msgSendSuper2_stret_fixup");
759 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000760
Chris Lattnera7c00b42009-04-22 02:15:23 +0000761 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000762 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000763 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000764 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000765
Chris Lattnera7c00b42009-04-22 02:15:23 +0000766 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000767
Chris Lattnera7c00b42009-04-22 02:15:23 +0000768 llvm::Constant *getObjCBeginCatchFn() {
769 std::vector<const llvm::Type*> Params;
770 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000771 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000772 Params, false),
773 "objc_begin_catch");
774 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000775
776 const llvm::StructType *EHTypeTy;
777 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000778
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000779 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
780 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000781};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000782
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000783class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000784public:
785 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000786 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000787 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000788 unsigned ivar_bytepos;
789 unsigned ivar_size;
790 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-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 Jahanian524bb202009-03-10 16:22:08 +0000797 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000798
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000799 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000800 public:
801 unsigned skip;
802 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000803 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000804 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000805 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000806
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000807protected:
808 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000809 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000810 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000811 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000812
Fariborz Jahaniancbaf73c2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000816
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000817 /// LazySymbols - Symbols to generate a lazy reference for. See
818 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000819 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000820
Daniel Dunbarc61d0e92008-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 Dunbard027a922009-09-07 00:20:42 +0000825 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000826
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000827 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000828 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000830 /// MethodVarNames - uniqued method variable names.
831 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000833 /// DefinedCategoryNames - list of category names in form Class_Category.
834 llvm::SetVector<std::string> DefinedCategoryNames;
835
Daniel Dunbarb036db82008-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 Dunbar59e476b2009-08-03 17:06:42 +0000839
Daniel Dunbar3c76cb52008-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 Dunbar59e476b2009-08-03 17:06:42 +0000843
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000844 /// PropertyNames - uniqued method variable names.
845 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000847 /// ClassReferences - uniqued class references.
848 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000850 /// SelectorReferences - uniqued selector references.
851 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Daniel Dunbarb036db82008-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 Dunbar59e476b2009-08-03 17:06:42 +0000857
Daniel Dunbarc475d422008-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 Dunbar59e476b2009-08-03 17:06:42 +0000861
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000862 /// DefinedClasses - List of defined classes.
863 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000864
865 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
866 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000867
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000868 /// DefinedCategories - List of defined categories.
869 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000870
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000871 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
872 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000873
Fariborz Jahanian0b1ccdc2009-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 Dunbard2386812009-10-19 01:21:19 +0000878 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000879
Fariborz Jahanian0b1ccdc2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000884
Fariborz Jahanian0b1ccdc2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000887
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000888 // FIXME: This is a horrible name.
889 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000890 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000891
Fariborz Jahanian0b1ccdc2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000895
Fariborz Jahanian0b1ccdc2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000899
Fariborz Jahanian9e3ad522009-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 Dunbar59e476b2009-08-03 17:06:42 +0000903
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000904 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
905
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000906 /// BuildIvarLayout - Builds ivar layout bitmap for the class
907 /// implementation for the __strong or __weak case.
908 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000909 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
910 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000911
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000912 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000914 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000915 unsigned int BytePos, bool ForStrongLayout,
916 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000917 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000918 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000919 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000920 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000921 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000922 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000923
Fariborz Jahanian01dff422009-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 Dunbar59e476b2009-08-03 17:06:42 +0000928
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000929 /// EmitPropertyList - Emit the given property list. The return
930 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000931 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000932 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000933 const ObjCContainerDecl *OCD,
934 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000935
Fariborz Jahanian751c1e72009-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 Jahanian56b3b772009-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 Jahanian67726212009-03-08 20:18:37 +0000947
Daniel Dunbar30c65362009-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 Lattnerf56501c2009-07-17 23:57:13 +0000953 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-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 Dunbar4527d302009-04-14 17:42:51 +0000961 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000962 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000963 llvm::Constant *Init,
964 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000965 unsigned Align,
966 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000967
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000968 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000969 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-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 Dunbaraff9fca2009-09-17 04:01:22 +0000976 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000977 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000978
Daniel Dunbar5e639272010-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 Jahanian279eda62009-01-21 22:04:16 +0000983public:
Owen Andersonae86c192009-07-13 04:10:07 +0000984 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000985 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000986
David Chisnall481e3a82010-01-23 02:40:42 +0000987 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000988
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000989 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
990 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000991
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000992 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000993
Fariborz Jahanian56b3b772009-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 Dunbar59e476b2009-08-03 17:06:42 +0000998
Fariborz Jahanian56b3b772009-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 McCall351762c2011-02-07 10:33:21 +00001004 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1005 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001006
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001007};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001008
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001009class CGObjCMac : public CGObjCCommonMac {
1010private:
1011 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001012
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001013 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001014 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001015 void EmitModuleInfo();
1016
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001017 /// EmitModuleSymols - Emit module symbols, the list of defined
1018 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001019 llvm::Constant *EmitModuleSymbols();
1020
Daniel Dunbar3ad53482008-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 Dunbarb036db82008-08-13 03:21:16 +00001024
Daniel Dunbar22d82ed2008-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 Dunbar59e476b2009-08-03 17:06:42 +00001032 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001033 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001034
1035 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1036 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-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 Jahanianb042a592009-01-28 19:12:34 +00001044 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001045
Daniel Dunbarca8531a2008-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 Dunbar22d82ed2008-08-21 04:36:09 +00001051 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001052 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001053 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1054 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001055 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001056
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001057 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001058
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001059 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001060
1061 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001062 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001063 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001064 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001065 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001066
1067 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001068 /// method declarations.
Daniel Dunbarb036db82008-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 Dunbar349e6fb2009-10-18 20:48:59 +00001078 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001079 const char *Section,
1080 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001081
Daniel Dunbarc475d422008-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 Jahanian56b3b772009-01-29 19:24:30 +00001085 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-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 Jahanian56b3b772009-01-29 19:24:30 +00001091 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001092
Daniel Dunbarb036db82008-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 Dunbareb1f9a22008-08-27 02:31:56 +00001097 llvm::Constant *
1098 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1099 const ConstantVector &OptInstanceMethods,
1100 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001101
1102 /// EmitProtocolList - Generate the list of referenced
1103 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001104 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001105 ObjCProtocolDecl::protocol_iterator begin,
1106 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001107
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001108 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1109 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001110 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1111 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001112
1113public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001114 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001115
Fariborz Jahanian71394042009-01-23 23:53:38 +00001116 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001117
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001118 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001119 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001120 QualType ResultType,
1121 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001122 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001123 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001124 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001125 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001126
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001127 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001128 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001129 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001130 QualType ResultType,
1131 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001132 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001133 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001134 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001135 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001136 const CallArgList &CallArgs,
1137 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138
Daniel Dunbarcb463852008-11-01 01:53:16 +00001139 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001140 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001141
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001142 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1143 bool lval = false);
Fariborz Jahanianf3648b82009-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 McCall2ca705e2010-07-24 00:37:23 +00001150 virtual llvm::Constant *GetEHType(QualType T);
1151
Daniel Dunbar92992502008-08-15 22:20:32 +00001152 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001153
Daniel Dunbar92992502008-08-15 22:20:32 +00001154 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001155
Daniel Dunbarcb463852008-11-01 01:53:16 +00001156 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001157 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001158
Chris Lattnerd4808922009-03-22 21:03:39 +00001159 virtual llvm::Constant *GetPropertyGetFunction();
1160 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001161 virtual llvm::Constant *GetGetStructFunction();
1162 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001163 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001164
John McCallbd309292010-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 Carlsson1963b0c2008-09-09 10:04:29 +00001170 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1171 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001172 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001173 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001174 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001175 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001176 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001177 llvm::Value *src, llvm::Value *dest,
1178 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001179 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001180 llvm::Value *src, llvm::Value *dest,
1181 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001182 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1183 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001184 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1185 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001186 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001187
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001188 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1189 QualType ObjectTy,
1190 llvm::Value *BaseValue,
1191 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001192 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001193 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001194 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001195 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001196};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001197
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001198class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001199private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001200 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001201 llvm::GlobalVariable* ObjCEmptyCacheVar;
1202 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001203
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001204 /// SuperClassReferences - uniqued super class references.
1205 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001206
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001207 /// MetaClassReferences - uniqued meta class references.
1208 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001209
1210 /// EHTypeReferences - uniqued class ehtype references.
1211 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001213 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001214 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001215 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001216
Fariborz Jahanian67260552009-11-17 21:37:35 +00001217 /// DefinedMetaClasses - List of defined meta-classes.
1218 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1219
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001220 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001221 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001222 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001223
Fariborz Jahanian71394042009-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 Dunbar8f28d012009-04-08 04:21:03 +00001227
Daniel Dunbar19573e72009-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 Dunbar59e476b2009-08-03 17:06:42 +00001234 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1235 unsigned InstanceStart,
1236 unsigned InstanceSize,
1237 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001238 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001239 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001240 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001241 llvm::Constant *ClassRoGV,
1242 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001244 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001245
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001246 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001247
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001248 /// EmitMethodList - Emit the method list for the given
1249 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001250 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001251 const char *Section,
Fariborz Jahanian7415caa2009-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 Dunbar59e476b2009-08-03 17:06:42 +00001259
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001260 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001261 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001262 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001263
Fariborz Jahanian56b3b772009-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 Dunbar59e476b2009-08-03 17:06:42 +00001268
Fariborz Jahanian56b3b772009-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 Dunbar59e476b2009-08-03 17:06:42 +00001274
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001275 /// EmitProtocolList - Generate the list of referenced
1276 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001277 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001278 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001279 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001280
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001281 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001282 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001283 QualType ResultType,
1284 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001285 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001286 QualType Arg0Ty,
1287 bool IsSuper,
1288 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001289
1290 /// GetClassGlobal - Return the global variable for the Objective-C
1291 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001292 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001293
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001294 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001295 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001296 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001297 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001298
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001299 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1300 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001301 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1302 const ObjCInterfaceDecl *ID);
1303
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001304 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1305 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001307 const ObjCInterfaceDecl *ID);
1308
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001309 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1310 /// the given ivar.
1311 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001312 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001313 const ObjCInterfaceDecl *ID,
1314 const ObjCIvarDecl *Ivar);
1315
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001316 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1317 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001318 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1319 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001320
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001321 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001322 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001323 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001324 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001325
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001326 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001327 return "OBJC_METACLASS_$_";
1328 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001329
Daniel Dunbar15894b72009-04-07 05:48:37 +00001330 const char *getClassSymbolPrefix() const {
1331 return "OBJC_CLASS_$_";
1332 }
1333
Daniel Dunbar961202372009-05-03 12:57:56 +00001334 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001335 uint32_t &InstanceStart,
1336 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001337
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001338 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001339 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001340 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1341 return CGM.getContext().Selectors.getSelector(0, &II);
1342 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001343
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001344 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001345 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1346 return CGM.getContext().Selectors.getSelector(1, &II);
1347 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001348
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001349 /// ImplementationIsNonLazy - Check whether the given category or
1350 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001351 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001352
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001353public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001354 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001355 // FIXME. All stubs for now!
1356 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001357
Fariborz Jahanian71394042009-01-23 23:53:38 +00001358 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001359 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001360 QualType ResultType,
1361 Selector Sel,
1362 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001363 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001364 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001365 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001366
1367 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001368 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001369 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001370 QualType ResultType,
1371 Selector Sel,
1372 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001373 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001374 llvm::Value *Receiver,
1375 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001376 const CallArgList &CallArgs,
1377 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001378
Fariborz Jahanian71394042009-01-23 23:53:38 +00001379 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001380 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001381
Fariborz Jahanian9240f3d2010-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 Jahanianf3648b82009-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 Dunbar59e476b2009-08-03 17:06:42 +00001391
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001392 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393
Fariborz Jahanian71394042009-01-23 23:53:38 +00001394 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001395 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001396 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001397
John McCall2ca705e2010-07-24 00:37:23 +00001398 virtual llvm::Constant *GetEHType(QualType T);
1399
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001400 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001401 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001402 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001403 virtual llvm::Constant *GetPropertySetFunction() {
1404 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001405 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001406
David Chisnall168b80f2010-12-26 22:13:16 +00001407 virtual llvm::Constant *GetSetStructFunction() {
1408 return ObjCTypes.getCopyStructFn();
1409 }
1410 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001411 return ObjCTypes.getCopyStructFn();
1412 }
1413
Chris Lattnerd4808922009-03-22 21:03:39 +00001414 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001415 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001416 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001417
John McCallbd309292010-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 Jahanian71394042009-01-23 23:53:38 +00001422 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001423 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001424 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001425 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001426 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001427 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001428 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001429 llvm::Value *src, llvm::Value *dest,
1430 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001431 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001432 llvm::Value *src, llvm::Value *dest,
1433 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001434 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001435 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001436 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1437 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001438 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001439 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1440 QualType ObjectTy,
1441 llvm::Value *BaseValue,
1442 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001443 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001444 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001445 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001446 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001447};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001448
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001449} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001450
1451/* *** Helper Functions *** */
1452
1453/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001454static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001455 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001456 unsigned idx0,
1457 unsigned idx1) {
1458 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001459 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1460 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001461 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001462 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001463}
1464
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001465/// hasObjCExceptionAttribute - Return true if this class or any super
1466/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001467static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001468 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001469 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001470 return true;
1471 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001472 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001473 return false;
1474}
1475
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001476/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001477
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001478CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001479 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001480 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001481 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001482}
1483
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001484/// GetClass - Return a reference to the class for the given interface
1485/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001486llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001487 const ObjCInterfaceDecl *ID) {
1488 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001489}
1490
1491/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001492llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1493 bool lval) {
1494 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001495}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001496llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001497 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001498 return EmitSelector(Builder, Method->getSelector());
1499}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001500
John McCall2ca705e2010-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 Dunbar8b8683f2008-08-12 00:12:39 +00001506/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-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 Dunbar8b8683f2008-08-12 00:12:39 +00001514*/
1515
Fariborz Jahanian63408e82010-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 Jahanian71394042009-01-23 23:53:38 +00001525llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001526 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001527 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1528 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001529 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-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 Dunbar97db84c2008-08-23 03:46:30 +00001535CodeGen::RValue
1536CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001537 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001538 QualType ResultType,
1539 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001540 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001541 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001542 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001543 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001544 const CodeGen::CallArgList &CallArgs,
1545 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-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 Dunbar59e476b2009-08-03 17:06:42 +00001548 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001549 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001550 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001551 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001552 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001553 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001554
Daniel Dunbarca8531a2008-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 Jahanianbac73ac2009-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 Stump658fe022009-07-30 22:28:39 +00001568 } else {
Fariborz Jahanianbac73ac2009-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 Dunbar59e476b2009-08-03 17:06:42 +00001573 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001574 }
1575 else if (isCategoryImpl)
1576 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1577 else {
Fariborz Jahanianeb80c982009-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 Dunbarca8531a2008-08-25 08:19:24 +00001581 }
Mike Stump18bb9282009-05-16 07:57:57 +00001582 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1583 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001584 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001585 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001586 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001587 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001588 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001589 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001590 EmitSelector(CGF.Builder, Sel),
1591 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001592 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001593}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001594
1595/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001596CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001597 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001598 QualType ResultType,
1599 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001600 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001601 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001602 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001603 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001604 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001605 EmitSelector(CGF.Builder, Sel),
1606 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001607 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001608}
1609
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001610CodeGen::RValue
1611CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001612 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-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 Dunbarc722b852008-08-30 03:02:31 +00001621 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001622 if (!IsSuper)
1623 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001624 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001625 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001626 CGF.getContext().getObjCSelType()));
1627 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001628
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001629 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001630 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001631 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001632 const llvm::FunctionType *FTy =
1633 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001634
Anders Carlsson280e61f12010-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 Jahanian5d5ed2d2009-05-11 19:25:47 +00001640 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001641 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001642 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001643 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001644 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1645 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1646 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001647 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001648 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001649 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001650 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001651 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001652 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001653}
1654
Fariborz Jahanian9659f6b2010-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 McCall351762c2011-02-07 10:33:21 +00001671llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1672 const CGBlockInfo &blockInfo) {
1673 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001674 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001675
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001676 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001677 return nullPtr;
1678
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001679 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-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 Jahaniancfddabf2010-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 McCall351762c2011-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 Jahanian933c6722010-09-11 01:27:29 +00001714 continue;
John McCall351762c2011-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 Jahanian903aba32010-08-05 21:00:25 +00001720 continue;
1721 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001722
John McCall351762c2011-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 Jahanian9659f6b2010-08-04 23:55:24 +00001729 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001730 SkipIvars.push_back(GC_IVAR(fieldOffset,
1731 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001732 }
1733
1734 if (IvarsInfo.empty())
John McCall351762c2011-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 Jahanian9659f6b2010-08-04 23:55:24 +00001741
1742 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001743 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-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 Jahanianc05349e2010-08-04 16:57:49 +00001756}
1757
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001758llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001759 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001760 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001761 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001762 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1763
Owen Andersonade90fd2009-07-29 18:54:39 +00001764 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001765 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001766}
1767
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001768void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-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 Dunbarc475d422008-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 Dunbar59e476b2009-08-03 17:06:42 +00001776 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001777 GetOrEmitProtocol(PD);
1778}
1779
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001780llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001781 if (DefinedProtocols.count(PD->getIdentifier()))
1782 return GetOrEmitProtocol(PD);
1783 return GetOrEmitProtocolRef(PD);
1784}
1785
Daniel Dunbarb036db82008-08-13 03:21:16 +00001786/*
Daniel Dunbar59e476b2009-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 Dunbarb036db82008-08-13 03:21:16 +00001795
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001796See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001797*/
Daniel Dunbarc475d422008-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 Dunbarc61d0e92008-08-25 06:02:07 +00001805 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001806 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001807 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1808
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001809 // Construct method lists.
1810 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1811 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001813 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-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 Dunbar59e476b2009-08-03 17:06:42 +00001820 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001821 }
1822
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001823 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001824 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-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 Dunbar59e476b2009-08-03 17:06:42 +00001831 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001832 }
1833
Daniel Dunbarb036db82008-08-13 03:21:16 +00001834 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001835 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001836 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001837 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001838 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001839 PD->protocol_begin(),
1840 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001841 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001842 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001843 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1844 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001845 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001846 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001847 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1848 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001849 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001850 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851
Daniel Dunbarb036db82008-08-13 03:21:16 +00001852 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001853 // Already created, fix the linkage and update the initializer.
1854 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001855 Entry->setInitializer(Init);
1856 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001857 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001858 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001859 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001860 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001861 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863 // FIXME: Is this necessary? Why only for protocol?
1864 Entry->setAlignment(4);
1865 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001866 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001867
1868 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869}
1870
Daniel Dunbarc475d422008-10-29 22:36:39 +00001871llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001872 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1873
1874 if (!Entry) {
Daniel Dunbarc475d422008-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 Dunbar59e476b2009-08-03 17:06:42 +00001878 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001879 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001880 llvm::GlobalValue::ExternalLinkage,
1881 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001882 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001883 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001884 // FIXME: Is this necessary? Why only for protocol?
1885 Entry->setAlignment(4);
1886 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887
Daniel Dunbarb036db82008-08-13 03:21:16 +00001888 return Entry;
1889}
1890
1891/*
1892 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-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 Dunbarb036db82008-08-13 03:21:16 +00001897 };
1898*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001899llvm::Constant *
1900CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1901 const ConstantVector &OptInstanceMethods,
1902 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001903 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001904 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001905 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001906 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001907 Values[1] =
1908 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001909 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001910 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1911 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001912 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001913 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001914 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1915 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001916 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001917 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001918
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001919 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001920 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001921 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001922 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001924 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001925 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001926
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001927 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001928 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001929 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001930 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001931}
1932
1933/*
1934 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001935 struct objc_protocol_list *next;
1936 long count;
1937 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001938 };
1939*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001940llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001941CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001942 ObjCProtocolDecl::protocol_iterator begin,
1943 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001944 std::vector<llvm::Constant*> ProtocolRefs;
1945
Daniel Dunbardec75f82008-08-21 21:57:41 +00001946 for (; begin != end; ++begin)
1947 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001948
1949 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001950 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001951 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001952
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001953 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001954 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001955
1956 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001957 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001958 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001959 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-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 Dunbarb036db82008-08-13 03:21:16 +00001964 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001965
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001966 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001968 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001969 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001970 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001971}
1972
Fariborz Jahanian751c1e72009-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 Dunbarb036db82008-08-13 03:21:16 +00001993/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001994 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001995 const char * const name;
1996 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001997 };
1998
1999 struct _objc_property_list {
Daniel Dunbar59e476b2009-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 Dunbar80a840b2008-08-23 00:19:03 +00002003 };
2004*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002005llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002006 const Decl *Container,
2007 const ObjCContainerDecl *OCD,
2008 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002009 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002010 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002011 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2012 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002013 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002014 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002015 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00002016 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002017 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002018 Prop));
2019 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002020 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-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 Jahanian7966aff2010-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 Dunbar80a840b2008-08-23 00:19:03 +00002033
2034 // Return null for empty list.
2035 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002036 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002037
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002038 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002039 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002040 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-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 Dunbar59e476b2009-08-03 17:06:42 +00002043 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002044 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002045 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002046 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002047
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002048 llvm::GlobalVariable *GV =
2049 CreateMetadataVar(Name, Init,
2050 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002051 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002052 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002053 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002054 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002055}
2056
2057/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002058 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002059 int count;
2060 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002061 };
2062*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002063llvm::Constant *
2064CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2065 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002066 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002067 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2068 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002069 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002070 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002071 Desc);
2072}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002073
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002074llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002075 const char *Section,
2076 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002077 // Return null for empty list.
2078 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002079 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002080
2081 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002082 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002083 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002084 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002085 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002086 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002087
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002088 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002089 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002090 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002091}
2092
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002093/*
2094 struct _objc_category {
Daniel Dunbar59e476b2009-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 Dunbar938a77f2008-08-22 20:34:54 +00002102 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002103*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002104void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002105 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002106
Mike Stump18bb9282009-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 Dunbar938a77f2008-08-22 20:34:54 +00002111 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002112 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002113 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002114
2115 llvm::SmallString<256> ExtName;
2116 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2117 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002118
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002119 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002120 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002121 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002122 // Instance methods should always be defined.
2123 InstanceMethods.push_back(GetMethodConstant(*i));
2124 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002125 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002126 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002127 // Class methods should always be defined.
2128 ClassMethods.push_back(GetMethodConstant(*i));
2129 }
2130
Daniel Dunbar938a77f2008-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 Jahaniane55f8662009-04-29 20:40:05 +00002134 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002135 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002136 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002137 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002138 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002140 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002141 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002142 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002143 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002144 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002145 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002146 Category->protocol_begin(),
2147 Category->protocol_end());
2148 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002149 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002150 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002151 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002152
2153 // If there is no category @interface then there can be no properties.
2154 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002155 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002156 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002157 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002158 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002159 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002160
Owen Anderson0e0189d2009-07-27 22:29:56 +00002161 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002162 Values);
2163
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002164 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002165 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002166 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002167 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002168 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002169 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002170}
2171
Daniel Dunbar22d82ed2008-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 Dunbar22d82ed2008-08-21 04:36:09 +00002183/*
2184 struct _objc_class {
Daniel Dunbar59e476b2009-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 Dunbar22d82ed2008-08-21 04:36:09 +00002198 };
2199
2200 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002201*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002202void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002203 DefinedSymbols.insert(ID->getIdentifier());
2204
Chris Lattner86d7d912008-11-24 03:54:41 +00002205 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002206 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002207 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002209 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002210 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002211 Interface->all_referenced_protocol_begin(),
2212 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002213 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002214 if (ID->getNumIvarInitializers())
2215 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002216 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002217 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002218
2219 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002220 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002221 Flags |= eClassFlags_Hidden;
2222
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002223 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002224 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002225 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002226 // Instance methods should always be defined.
2227 InstanceMethods.push_back(GetMethodConstant(*i));
2228 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002229 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002230 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002231 // Class methods should always be defined.
2232 ClassMethods.push_back(GetMethodConstant(*i));
2233 }
2234
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002235 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002236 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-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 Dunbar22d82ed2008-08-21 04:36:09 +00002251 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002252 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002254 // Record a reference to the super class.
2255 LazySymbols.insert(Super->getIdentifier());
2256
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002257 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002258 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002259 ObjCTypes.ClassPtrTy);
2260 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002261 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002262 }
2263 Values[ 2] = GetClassName(ID->getIdentifier());
2264 // Version is always 0.
Owen Andersonb7a2fe62009-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 Jahanianb042a592009-01-28 19:12:34 +00002268 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002269 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002270 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002271 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002272 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002273 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002274 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002275 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002276 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002277 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002278 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002279 Values);
Fariborz Jahanianeb80c982009-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 Dunbar22d82ed2008-08-21 04:36:09 +00002296 DefinedClasses.push_back(GV);
2297}
2298
2299llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2300 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002301 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002302 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002303 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002304
John McCall457a04e2010-10-22 21:05:15 +00002305 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002306 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002307
Daniel Dunbar22d82ed2008-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 Dunbar59e476b2009-08-03 17:06:42 +00002313 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002314 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002315 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-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 Dunbar22d82ed2008-08-21 04:36:09 +00002319 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002320 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002321 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002322 ObjCTypes.ClassPtrTy);
2323 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002324 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325 }
2326 Values[ 2] = GetClassName(ID->getIdentifier());
2327 // Version is always 0.
Owen Andersonb7a2fe62009-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 Jahanianb042a592009-01-28 19:12:34 +00002331 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002332 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002333 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002334 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002335 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002337 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002338 Values[ 9] = Protocols;
2339 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002340 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002341 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002342 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002343 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002344 Values);
2345
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002346 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002347 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-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 Andersonc10c8d32009-07-08 19:05:04 +00002357 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002358 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002359 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002360 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002361 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002362 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002363 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002364
2365 return GV;
2366}
2367
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002368llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002369 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002370
Mike Stump18bb9282009-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 Dunbarca8531a2008-08-25 08:19:24 +00002375
2376 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-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 Dunbar59e476b2009-08-03 17:06:42 +00002379 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2380 true)) {
Daniel Dunbarca8531a2008-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 Andersonc10c8d32009-07-08 19:05:04 +00002387 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002388 llvm::GlobalValue::ExternalLinkage,
2389 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002390 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002391 }
2392}
2393
Fariborz Jahanianeb80c982009-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 Dunbar22d82ed2008-08-21 04:36:09 +00002410/*
2411 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002412 uint32_t size;
2413 const char *weak_ivar_layout;
2414 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002415 };
2416*/
2417llvm::Constant *
2418CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002419 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002420 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421
2422 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002423 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002424 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002425 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002426 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002427
2428 // Return null if no extension bits are used.
2429 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002430 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002432 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002433 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002434 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002435 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002436 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002437}
2438
2439/*
2440 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002441 char *ivar_name;
2442 char *ivar_type;
2443 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444 };
2445
2446 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002447 int ivar_count;
2448 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002450*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002451llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002452 bool ForClass) {
Daniel Dunbar22d82ed2008-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 Anderson0b75f232009-07-31 20:28:54 +00002461 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002462
2463 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002464 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002465
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002466 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002467 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002468
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002469 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2470 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002471 // Ignore unnamed bit-fields.
2472 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002473 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002474 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2475 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002476 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002477 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002478 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002479 }
2480
2481 // Return null for empty list.
2482 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002483 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002484
2485 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002486 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002487 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002488 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002489 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002490 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002491
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002492 llvm::GlobalVariable *GV;
2493 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002494 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002495 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002496 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002497 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002498 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002499 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002500 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002501 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002502}
2503
2504/*
2505 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002506 SEL method_name;
2507 char *method_types;
2508 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002509 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002510
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002511 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002512 struct objc_method_list *obsolete;
2513 int count;
2514 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002515 };
2516*/
Daniel Dunbar3c76cb52008-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 Dunbareb1f9a22008-08-27 02:31:56 +00002521llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002522 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002523 if (!Fn)
2524 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002525
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002526 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002527 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002528 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002529 ObjCTypes.SelectorPtrTy);
2530 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002531 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002532 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002533}
2534
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002535llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002536 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002537 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002538 // Return null for empty list.
2539 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002540 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002541
2542 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002543 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002544 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002545 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002546 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002547 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002548 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002549
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002550 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002551 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002552 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002553}
2554
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002555llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002556 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002557 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002558 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002559
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002560 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002561 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002562 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002563 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002564 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002565 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002566 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002567 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002568 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002569
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002570 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002571}
2572
Daniel Dunbar30c65362009-03-09 20:09:19 +00002573llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002574CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002575 llvm::Constant *Init,
2576 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002577 unsigned Align,
2578 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002579 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002580 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002581 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002582 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002583 if (Section)
2584 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002585 if (Align)
2586 GV->setAlignment(Align);
2587 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002588 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002589 return GV;
2590}
2591
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002592llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002593 // Abuse this interface function as a place to finalize.
2594 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002595 return NULL;
2596}
2597
Chris Lattnerd4808922009-03-22 21:03:39 +00002598llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002599 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002600}
2601
Chris Lattnerd4808922009-03-22 21:03:39 +00002602llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002603 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002604}
2605
David Chisnall168b80f2010-12-26 22:13:16 +00002606llvm::Constant *CGObjCMac::GetGetStructFunction() {
2607 return ObjCTypes.getCopyStructFn();
2608}
2609llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002610 return ObjCTypes.getCopyStructFn();
2611}
2612
Chris Lattnerd4808922009-03-22 21:03:39 +00002613llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002614 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002615}
2616
John McCallbd309292010-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 McCall65bea082010-07-21 06:59:36 +00002626namespace {
John McCallcda666c2010-07-21 07:22:38 +00002627 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002628 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002629 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002630 llvm::Value *CallTryExitVar;
2631 llvm::Value *ExceptionData;
2632 ObjCTypesHelper &ObjCTypes;
2633 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002634 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002635 llvm::Value *CallTryExitVar,
2636 llvm::Value *ExceptionData,
2637 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002638 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-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 McCallcebe0ca2010-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 McCall65bea082010-07-21 06:59:36 +00002665 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2666
John McCallcebe0ca2010-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 McCall65bea082010-07-21 06:59:36 +00002675 } else {
2676 // Emit objc_sync_exit(expr); as finally's sole statement for
2677 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002678 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002679 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2680 ->setDoesNotThrow();
2681 }
2682 }
2683 };
John McCall42227ed2010-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 McCall2dd7d442010-08-04 05:59:32 +00002700
John McCall42227ed2010-07-31 23:20:56 +00002701 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002702 void emitHazardsInNewBlocks();
John McCall42227ed2010-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 McCall42227ed2010-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 McCall2dd7d442010-08-04 05:59:32 +00002769void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-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 McCall2dd7d442010-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 McCall42227ed2010-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 McCall65bea082010-07-21 06:59:36 +00002832}
2833
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002834/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002835
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002836 Objective-C setjmp-longjmp (sjlj) Exception Handling
2837 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002838
John McCallbd309292010-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 Dunbar59e476b2009-08-03 17:06:42 +00002871 The basic framework for a @try-catch-finally is as follows:
2872 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002873 objc_exception_data d;
2874 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002875 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002876
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002877 objc_exception_try_enter(&d);
2878 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002879 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002880 } else {
Daniel Dunbar59e476b2009-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 Dunbard3dcb4f82008-09-28 01:03:14 +00002897 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002898 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002899
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002900 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002901 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002902 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002903
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002904 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002905 ... dispatch to finally destination ...
2906
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002907 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002908 objc_exception_throw(_rethrow);
2909
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002910 finally_end:
2911 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002912
Daniel Dunbar59e476b2009-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 Dunbard3dcb4f82008-09-28 01:03:14 +00002917
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002918 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002919
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002920 - If there are no catch blocks, then we avoid emitting the second
2921 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002922
Daniel Dunbar59e476b2009-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 Dunbard3dcb4f82008-09-28 01:03:14 +00002925
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002926 - FIXME: If there is no @finally block we can do a few more
2927 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002928
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002929 Rethrows and Jumps-Through-Finally
2930 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002931
John McCallbd309292010-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 Dunbard3dcb4f82008-09-28 01:03:14 +00002934
John McCallbd309292010-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 Dunbar2efd5382008-09-30 01:06:03 +00002939
John McCallbd309292010-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 Dunbard3dcb4f82008-09-28 01:03:14 +00002944*/
2945
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002946void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2947 const Stmt &S) {
2948 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-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 Dunbar59e476b2009-08-03 17:06:42 +00002959
Daniel Dunbar94ceb612009-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 McCall2dd7d442010-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 Dunbar94ceb612009-02-24 01:43:46 +00002966 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002967 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002968 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2969 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002970 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2971 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002972
2973 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2974 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002975 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002976
John McCall2dd7d442010-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 McCall42227ed2010-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 McCallbd309292010-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 McCall2dd7d442010-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 McCallbd309292010-07-06 01:34:17 +00002996 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002997 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002998
John McCall9916e3f2010-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 McCallbd309292010-07-06 01:34:17 +00003003 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003004 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003005 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003006 CallTryExitVar,
3007 ExceptionData,
3008 &ObjCTypes);
John McCallbd309292010-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 Dunbar75283ff2008-11-11 02:29:29 +00003027 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3028 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003029 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003030 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3031 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003032
John McCallbd309292010-07-06 01:34:17 +00003033 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003034 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003035 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003036 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003037 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003038
3039 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003040
John McCallbd309292010-07-06 01:34:17 +00003041 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003042 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003043
John McCall42227ed2010-07-31 23:20:56 +00003044 // Don't optimize loads of the in-scope locals across this point.
3045 Hazards.emitWriteHazard();
3046
John McCallbd309292010-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 McCall2dd7d442010-08-04 05:59:32 +00003051 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003052 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003053
3054 // Otherwise, we have to match against the caught exceptions.
3055 } else {
John McCall2dd7d442010-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 McCallbd309292010-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 Gregor96c79492010-04-23 22:50:49 +00003067 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003068
John McCall2dd7d442010-08-04 05:59:32 +00003069 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003070
John McCall2dd7d442010-08-04 05:59:32 +00003071 llvm::BasicBlock *CatchBlock = 0;
3072 llvm::BasicBlock *CatchHandler = 0;
3073 if (HasFinally) {
John McCall9916e3f2010-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 McCall2dd7d442010-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 Carlsson4f1c7c32008-09-09 17:59:25 +00003084
John McCall2dd7d442010-08-04 05:59:32 +00003085 llvm::CallInst *SetJmpResult =
3086 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3087 "setjmp.result");
3088 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003089
John McCall2dd7d442010-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 Dunbar59e476b2009-08-03 17:06:42 +00003101
Daniel Dunbarb22ff592008-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 Gregor96c79492010-04-23 22:50:49 +00003106 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3107 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003108
Douglas Gregor46a572b2010-04-26 16:46:50 +00003109 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003110 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003111
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003112 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003113 if (!CatchParam) {
3114 AllMatched = true;
3115 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003116 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003117
John McCallbd309292010-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 Dunbar86919f42008-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 Friedman55179ca2009-07-11 00:57:02 +00003122 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003123 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003124 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003125
John McCallbd309292010-07-06 01:34:17 +00003126 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003127 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003128 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3129
Anders Carlsson9396a892008-09-11 09:15:33 +00003130 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003131 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003132 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003133
3134 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003135 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003136 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003137
Anders Carlsson9396a892008-09-11 09:15:33 +00003138 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003139
3140 // The scope of the catch variable ends right here.
3141 CatchVarCleanups.ForceCleanup();
3142
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003143 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003144 break;
3145 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003146
Steve Naroff7cae42b2009-07-10 23:34:53 +00003147 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003148 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003149
3150 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003151 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3152 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003153
3154 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003155 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003156
John McCallbd309292010-07-06 01:34:17 +00003157 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003158 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3159 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003160 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003161
John McCallbd309292010-07-06 01:34:17 +00003162 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3163 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003164
3165 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003166 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003168 // Emit the @catch block.
3169 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-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 McCall2dd7d442010-08-04 05:59:32 +00003173 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003174
John McCall1c9c3fd2010-10-15 04:57:14 +00003175 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003176 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003177
John McCallbd309292010-07-06 01:34:17 +00003178 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003179 llvm::Value *Tmp =
3180 CGF.Builder.CreateBitCast(Caught,
3181 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003182 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003183 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003184
Anders Carlsson9396a892008-09-11 09:15:33 +00003185 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003186
3187 // We're done with the catch variable.
3188 CatchVarCleanups.ForceCleanup();
3189
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003190 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003191
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003192 CGF.EmitBlock(NextCatchBlock);
3193 }
3194
John McCallbd309292010-07-06 01:34:17 +00003195 CGF.ObjCEHValueStack.pop_back();
3196
John McCall2dd7d442010-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 McCall9916e3f2010-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 McCall2dd7d442010-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 Carlssonbfee7e92009-02-09 20:38:58 +00003225 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003226 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003227 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003228
John McCall42227ed2010-07-31 23:20:56 +00003229 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003230 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003231
John McCallbd309292010-07-06 01:34:17 +00003232 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003233 CGF.Builder.restoreIP(TryFallthroughIP);
3234 if (CGF.HaveInsertPoint())
3235 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003236 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003237 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003238
John McCallbd309292010-07-06 01:34:17 +00003239 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003240 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003241 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003242 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-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 McCall2dd7d442010-08-04 05:59:32 +00003247
John McCall9916e3f2010-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 McCallbd309292010-07-06 01:34:17 +00003258 ->setDoesNotThrow();
3259 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003260 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003261
John McCall42227ed2010-07-31 23:20:56 +00003262 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003263}
3264
3265void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003266 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003267 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003268
Anders Carlssone005aa12008-09-09 16:16:55 +00003269 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3270 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003271 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003272 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3273 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003274 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003275 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003276 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003277 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003278
John McCallbd309292010-07-06 01:34:17 +00003279 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3280 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003281 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003282
3283 // Clear the insertion point to indicate we are in unreachable code.
3284 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003285}
3286
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003287/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003288/// object: objc_read_weak (id *src)
3289///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003290llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003291 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003292 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003293 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3294 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3295 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003296 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003297 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003298 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003299 return read_weak;
3300}
3301
Fariborz Jahanian83f45b552008-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 Stump11289f42009-09-09 15:08:12 +00003306 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003307 const llvm::Type * SrcTy = src->getType();
3308 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003309 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Dunbar59e476b2009-08-03 17:06:42 +00003312 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003313 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3314 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003315 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3316 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003317 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003318 src, dst, "weakassign");
3319 return;
3320}
3321
Fariborz Jahaniand7db9642008-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 Jahanian217af242010-07-20 20:30:03 +00003326 llvm::Value *src, llvm::Value *dst,
3327 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003328 const llvm::Type * SrcTy = src->getType();
3329 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003330 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Dunbar59e476b2009-08-03 17:06:42 +00003333 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003334 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3335 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003336 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3337 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-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 Jahaniand7db9642008-11-19 00:59:10 +00003344 return;
3345}
3346
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003347/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003348/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003349///
3350void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003351 llvm::Value *src, llvm::Value *dst,
3352 llvm::Value *ivarOffset) {
3353 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003354 const llvm::Type * SrcTy = src->getType();
3355 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003356 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Dunbar59e476b2009-08-03 17:06:42 +00003359 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003360 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3361 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003362 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3363 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003364 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3365 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003366 return;
3367}
3368
Fariborz Jahaniand7db9642008-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 Stump11289f42009-09-09 15:08:12 +00003373 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003374 const llvm::Type * SrcTy = src->getType();
3375 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003376 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Dunbar59e476b2009-08-03 17:06:42 +00003379 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003380 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3381 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003382 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3383 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003384 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003385 src, dst, "weakassign");
3386 return;
3387}
3388
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003389void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003390 llvm::Value *DestPtr,
3391 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003392 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003393 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3394 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003395 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003396 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003397 return;
3398}
3399
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003400/// EmitObjCValueForIvar - Code Gen for ivar reference.
3401///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003402LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3403 QualType ObjectTy,
3404 llvm::Value *BaseValue,
3405 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003406 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003407 const ObjCInterfaceDecl *ID =
3408 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003409 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3410 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003411}
3412
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003413llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003414 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003415 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003416 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003417 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003418 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3419 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003420}
3421
Daniel Dunbar3ad53482008-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 Dunbar5e639272010-04-25 20:39:01 +00003433 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003434 eImageInfo_GarbageCollected = (1 << 1),
3435 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003436 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3437
Daniel Dunbar5e639272010-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 Dunbar75e909f2009-04-20 07:11:47 +00003440 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003441};
3442
Daniel Dunbar5e639272010-04-25 20:39:01 +00003443void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-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 Dunbar59e476b2009-08-03 17:06:42 +00003452
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003453 // We never allow @synthesize of a superclass property.
3454 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003455
Chris Lattner5e016ae2010-06-27 07:15:29 +00003456 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3457
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003458 // Emitted as int[2];
3459 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003460 llvm::ConstantInt::get(Int32Ty, version),
3461 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003462 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003463 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-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 Dunbar59e476b2009-08-03 17:06:42 +00003470 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003471 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003472 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003473 Section,
3474 0,
3475 true);
3476 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003477}
3478
Daniel Dunbar7bd00bd2008-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 Sandsc76fe8b2009-05-09 07:08:47 +00003491 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003492
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003493 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003494 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3495 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003496 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003497 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003498 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003499 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003500 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003501 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003502 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003503}
3504
3505llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003506 unsigned NumClasses = DefinedClasses.size();
3507 unsigned NumCategories = DefinedCategories.size();
3508
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003509 // Return null if no symbols were defined.
3510 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003511 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003512
3513 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003514 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003515 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003516 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3517 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003518
Daniel Dunbar938a77f2008-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 Dunbar22d82ed2008-08-21 04:36:09 +00003521 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003522 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003523 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003524 ObjCTypes.Int8PtrTy);
3525 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003526 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003527 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003528 ObjCTypes.Int8PtrTy);
3529
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003530 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003531 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003532 NumClasses + NumCategories),
3533 Symbols);
3534
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003535 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003536
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003537 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003538 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3539 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003540 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003541 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003542}
3543
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003544llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003545 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003546 LazySymbols.insert(ID->getIdentifier());
3547
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003548 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003549
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003550 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003551 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003552 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003553 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003554 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003555 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3556 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003557 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003558 }
3559
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003560 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003561}
3562
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003563llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3564 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003565 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003566
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003567 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003568 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003569 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003570 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003571 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003572 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3573 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003574 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003575 }
3576
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003577 if (lvalue)
3578 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003579 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003580}
3581
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003582llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003583 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003584
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003585 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003586 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003587 llvm::ConstantArray::get(VMContext,
3588 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003589 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003590 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003591
Owen Anderson170229f2009-07-14 23:10:40 +00003592 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003593}
3594
Argyrios Kyrtzidis13257c52010-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 Jahanian01dff422009-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 Dunbar59e476b2009-08-03 17:06:42 +00003614 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003615 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003616}
3617
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003618void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003619 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003620 bool ForStrongLayout,
3621 bool &HasUnion) {
3622 const RecordDecl *RD = RT->getDecl();
3623 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003624 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003625 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003626 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003627 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003628
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003629 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3630 ForStrongLayout, HasUnion);
3631}
3632
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003633void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003634 const llvm::StructLayout *Layout,
3635 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003636 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637 unsigned int BytePos, bool ForStrongLayout,
3638 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-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 Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003644 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003645 uint64_t MaxFieldOffset = 0;
3646 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003647 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003648
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003649 if (RecFields.empty())
3650 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003651 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3652 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3653
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003654 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003655 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003656 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003657 if (RD) {
Daniel Dunbard51c1a62010-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 Carlssone2c6baf2009-07-24 17:23:54 +00003662 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003663 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003664
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003665 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003666 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003667 LastFieldBitfieldOrUnnamed = Field;
3668 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003669 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003670 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003671
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003672 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003673 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003674 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003675 if (FQT->isUnionType())
3676 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003677
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003678 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003679 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003680 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003681 continue;
3682 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003683
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003684 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003685 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003686 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003687 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003688 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003689 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003690 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3691 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003692 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003693 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003694 FQT = CArray->getElementType();
3695 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003696
3697 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003698 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003699 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003700 int OldIndex = IvarsInfo.size() - 1;
3701 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003702
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003703 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003704 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003705 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706
Fariborz Jahanian3b0f8862009-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 Dunbar59e476b2009-08-03 17:06:42 +00003710 for (int FirstIndex = IvarsInfo.size() - 1,
3711 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003712 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-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 Jahanian3b0f8862009-03-11 00:07:04 +00003719 }
3720 continue;
3721 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003722 }
Fariborz Jahanian3b0f8862009-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 McCall8ccfcb52009-09-24 19:53:00 +00003725 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003726
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003727 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003728 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3729 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003730 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003731 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003732 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003733 MaxUnionIvarSize = UnionIvarSize;
3734 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003735 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003736 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003737 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003738 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003739 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003740 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003741 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003742 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3743 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003744 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003745 // FIXME: Why the asymmetry? We divide by word size in bits on other
3746 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003747 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003748 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003749 MaxSkippedUnionIvarSize = UnionIvarSize;
3750 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003751 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003752 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003753 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003754 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003755 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003756 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003757 }
3758 }
3759 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003760
Argyrios Kyrtzidis2fdb5b52010-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 Jahanianf5fec022009-04-21 18:33:06 +00003780 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003781
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003782 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003783 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003784 MaxUnionIvarSize));
3785 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003786 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003787 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003788}
3789
Fariborz Jahanian1f78a9a2010-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 Jahaniancbaf73c2009-03-11 20:59:05 +00003796 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003797 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003798
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003799 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003800 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003801 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003802 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003803 if (IvarsInfo[0].ivar_bytepos == 0) {
3804 WordsToSkip = 0;
3805 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003806 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003807 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3808 WordsToScan = IvarsInfo[0].ivar_size;
3809 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003810 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003812 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003813 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003814 // consecutive 'scanned' object pointers.
3815 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003816 } else {
Fariborz Jahaniancbaf73c2009-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 Jahanian1bf72882009-03-12 22:50:49 +00003822 SKIP_SCAN SkScan;
3823 SkScan.skip = WordsToSkip;
3824 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003825 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003826
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003827 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003828 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3829 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003830 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003831 WordsToSkip = 0;
3832 WordsToScan = IvarsInfo[i].ivar_size;
3833 }
3834 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003835 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003836 SKIP_SCAN SkScan;
3837 SkScan.skip = WordsToSkip;
3838 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003839 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003840 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003841
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003842 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003843 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003844 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003845 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003846 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003847 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003848 IvarsInfo[LastIndex].ivar_bytepos +
3849 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003850 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003851 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003852 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003853 SKIP_SCAN SkScan;
3854 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3855 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003856 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-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 Jahaniana123b642009-04-24 16:17:09 +00003861 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003862 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-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 Jahanian9659f6b2010-08-04 23:55:24 +00003872
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003873 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003874 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-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 Jahanian9659f6b2010-08-04 23:55:24 +00003880
Fariborz Jahaniancbaf73c2009-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 Jahanian9659f6b2010-08-04 23:55:24 +00003884
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003885 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003886 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003887 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003888 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003889 byte |= 0xf;
3890 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003891 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-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 Dunbard22aa4a2009-05-03 23:21:22 +00003901 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003902 byte = scan_small;
3903 BitMap += byte;
3904 }
3905 }
3906 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003907 unsigned char zero = 0;
3908 BitMap += zero;
Fariborz Jahanian9659f6b2010-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 Dunbar59e476b2009-08-03 17:06:42 +00003917
Fariborz Jahanian9659f6b2010-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 Jahaniana50b3a22010-08-20 21:21:08 +00003943 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003944 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003945 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003946
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003947 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-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 Jahanian1f78a9a2010-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 Jahanian9659f6b2010-08-04 23:55:24 +00003967 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003968 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003969
3970 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003971 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003972 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003973 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-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 Jahanian9659f6b2010-08-04 23:55:24 +00003982 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003983}
3984
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003985llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003986 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3987
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003988 // FIXME: Avoid std::string copying.
3989 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003990 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003991 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003992 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003993 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003994
Owen Anderson170229f2009-07-14 23:10:40 +00003995 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003996}
3997
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003998// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003999llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004000 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4001}
4002
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004003llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-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 Dunbarb036db82008-08-13 03:21:16 +00004008
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004009 if (!Entry)
4010 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004011 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004012 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004013 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004014
Owen Anderson170229f2009-07-14 23:10:40 +00004015 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004016}
4017
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004018llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004019 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00004020 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4021 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004022
4023 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4024
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004025 if (!Entry)
4026 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004027 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004028 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004029 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004030
Owen Anderson170229f2009-07-14 23:10:40 +00004031 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004032}
4033
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004034// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004035llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004036 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004037
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004038 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004039 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004040 llvm::ConstantArray::get(VMContext,
4041 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004042 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004043 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004044
Owen Anderson170229f2009-07-14 23:10:40 +00004045 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004046}
4047
4048// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004049// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004050llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004051CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4052 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004053 std::string TypeStr;
4054 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004055 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4056}
4057
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004058void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004059 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004060 llvm::SmallVectorImpl<char> &Name) {
4061 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004062 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004063 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4064 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004065 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004066 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004067 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004068 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004069}
4070
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004071void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004072 EmitModuleInfo();
4073
Daniel Dunbarc475d422008-10-29 22:36:39 +00004074 // Emit the dummy bodies for any protocols which were referenced but
4075 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004076 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004077 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4078 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004079 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004080
Daniel Dunbarc475d422008-10-29 22:36:39 +00004081 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004082 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004083 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004084 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004085 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004086 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004087 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004088 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004089 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004090 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004091 }
4092
Daniel Dunbarc61d0e92008-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 Dunbard027a922009-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 Dunbarc61d0e92008-08-25 06:02:07 +00004103
Daniel Dunbard027a922009-09-07 00:20:42 +00004104 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004105 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4106 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-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 Lattnera299f2c2010-04-17 18:26:20 +00004109 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004110 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004111 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-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 Lattnera299f2c2010-04-17 18:26:20 +00004118
Daniel Dunbard027a922009-09-07 00:20:42 +00004119 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004120 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004121}
4122
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004123CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004124 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004125 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004126 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004127 ObjCABI = 2;
4128}
4129
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004130/* *** */
4131
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004132ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004133 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004134 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4135 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004136
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004137 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004138 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004139 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004140 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004141 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004142
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004143 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004144 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004145 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004146
Mike Stump18bb9282009-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 Dunbarb036db82008-08-13 03:21:16 +00004149 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004150 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004151
Fariborz Jahanian279eda62009-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 Dunbar59e476b2009-08-03 17:06:42 +00004156
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004157 // FIXME: This is leaked.
4158 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004159
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004160 // struct _objc_super {
4161 // id self;
4162 // Class cls;
4163 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004164 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004165 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004166 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004167 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004168 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004169 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004170 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004171 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004172 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004173
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004174 SuperCTy = Ctx.getTagDeclType(RD);
4175 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004176
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004177 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004178 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4179
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004180 // struct _prop_t {
4181 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004182 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004183 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004184 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004185 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004186 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004187
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004188 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004189 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004190 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004191 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004192 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004193 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004194 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004195 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004196 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004197 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004198 PropertyListTy);
4199 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004200 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004201
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004202 // struct _objc_method {
4203 // SEL _cmd;
4204 // char *method_type;
4205 // char *_imp;
4206 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004207 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004208 Int8PtrTy,
4209 Int8PtrTy,
4210 NULL);
4211 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004212
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004213 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004214 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004215 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004216 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004217}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004218
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004219ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004220 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004221 // struct _objc_method_description {
4222 // SEL name;
4223 // char *types;
4224 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004225 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004226 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004227 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004228 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004229 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004230 MethodDescriptionTy);
4231
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004232 // struct _objc_method_description_list {
4233 // int count;
4234 // struct _objc_method_description[1];
4235 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004236 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004237 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004238 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004239 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004240 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004241 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004242
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004243 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004244 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004245 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004246
Daniel Dunbarb036db82008-08-13 03:21:16 +00004247 // Protocol description structures
4248
Fariborz Jahanian4b4c8262009-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 Dunbar59e476b2009-08-03 17:06:42 +00004255 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004256 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004257 MethodDescriptionListPtrTy,
4258 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004259 PropertyListPtrTy,
4260 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004261 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004262 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004263
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004264 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004265 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004266
Daniel Dunbarc475d422008-10-29 22:36:39 +00004267 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004268
Owen Andersonc36edfe2009-08-13 23:27:53 +00004269 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4270 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004271
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004272 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004273 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004274 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004275 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004276 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004277 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004278 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4279
Fariborz Jahanian4b4c8262009-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 Anderson758428f2009-08-05 23:18:46 +00004287 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004288 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004289 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-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 Dunbar59e476b2009-08-03 17:06:42 +00004296 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004297 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004298 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004299 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004300
4301 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004302 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004303 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004304
4305 // Class description structures
4306
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004307 // struct _objc_ivar {
4308 // char *ivar_name;
4309 // char *ivar_type;
4310 // int ivar_offset;
4311 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004312 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004313 Int8PtrTy,
4314 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004315 NULL);
4316 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4317
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004318 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004319 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004320 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004321 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004322
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004323 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004324 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004325 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004326 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004327
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004328 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004329 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004330 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004331 Int8PtrTy,
4332 PropertyListPtrTy,
4333 NULL);
4334 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004335 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336
Owen Andersonc36edfe2009-08-13 23:27:53 +00004337 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004338
Fariborz Jahanian4b4c8262009-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 Anderson758428f2009-08-05 23:18:46 +00004353 T = llvm::StructType::get(VMContext,
4354 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004355 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-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 Dunbar59e476b2009-08-03 17:06:42 +00004368
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004369 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4370 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004371 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004372
Fariborz Jahanian4b4c8262009-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 Anderson758428f2009-08-05 23:18:46 +00004381 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-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 Dunbar22d82ed2008-08-21 04:36:09 +00004391 // Global metadata structures
4392
Fariborz Jahanian4b4c8262009-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 Anderson758428f2009-08-05 23:18:46 +00004400 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004401 SelectorPtrTy,
4402 ShortTy,
4403 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004404 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004405 NULL);
4406 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004407 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004408
Fariborz Jahanianeee54df2009-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 Dunbar59e476b2009-08-03 17:06:42 +00004415 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004416 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004417 LongTy,
4418 Int8PtrTy,
4419 SymtabPtrTy,
4420 NULL);
4421 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004422
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004423
Mike Stump18bb9282009-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 Carlsson9ff22482008-09-09 10:10:21 +00004426 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004427
Anders Carlsson9ff22482008-09-09 10:10:21 +00004428 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004429 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004430 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004431
4432 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004433 llvm::StructType::get(VMContext,
4434 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4435 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004436 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004437 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004438 ExceptionDataTy);
4439
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004440}
4441
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004443 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-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 Anderson758428f2009-08-05 23:18:46 +00004449 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004450 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004451 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004452 NULL);
4453 CGM.getModule().addTypeName("struct.__method_list_t",
4454 MethodListnfABITy);
4455 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004456 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004457
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004458 // struct _protocol_t {
4459 // id isa; // NULL
4460 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004461 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-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 Jahanian0232c052009-01-23 01:46:23 +00004466 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004467 // const uint32_t size; // sizeof(struct _protocol_t)
4468 // const uint32_t flags; // = 0
4469 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004470
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004471 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004472 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004473
Owen Anderson758428f2009-08-05 23:18:46 +00004474 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004475 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004476 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-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 Dunbar8de90f02009-02-15 07:36:20 +00004488
4489 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004490 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004491
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004492 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004493 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004494 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004495 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004496 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004497 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004498 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004499 NULL);
4500 CGM.getModule().addTypeName("struct._objc_protocol_list",
4501 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004502 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004503 ProtocolListnfABITy);
4504
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004505 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004506 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004507
Fariborz Jahanianb15a3d52009-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 Stump11289f42009-09-09 15:08:12 +00004515 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004516 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004517 Int8PtrTy,
4518 Int8PtrTy,
4519 IntTy,
4520 IntTy,
4521 NULL);
4522 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523
Fariborz Jahanianb15a3d52009-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 Anderson758428f2009-08-05 23:18:46 +00004529 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004530 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004531 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004532 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004533 NULL);
4534 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004535
Owen Anderson9793f0e2009-07-29 22:16:19 +00004536 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004537
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004538 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-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 Dunbar59e476b2009-08-03 17:06:42 +00004551
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004552 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004553 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-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 Dunbar59e476b2009-08-03 17:06:42 +00004566
Fariborz Jahanian0232c052009-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 Anderson9793f0e2009-07-29 22:16:19 +00004571 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4573
Fariborz Jahanian0232c052009-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 Dunbar59e476b2009-08-03 17:06:42 +00004581
Owen Andersonc36edfe2009-08-13 23:27:53 +00004582 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004583 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004584 llvm::StructType::get(VMContext,
4585 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-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 Jahanian0232c052009-01-23 01:46:23 +00004591 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4592
4593 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004594 ClassnfABITy);
4595
Fariborz Jahanian71394042009-01-23 23:53:38 +00004596 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004597 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004598
Fariborz Jahanian0232c052009-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 Jahanian5a63e4c2009-01-23 17:41:22 +00004606 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004607 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004608 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004609 MethodListnfABIPtrTy,
4610 MethodListnfABIPtrTy,
4611 ProtocolListnfABIPtrTy,
4612 PropertyListPtrTy,
4613 NULL);
4614 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004616 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004617 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4618 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004619
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004620 // MessageRefTy - LLVM for:
4621 // struct _message_ref_t {
4622 // IMP messenger;
4623 // SEL name;
4624 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004625
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004626 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004627 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004628 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004629 SourceLocation(),
4630 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004631 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004632 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004633 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004634 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004635 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004636
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004637 MessageRefCTy = Ctx.getTagDeclType(RD);
4638 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4639 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004640
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004641 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004642 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004643
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004644 // SuperMessageRefTy - LLVM for:
4645 // struct _super_message_ref_t {
4646 // SUPER_IMP messenger;
4647 // SEL name;
4648 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004649 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004650 SelectorPtrTy,
4651 NULL);
4652 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004654 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004655 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4656
Daniel Dunbarb1559a42009-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 Anderson758428f2009-08-05 23:18:46 +00004663 EHTypeTy = llvm::StructType::get(VMContext,
4664 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004665 Int8PtrTy,
4666 ClassnfABIPtrTy,
4667 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004668 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004669 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004670}
4671
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004672llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004673 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004674
Fariborz Jahanian71394042009-01-23 23:53:38 +00004675 return NULL;
4676}
4677
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004678void CGObjCNonFragileABIMac::AddModuleClassList(const
4679 std::vector<llvm::GlobalValue*>
4680 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004681 const char *SymbolName,
4682 const char *SectionName) {
4683 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684
Daniel Dunbar19573e72009-05-15 21:48:48 +00004685 if (!NumClasses)
4686 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004687
Daniel Dunbar19573e72009-05-15 21:48:48 +00004688 std::vector<llvm::Constant*> Symbols(NumClasses);
4689 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004690 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004691 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004692 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004693 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004694 NumClasses),
4695 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004696
Daniel Dunbar19573e72009-05-15 21:48:48 +00004697 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004698 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004699 llvm::GlobalValue::InternalLinkage,
4700 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004701 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004702 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004703 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004704 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004705}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004706
Fariborz Jahanian71394042009-01-23 23:53:38 +00004707void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4708 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004709
Daniel Dunbar19573e72009-05-15 21:48:48 +00004710 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004711 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004712 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004713 "\01L_OBJC_LABEL_CLASS_$",
4714 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004715
Fariborz Jahanian67260552009-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 Jahanian67260552009-11-17 21:37:35 +00004721 }
4722
Fariborz Jahaniana6227fd2009-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 Jahanian67260552009-11-17 21:37:35 +00004729
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004730 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004731 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4732 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004733
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004734 // Build list of all implemented category addresses in array
4735 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004736 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004737 "\01L_OBJC_LABEL_CATEGORY_$",
4738 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004739 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004740 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4741 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004742
Daniel Dunbar5e639272010-04-25 20:39:01 +00004743 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004744}
4745
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004746/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004747/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004748/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004749/// message dispatch call for all the rest.
4750///
4751bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004752 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4753 default:
4754 assert(0 && "Invalid dispatch method!");
4755 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004756 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004757 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004758 return false;
Daniel Dunbarfca18c1b42010-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 Jahaniane4128642009-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 Dunbar59e476b2009-08-03 17:06:42 +00004776
Fariborz Jahaniane4128642009-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 Dunbar59e476b2009-08-03 17:06:42 +00004785 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004786 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004787 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4788 &CGM.getContext().Idents.get("objects"),
4789 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004790 };
4791 NonLegacyDispatchMethods.insert(
4792 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004793 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004794
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004795 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004796}
4797
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004798// Metadata flags
4799enum MetaDataDlags {
4800 CLS = 0x0,
4801 CLS_META = 0x1,
4802 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004803 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-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 Jahanian56b3b772009-01-29 19:24:30 +00004815/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-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 Dunbar59e476b2009-08-03 17:06:42 +00004822 unsigned flags,
4823 unsigned InstanceStart,
4824 unsigned InstanceSize,
4825 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004826 std::string ClassName = ID->getNameAsString();
4827 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-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 Jahanian9e3ad522009-01-24 20:21:50 +00004831 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4833 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004834 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-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 Dunbar59e476b2009-08-03 17:06:42 +00004840 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004841 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-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 Dunbar59e476b2009-08-03 17:06:42 +00004847 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004848 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004849 // Instance methods should always be defined.
4850 Methods.push_back(GetMethodConstant(*i));
4851 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004852 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004853 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004854 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004855
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004856 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4857 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004858
Fariborz Jahaniand27a8202009-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 Jahanian99113fd2009-01-26 21:38:32 +00004867 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004868 Values[ 5] = EmitMethodList(MethodListName,
4869 "__DATA, __objc_const", Methods);
4870
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004871 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4872 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004873 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004874 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004875 OID->all_referenced_protocol_begin(),
4876 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004877
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004878 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004879 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004880 else
4881 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004882 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4883 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004884 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004885 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004886 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004887 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4888 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004889 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004890 Values);
4891 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-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 Jahanianc22f2362009-01-31 02:43:27 +00004898 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004899 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004900 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004901 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004902
Fariborz Jahanian9e3ad522009-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 Jahanian4723fb72009-01-24 21:21:53 +00004915llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-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 Jahanian9e3ad522009-01-24 20:21:50 +00004921 std::vector<llvm::Constant*> Values(5);
4922 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004923 Values[1] = SuperClassGV;
4924 if (!Values[1])
4925 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004926 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4927 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4928 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004930 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004931 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4932 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004933 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004934 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004935 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004936 if (HiddenVisibility)
4937 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004938 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004939}
4940
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004941bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004942CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004943 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004944}
4945
Daniel Dunbar961202372009-05-03 12:57:56 +00004946void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004947 uint32_t &InstanceStart,
4948 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004949 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004950 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004951
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004952 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004953 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-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 Dunbar554fd792009-04-19 23:41:48 +00004960}
4961
Fariborz Jahanian71394042009-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 Dunbar59e476b2009-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 Jahanian71394042009-01-23 23:53:38 +00004973 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004974 CGM.getModule(),
4975 ObjCTypes.ImpnfABITy,
4976 false,
4977 llvm::GlobalValue::ExternalLinkage,
4978 0,
4979 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004980 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004981 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004982 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004983 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004984 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004985 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004986 uint32_t InstanceSize = InstanceStart;
4987 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004988 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4989 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004990
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004991 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004992
4993 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004994 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004995 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004996 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004997 if (ID->getNumIvarInitializers())
4998 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004999 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005000 // class is root
5001 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005002 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005003 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005004 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005005 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005006 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5007 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5008 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005009 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005010 if (Root->hasAttr<WeakImportAttr>())
5011 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005012 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005013 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005014 ObjCMetaClassName +
5015 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005016 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005017 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5018 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005019 }
5020 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5021 InstanceStart,
5022 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005023 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005024 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005025 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5026 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005027 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005028
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005029 // Metadata for the class
5030 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005031 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005032 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005033 if (ID->getNumIvarInitializers())
5034 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005035
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005036 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005037 flags |= CLS_EXCEPTION;
5038
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005039 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005040 flags |= CLS_ROOT;
5041 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005042 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005043 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005044 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005045 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005046 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005047 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5048 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005049 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005050 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005051 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005052 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005053 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005054 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005056 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005057 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005058 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5059 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005060 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005061
Daniel Dunbar9a017d72009-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 Dunbar8f28d012009-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 Jahanian71394042009-01-23 23:53:38 +00005069}
5070
Fariborz Jahanian097feda2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005080 const ObjCProtocolDecl *PD) {
5081
Fariborz Jahanian464423d2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005085 llvm::Constant *Init =
5086 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5087 ObjCTypes.ExternalProtocolPtrTy);
5088
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005089 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005090 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005092 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5093 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005094 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005095 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005096 CGM.getModule(),
5097 Init->getType(), false,
5098 llvm::GlobalValue::WeakAnyLinkage,
5099 Init,
5100 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005101 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5102 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005103 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005104 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005105}
5106
Fariborz Jahanian0c8d0602009-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 Dunbar9a017d72009-05-15 22:33:15 +00005117void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005118 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005119 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005120 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5121 "_$_" + OCD->getNameAsString());
5122 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005123 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005124
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005125 std::vector<llvm::Constant*> Values(6);
5126 Values[0] = GetClassName(OCD->getIdentifier());
5127 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005128 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005129 if (Interface->hasAttr<WeakImportAttr>())
5130 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5131
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005132 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005133 std::vector<llvm::Constant*> Methods;
5134 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005135 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005136 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005137
5138 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005139 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005140 // Instance methods should always be defined.
5141 Methods.push_back(GetMethodConstant(*i));
5142 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005143
5144 Values[2] = EmitMethodList(MethodListName,
5145 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005146 Methods);
5147
5148 MethodListName = Prefix;
5149 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5150 OCD->getNameAsString();
5151 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005152 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005153 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005154 // Class methods should always be defined.
5155 Methods.push_back(GetMethodConstant(*i));
5156 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005157
5158 Values[3] = EmitMethodList(MethodListName,
5159 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005160 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005161 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005162 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005163 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005164 llvm::SmallString<256> ExtName;
5165 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5166 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005167 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005168 + Interface->getName() + "_$_"
5169 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005170 Category->protocol_begin(),
5171 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005172 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5173 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005174 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005175 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5176 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005177 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005178
5179 llvm::Constant *Init =
5180 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005181 Values);
5182 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005183 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005184 false,
5185 llvm::GlobalValue::InternalLinkage,
5186 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005187 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005188 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005189 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005190 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005191 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005192 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-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 Jahanian0c8d0602009-01-26 18:32:24 +00005197}
Fariborz Jahanian99113fd2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005203 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005204 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005205 if (!Fn)
5206 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005207
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005208 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005209 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005210 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005212 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005213 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005214 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-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 Dunbar349e6fb2009-10-18 20:48:59 +00005224llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5225 const char *Section,
5226 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005227 // Return null for empty list.
5228 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005229 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005230
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005231 std::vector<llvm::Constant*> Values(3);
5232 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005233 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005234 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005235 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005236 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005237 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005238 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005239 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005240 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005241
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005242 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005243 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005244 llvm::GlobalValue::InternalLinkage,
5245 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005246 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005247 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005248 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005249 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005250 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005251 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005252 ObjCTypes.MethodListnfABIPtrTy);
5253}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005254
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005255/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5256/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005257llvm::GlobalVariable *
5258CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5259 const ObjCIvarDecl *Ivar) {
5260 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005261 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005262 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005263 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005264 CGM.getModule().getGlobalVariable(Name);
5265 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005266 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005267 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005268 false,
5269 llvm::GlobalValue::ExternalLinkage,
5270 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005271 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005272 return IvarOffsetGV;
5273}
5274
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005275llvm::Constant *
5276CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5277 const ObjCIvarDecl *Ivar,
5278 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005279 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005280 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005281 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005282 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005283 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005284
Mike Stump18bb9282009-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 Dunbarbf90b332009-04-19 00:44:02 +00005287 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5288 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005289 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005290 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005291 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005292 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005293 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005294 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005295}
5296
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005297/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005298/// implementation. The return value has type
Fariborz Jahanian7415caa2009-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 Dunbarf5c18462009-04-20 06:54:31 +00005313
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005314llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005315 const ObjCImplementationDecl *ID) {
5316
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005317 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005318
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005319 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5320 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005321
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005322 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323
Daniel Dunbarae032262009-04-20 00:33:43 +00005324 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005325 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005326 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005327
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005328 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5329 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005330 // Ignore unnamed bit-fields.
5331 if (!IVD->getDeclName())
5332 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005333 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005334 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005335 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5336 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005337 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005338 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005339 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005340 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005341 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005342 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005343 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-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 Andersonb7a2fe62009-07-24 23:12:58 +00005349 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005350 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005351 }
5352 // Return null for empty list.
5353 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005354 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005355 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005356 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-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 Anderson9793f0e2009-07-29 22:16:19 +00005359 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005360 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005361 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005362 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005363 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5364 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005365 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005366 llvm::GlobalValue::InternalLinkage,
5367 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005368 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005369 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005370 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005371 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005372
Chris Lattnerf56501c2009-07-17 23:57:13 +00005373 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005374 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005375}
5376
5377llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005379 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005380
Fariborz Jahanian56b3b772009-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 Dunbar59e476b2009-08-03 17:06:42 +00005385 Entry =
5386 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5387 llvm::GlobalValue::ExternalLinkage,
5388 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005389 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005390 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005392
Fariborz Jahanian56b3b772009-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 Dunbar59e476b2009-08-03 17:06:42 +00005414 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005415 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005416
Fariborz Jahanian56b3b772009-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 Jahanian56b3b772009-01-29 19:24:30 +00005421 // Construct method lists.
5422 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5423 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005424 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005425 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005426 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005427 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005428 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5429 OptInstanceMethods.push_back(C);
5430 } else {
5431 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005432 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434
5435 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005436 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005437 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005438 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005439 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5440 OptClassMethods.push_back(C);
5441 } else {
5442 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005443 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005444 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005445
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005446 std::vector<llvm::Constant*> Values(10);
5447 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005448 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005449 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005450 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5451 PD->protocol_begin(),
5452 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005453
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005454 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005455 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005456 "__DATA, __objc_const",
5457 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005458 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005459 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005460 "__DATA, __objc_const",
5461 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005462 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005463 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005464 "__DATA, __objc_const",
5465 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005466 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005467 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005468 "__DATA, __objc_const",
5469 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005470 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005471 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005472 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005473 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005474 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005475 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005476 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005477 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005479 if (Entry) {
5480 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005481 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005482 Entry->setInitializer(Init);
5483 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005484 Entry =
Daniel Dunbar349e6fb2009-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 Jahanianc22f2362009-01-31 02:43:27 +00005488 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005489 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005490 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005491 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005492 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005493 CGM.AddUsedGlobal(Entry);
5494
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005495 // Use this protocol meta-data to build protocol list table in section
5496 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-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 Jahanianc22f2362009-01-31 02:43:27 +00005501 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005502 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005503 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005504 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005505 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-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 Dunbar349e6fb2009-10-18 20:48:59 +00005518CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5519 ObjCProtocolDecl::protocol_iterator begin,
5520 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005521 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005522
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005523 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005524 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005525 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005526
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005527 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-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 Jahanian56b3b772009-01-29 19:24:30 +00005532 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005533 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005534
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005535 for (; begin != end; ++begin)
5536 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5537
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005538 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005539 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005540 ObjCTypes.ProtocolnfABIPtrTy));
5541
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005542 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005543 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005544 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005545 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005546 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005547 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005548 ProtocolRefs.size()),
5549 ProtocolRefs);
5550
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005551 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005552 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005553 llvm::GlobalValue::InternalLinkage,
5554 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005555 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005556 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005557 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005558 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005559 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005560 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005561 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005562}
5563
Fariborz Jahaniand9c28b82009-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 Anderson170229f2009-07-14 23:10:40 +00005574 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005575 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5576 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005577 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005578 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005579 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005580 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005581}
Fariborz Jahanianc88a70d2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005588///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005589LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-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 Dunbar9fd114d2009-04-22 07:32:20 +00005596 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5597 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005598}
5599
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005600llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005601 CodeGen::CodeGenFunction &CGF,
5602 const ObjCInterfaceDecl *Interface,
5603 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005604 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005605}
5606
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005607CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005608 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005609 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-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 Stump18bb9282009-05-16 07:57:57 +00005616 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5617 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005618 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005619 llvm::Value *Arg0 = Receiver;
5620 if (!IsSuper)
5621 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005622
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005623 // Find the message function name.
Mike Stump18bb9282009-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 McCall2da83a32010-02-26 00:48:12 +00005626 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005627 = Types.getFunctionInfo(ResultType, CallArgList(),
5628 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005629 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005630 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005631 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner396639d2010-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 Dunbar6f2e8392010-07-14 23:39:36 +00005639 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5640 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5641 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005642 } else {
Chris Lattner396639d2010-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 Jahaniane4dc35d2009-02-04 20:42:28 +00005650 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005651 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005652 Name += '_';
5653 std::string SelName(Sel.getAsString());
5654 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005655 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-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 Dunbare60aa052009-04-15 19:03:14 +00005661 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005662 std::vector<llvm::Constant*> Values(2);
5663 Values[0] = Fn;
5664 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005665 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005666 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005667 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005668 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005669 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005670 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005671 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005672 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005673 }
5674 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005675
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005676 CallArgList ActualArgs;
5677 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005678 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005679 ObjCTypes.MessageRefCPtrTy));
5680 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005681 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005682 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005683 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5684 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005685 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005686 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005687 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005688 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005689}
5690
5691/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005692CodeGen::RValue
5693CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005694 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005695 QualType ResultType,
5696 Selector Sel,
5697 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005698 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005699 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005700 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005701 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005702 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5703 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005704 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005705 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005706 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005707 Receiver, CGF.getContext().getObjCIdType(),
5708 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005709}
5710
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005711llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005712CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005713 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5714
Daniel Dunbara6468342009-03-02 05:18:14 +00005715 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005716 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005717 false, llvm::GlobalValue::ExternalLinkage,
5718 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005719 }
5720
5721 return GV;
5722}
5723
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005724llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5725 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005726 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005728 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005729 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005730 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005731 Entry =
5732 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005733 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005734 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005735 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005736 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005737 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005738 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005739 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005740 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005741 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005742
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005743 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005744}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005745
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005746llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005747CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005748 const ObjCInterfaceDecl *ID) {
5749 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005750
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005751 if (!Entry) {
5752 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5753 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005754 Entry =
5755 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005756 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005757 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005758 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005759 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005760 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005762 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005763 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005764 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005765
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005766 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005767}
5768
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005769/// EmitMetaClassRef - Return a Value * of the address of _class_t
5770/// meta-data
5771///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005772llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5773 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005774 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5775 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005776 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005777
Daniel Dunbar15894b72009-04-07 05:48:37 +00005778 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005779 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005780 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005781 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005782 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005783 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005784 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005785 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005786 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005787 ObjCTypes.ClassnfABIPtrTy));
5788
Daniel Dunbare60aa052009-04-15 19:03:14 +00005789 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005790 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005791
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005792 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005793}
5794
Fariborz Jahanian33f66e62009-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 Jahanian95ace552009-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 Jahanian33f66e62009-02-05 20:41:40 +00005805 return EmitClassRef(Builder, ID);
5806}
5807
Fariborz Jahanian6b7cd6e2009-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 McCall78a15112010-05-22 01:48:05 +00005813 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-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 Dunbaraff9fca2009-09-17 04:01:22 +00005820 const CodeGen::CallArgList &CallArgs,
5821 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005827
Fariborz Jahanian6b7cd6e2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005832
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005833 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-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 Dunbar508a7dd2009-04-18 08:51:00 +00005839 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005840 Target = CGF.Builder.CreateStructGEP(Target, 0);
5841 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005842 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005843 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005844 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005845 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005846
Mike Stump18bb9282009-05-16 07:57:57 +00005847 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5848 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-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 Dunbar59e476b2009-08-03 17:06:42 +00005854
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005855 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005856 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5857 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005858 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005859 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005860 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005861 ObjCSuper, ObjCTypes.SuperPtrCTy,
5862 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005863}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005864
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005865llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005866 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005867 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005868
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005869 if (!Entry) {
Daniel Dunbar59e476b2009-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 Jahanian5d5ed2d2009-05-11 19:25:47 +00005877 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005878 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005879 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005880
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005881 if (lval)
5882 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005883 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005884}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005885/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005886/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005887///
5888void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005889 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005890 llvm::Value *dst,
5891 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005892 const llvm::Type * SrcTy = src->getType();
5893 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005894 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Jahanian1b074a32009-03-13 00:42:52 +00005898 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5899 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005900 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5901 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005902 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5903 src, dst, ivarOffset);
Fariborz Jahanian06292952009-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 Dunbar59e476b2009-08-03 17:06:42 +00005911 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005912 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005913 const llvm::Type * SrcTy = src->getType();
5914 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005915 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Dunbar59e476b2009-08-03 17:06:42 +00005918 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005919 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5920 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005921 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5922 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005923 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005924 src, dst, "weakassign");
5925 return;
5926}
5927
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005928void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005929 CodeGen::CodeGenFunction &CGF,
5930 llvm::Value *DestPtr,
5931 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005932 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005933 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5934 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005935 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005936 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005937 return;
5938}
5939
Fariborz Jahanian06292952009-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 Dunbar59e476b2009-08-03 17:06:42 +00005944 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005945 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005946 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005947 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5948 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005949 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005950 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005951 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-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 Stump11289f42009-09-09 15:08:12 +00005959 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005960 const llvm::Type * SrcTy = src->getType();
5961 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005962 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Jahanian1b074a32009-03-13 00:42:52 +00005966 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5967 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005968 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5969 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005970 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-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 Jahanian217af242010-07-20 20:30:03 +00005979 llvm::Value *src, llvm::Value *dst,
5980 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005981 const llvm::Type * SrcTy = src->getType();
5982 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005983 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-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 Jahanian1b074a32009-03-13 00:42:52 +00005987 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5988 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005989 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5990 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-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 Jahanian06292952009-02-16 22:52:32 +00005997 return;
5998}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005999
John McCallc20acd32010-07-21 00:41:47 +00006000namespace {
John McCallcda666c2010-07-21 07:22:38 +00006001 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-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 Dunbar59e476b2009-08-03 17:06:42 +00006013void
John McCallbd309292010-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 Dunbar0b0dcd92009-02-24 07:47:38 +00006018
John McCallbd309292010-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 McCallcda666c2010-07-21 07:22:38 +00006025 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6026 ObjCTypes.getSyncExitFn(),
6027 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006028
John McCallbd309292010-07-06 01:34:17 +00006029 // Emit the body of the statement.
6030 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006031
John McCallbd309292010-07-06 01:34:17 +00006032 // Pop the lock-release cleanup.
6033 CGF.PopCleanupBlock();
6034}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006035
John McCallbd309292010-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 McCall5c08ab92010-07-13 22:12:14 +00006043
John McCallcda666c2010-07-21 07:22:38 +00006044 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-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 McCallbd309292010-07-06 01:34:17 +00006059}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006060
John McCall2ca705e2010-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 McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006092
John McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006099
John McCallbd309292010-07-06 01:34:17 +00006100 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006101
John McCallbd309292010-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 Gregor46a572b2010-04-26 16:46:50 +00006106 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006107
John McCallbd309292010-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 Gregor96c79492010-04-23 22:50:49 +00006115 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006116 Handler.TypeInfo = 0; // catch-all
6117 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006118 break;
6119 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006120
John McCall2ca705e2010-07-24 00:37:23 +00006121 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006122 }
John McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006127 }
John McCallbd309292010-07-06 01:34:17 +00006128
6129 // Emit the try body.
6130 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006131
John McCallbd309292010-07-06 01:34:17 +00006132 // Leave the try.
6133 if (S.getNumCatchStmts())
6134 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006135
John McCallbd309292010-07-06 01:34:17 +00006136 // Remember where we were.
6137 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006138
John McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006142
John McCallbd309292010-07-06 01:34:17 +00006143 CGF.EmitBlock(Handler.Block);
6144 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006145
John McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006151
John McCallbd309292010-07-06 01:34:17 +00006152 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006153 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006154 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6155 EndCatchMightThrow,
6156 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006157
John McCallbd309292010-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 Dunbar76b7acc2009-03-02 06:08:11 +00006162
John McCall1c9c3fd2010-10-15 04:57:14 +00006163 CGF.EmitAutoVarDecl(*CatchParam);
John McCallbd309292010-07-06 01:34:17 +00006164 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006165 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006166
John McCallbd309292010-07-06 01:34:17 +00006167 CGF.ObjCEHValueStack.push_back(Exn);
6168 CGF.EmitStmt(Handler.Body);
6169 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006170
John McCallbd309292010-07-06 01:34:17 +00006171 // Leave the earlier cleanup.
6172 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006173
John McCallbd309292010-07-06 01:34:17 +00006174 CGF.EmitBranchThroughCleanup(Cont);
6175 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006176
John McCallbd309292010-07-06 01:34:17 +00006177 // Go back to the try-statement fallthrough.
6178 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006179
John McCallbd309292010-07-06 01:34:17 +00006180 // Pop out of the normal cleanup on the finally.
6181 if (S.getFinallyStmt())
6182 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006183
John McCallad5d61e2010-07-23 21:56:41 +00006184 if (Cont.isValid())
6185 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006186}
6187
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006188/// EmitThrowStmt - Generate code for a throw statement.
6189void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6190 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006191 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006192 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006193 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6194 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006195 llvm::Value *Args[] = { Exception };
6196 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6197 Args, Args+1)
6198 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006199 } else {
John McCall17afe452010-10-16 08:21:07 +00006200 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6201 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006202 }
6203
John McCall17afe452010-10-16 08:21:07 +00006204 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006205 CGF.Builder.ClearInsertionPoint();
6206}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006207
John McCall2ca705e2010-07-24 00:37:23 +00006208llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006209CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006210 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006211 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006212
Daniel Dunbar8f28d012009-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 Dunbard7beeea2009-04-07 06:43:45 +00006218
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006219 // If this type (or a super class) has the __objc_exception__
6220 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006221 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006222 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006223 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006224 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006225 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006226 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006227 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006228 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006229
Daniel Dunbar8f28d012009-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 Dunbar15894b72009-04-07 05:48:37 +00006233 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006234 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006235 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006236 CGM.getModule().getGlobalVariable(VTableName);
6237 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006238 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6239 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006240 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006241 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006242
Chris Lattner5e016ae2010-06-27 07:15:29 +00006243 llvm::Value *VTableIdx =
6244 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006245
6246 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006247 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006248 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006249 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006250 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006251 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006252
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006253 if (Entry) {
6254 Entry->setInitializer(Init);
6255 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006256 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006257 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006258 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006259 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006260 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006261 }
6262
John McCall457a04e2010-10-22 21:05:15 +00006263 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006264 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006265 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6266 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-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 Dunbarb1559a42009-03-01 04:46:24 +00006274
6275 return Entry;
6276}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006277
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006278/* *** */
6279
Daniel Dunbarb036db82008-08-13 03:21:16 +00006280CodeGen::CGObjCRuntime *
6281CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006282 return new CGObjCMac(CGM);
6283}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006284
6285CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006286CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006287 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006288}