blob: 962712549153783fd8030734adf78ed74bc8a1b0 [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 McCalled1ae862011-01-28 11:13:47 +000019#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000027
John McCall42227ed2010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000043
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000044// Common CGObjCRuntime functions, these don't belong here, but they
45// don't belong in CGObjCRuntime either so we will live with it for
46// now.
47
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000053
Daniel Dunbar7e5aba42010-04-02 22:29:40 +000054 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
55 // in here; it should never be necessary because that should be the lexical
56 // decl context for the ivar.
Daniel Dunbar031d4d42010-04-02 15:43:29 +000057
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000058 // If we know have an implementation (and the ivar is in it) then
59 // look up in the implementation layout.
Daniel Dunbar59e476b2009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000061 if (ID && ID->getClassInterface() == Container)
62 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
63 else
64 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000065
66 // Compute field index.
67 //
68 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
69 // implemented. This should be fixed to get the information from the layout
70 // directly.
71 unsigned Index = 0;
72 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
73 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
74 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
75 if (Ivar == Ivars[k])
76 break;
77 ++Index;
78 }
79 assert(Index != Ivars.size() && "Ivar is not inside container!");
David Chisnalle8431a72010-11-03 16:12:44 +000080 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000081
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000082 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000088 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
89}
90
91uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
92 const ObjCImplementationDecl *OID,
93 const ObjCIvarDecl *Ivar) {
94 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000095}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000104 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000105 QualType IvarTy = Ivar->getType();
106 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000107 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000108 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000109 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000110
Daniel Dunbarf166a522010-08-21 03:44:13 +0000111 if (!Ivar->isBitField()) {
112 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
113 LV.getQuals().addCVRQualifiers(CVRQualifiers);
114 return LV;
115 }
Daniel Dunbar961202372009-05-03 12:57:56 +0000116
Daniel Dunbara70fab82010-09-02 23:53:31 +0000117 // We need to compute an access strategy for this bit-field. We are given the
118 // offset to the first byte in the bit-field, the sub-byte offset is taken
119 // from the original layout. We reuse the normal bit-field access strategy by
120 // treating this as an access to a struct where the bit-field is in byte 0,
121 // and adjust the containing type size as appropriate.
122 //
123 // FIXME: Note that currently we make a very conservative estimate of the
124 // alignment of the bit-field, because (a) it is not clear what guarantees the
125 // runtime makes us, and (b) we don't have a way to specify that the struct is
126 // at an alignment plus offset.
127 //
128 // Note, there is a subtle invariant here: we can only call this routine on
129 // non-synthesized ivars but we may be called for synthesized ivars. However,
130 // a synthesized ivar can never be a bit-field, so this is safe.
131 const ASTRecordLayout &RL =
132 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
133 uint64_t TypeSizeInBits = RL.getSize();
134 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
135 uint64_t BitOffset = FieldBitOffset % 8;
136 uint64_t ContainingTypeAlign = 8;
137 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000138 uint64_t BitFieldSize =
139 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000140
Daniel Dunbardc406b82010-04-05 21:36:35 +0000141 // Allocate a new CGBitFieldInfo object to describe this access.
142 //
143 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
144 // layout object. However, this is blocked on other cleanups to the
145 // Objective-C code, so for now we just live with allocating a bunch of these
146 // objects.
Daniel Dunbara70fab82010-09-02 23:53:31 +0000147 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
148 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
149 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbardc406b82010-04-05 21:36:35 +0000150
Daniel Dunbar26d2c392010-08-21 04:05:54 +0000151 return LValue::MakeBitfield(V, *Info,
152 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000153}
154
155///
156
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000157namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000158
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000159typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000160
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000161// FIXME: We should find a nicer way to make the labels for metadata, string
162// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000163
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000164class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000165protected:
166 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000167
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000168private:
169 llvm::Constant *getMessageSendFn() const {
170 // id objc_msgSend (id, SEL, ...)
171 std::vector<const llvm::Type*> Params;
172 Params.push_back(ObjectPtrTy);
173 Params.push_back(SelectorPtrTy);
174 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
176 Params, true),
177 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000178 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000179
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000180 llvm::Constant *getMessageSendStretFn() const {
181 // id objc_msgSend_stret (id, SEL, ...)
182 std::vector<const llvm::Type*> Params;
183 Params.push_back(ObjectPtrTy);
184 Params.push_back(SelectorPtrTy);
185 return
Owen Anderson41a75022009-08-13 21:57:51 +0000186 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000187 Params, true),
188 "objc_msgSend_stret");
189
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000190 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000191
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000192 llvm::Constant *getMessageSendFpretFn() const {
193 // FIXME: This should be long double on x86_64?
194 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
195 std::vector<const llvm::Type*> Params;
196 Params.push_back(ObjectPtrTy);
197 Params.push_back(SelectorPtrTy);
198 return
Owen Anderson41a75022009-08-13 21:57:51 +0000199 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
200 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000201 Params,
202 true),
203 "objc_msgSend_fpret");
204
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000205 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000206
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000207 llvm::Constant *getMessageSendSuperFn() const {
208 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
209 const char *SuperName = "objc_msgSendSuper";
210 std::vector<const llvm::Type*> Params;
211 Params.push_back(SuperPtrTy);
212 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000213 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000214 Params, true),
215 SuperName);
216 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000217
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000218 llvm::Constant *getMessageSendSuperFn2() const {
219 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
220 const char *SuperName = "objc_msgSendSuper2";
221 std::vector<const llvm::Type*> Params;
222 Params.push_back(SuperPtrTy);
223 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000224 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000225 Params, true),
226 SuperName);
227 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000228
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000229 llvm::Constant *getMessageSendSuperStretFn() const {
230 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
231 // SEL op, ...)
232 std::vector<const llvm::Type*> Params;
233 Params.push_back(Int8PtrTy);
234 Params.push_back(SuperPtrTy);
235 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000236 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000237 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000238 Params, true),
239 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000240 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000241
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000242 llvm::Constant *getMessageSendSuperStretFn2() const {
243 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
244 // SEL op, ...)
245 std::vector<const llvm::Type*> Params;
246 Params.push_back(Int8PtrTy);
247 Params.push_back(SuperPtrTy);
248 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000249 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000250 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000251 Params, true),
252 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000253 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000254
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000255 llvm::Constant *getMessageSendSuperFpretFn() const {
256 // There is no objc_msgSendSuper_fpret? How can that work?
257 return getMessageSendSuperFn();
258 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000259
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000260 llvm::Constant *getMessageSendSuperFpretFn2() const {
261 // There is no objc_msgSendSuper_fpret? How can that work?
262 return getMessageSendSuperFn2();
263 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000264
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000265protected:
266 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000267
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000268public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000269 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000270 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000271
Daniel Dunbar5d715592008-08-12 05:28:47 +0000272 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
273 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000274
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000275 /// PtrObjectPtrTy - LLVM type for id *
276 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000278 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000279 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000280 /// ProtocolPtrTy - LLVM type for external protocol handles
281 /// (typeof(Protocol))
282 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000283
Daniel Dunbarc722b852008-08-30 03:02:31 +0000284 // SuperCTy - clang type for struct objc_super.
285 QualType SuperCTy;
286 // SuperPtrCTy - clang type for struct objc_super *.
287 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000288
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000289 /// SuperTy - LLVM type for struct objc_super.
290 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000291 /// SuperPtrTy - LLVM type for struct objc_super *.
292 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000293
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000294 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
295 /// in GCC parlance).
296 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000297
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000298 /// PropertyListTy - LLVM type for struct objc_property_list
299 /// (_prop_list_t in GCC parlance).
300 const llvm::StructType *PropertyListTy;
301 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
302 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000303
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000304 // MethodTy - LLVM type for struct objc_method.
305 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000306
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000307 /// CacheTy - LLVM type for struct objc_cache.
308 const llvm::Type *CacheTy;
309 /// CachePtrTy - LLVM type for struct objc_cache *.
310 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000311
Chris Lattnerce8754e2009-04-22 02:44:54 +0000312 llvm::Constant *getGetPropertyFn() {
313 CodeGen::CodeGenTypes &Types = CGM.getTypes();
314 ASTContext &Ctx = CGM.getContext();
315 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000316 llvm::SmallVector<CanQualType,4> Params;
317 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
318 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000319 Params.push_back(IdType);
320 Params.push_back(SelType);
321 Params.push_back(Ctx.LongTy);
322 Params.push_back(Ctx.BoolTy);
323 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000324 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000325 FunctionType::ExtInfo()),
326 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000327 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
328 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000329
Chris Lattnerce8754e2009-04-22 02:44:54 +0000330 llvm::Constant *getSetPropertyFn() {
331 CodeGen::CodeGenTypes &Types = CGM.getTypes();
332 ASTContext &Ctx = CGM.getContext();
333 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000334 llvm::SmallVector<CanQualType,6> Params;
335 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
336 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000337 Params.push_back(IdType);
338 Params.push_back(SelType);
339 Params.push_back(Ctx.LongTy);
340 Params.push_back(IdType);
341 Params.push_back(Ctx.BoolTy);
342 Params.push_back(Ctx.BoolTy);
343 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000344 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000345 FunctionType::ExtInfo()),
346 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000347 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
348 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000349
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000350
351 llvm::Constant *getCopyStructFn() {
352 CodeGen::CodeGenTypes &Types = CGM.getTypes();
353 ASTContext &Ctx = CGM.getContext();
354 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
355 llvm::SmallVector<CanQualType,5> Params;
356 Params.push_back(Ctx.VoidPtrTy);
357 Params.push_back(Ctx.VoidPtrTy);
358 Params.push_back(Ctx.LongTy);
359 Params.push_back(Ctx.BoolTy);
360 Params.push_back(Ctx.BoolTy);
361 const llvm::FunctionType *FTy =
362 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
363 FunctionType::ExtInfo()),
364 false);
365 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
366 }
367
Chris Lattnerce8754e2009-04-22 02:44:54 +0000368 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000369 CodeGen::CodeGenTypes &Types = CGM.getTypes();
370 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000371 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000372 llvm::SmallVector<CanQualType,1> Params;
373 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000374 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000375 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000376 FunctionType::ExtInfo()),
377 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000378 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
379 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000380
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000381 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000382 llvm::Constant *getGcReadWeakFn() {
383 // id objc_read_weak (id *)
384 std::vector<const llvm::Type*> Args;
385 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000386 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000387 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000388 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000389 }
390
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000391 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000392 llvm::Constant *getGcAssignWeakFn() {
393 // id objc_assign_weak (id, id *)
394 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
395 Args.push_back(ObjectPtrTy->getPointerTo());
396 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000397 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000398 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000400
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000401 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000402 llvm::Constant *getGcAssignGlobalFn() {
403 // id objc_assign_global(id, id *)
404 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
405 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000406 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000407 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000408 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
409 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000410
Fariborz Jahanian217af242010-07-20 20:30:03 +0000411 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
412 llvm::Constant *getGcAssignThreadLocalFn() {
413 // id objc_assign_threadlocal(id src, id * dest)
414 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
415 Args.push_back(ObjectPtrTy->getPointerTo());
416 llvm::FunctionType *FTy =
417 llvm::FunctionType::get(ObjectPtrTy, Args, false);
418 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
419 }
420
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000421 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000422 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000423 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000424 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
425 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000426 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000427 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000428 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000429 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
430 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000431
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000432 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
433 llvm::Constant *GcMemmoveCollectableFn() {
434 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
435 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
436 Args.push_back(Int8PtrTy);
437 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000438 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000439 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
440 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000441
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000442 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000443 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000444 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000445 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
446 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000447 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000448 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000449 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
450 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000451
452 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000453 llvm::Constant *getExceptionThrowFn() {
454 // void objc_exception_throw(id)
455 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
456 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000457 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000458 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
459 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000460
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000461 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
462 llvm::Constant *getExceptionRethrowFn() {
463 // void objc_exception_rethrow(void)
464 std::vector<const llvm::Type*> Args;
465 llvm::FunctionType *FTy =
John McCall17afe452010-10-16 08:21:07 +0000466 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000467 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
468 }
469
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000470 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000471 llvm::Constant *getSyncEnterFn() {
472 // void objc_sync_enter (id)
473 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
474 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000475 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000476 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
477 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000478
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000479 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000480 llvm::Constant *getSyncExitFn() {
481 // void objc_sync_exit (id)
482 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
483 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000484 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000485 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
486 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000487
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000488 llvm::Constant *getSendFn(bool IsSuper) const {
489 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
490 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000491
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000492 llvm::Constant *getSendFn2(bool IsSuper) const {
493 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
494 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000495
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000496 llvm::Constant *getSendStretFn(bool IsSuper) const {
497 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
498 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000499
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000500 llvm::Constant *getSendStretFn2(bool IsSuper) const {
501 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
502 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000503
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000504 llvm::Constant *getSendFpretFn(bool IsSuper) const {
505 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
506 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000508 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
509 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
510 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000511
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000512 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
513 ~ObjCCommonTypesHelper(){}
514};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000515
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000516/// ObjCTypesHelper - Helper class that encapsulates lazy
517/// construction of varies types used during ObjC generation.
518class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000519public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000520 /// SymtabTy - LLVM type for struct objc_symtab.
521 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000522 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
523 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000524 /// ModuleTy - LLVM type for struct objc_module.
525 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000526
Daniel Dunbarb036db82008-08-13 03:21:16 +0000527 /// ProtocolTy - LLVM type for struct objc_protocol.
528 const llvm::StructType *ProtocolTy;
529 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
530 const llvm::Type *ProtocolPtrTy;
531 /// ProtocolExtensionTy - LLVM type for struct
532 /// objc_protocol_extension.
533 const llvm::StructType *ProtocolExtensionTy;
534 /// ProtocolExtensionTy - LLVM type for struct
535 /// objc_protocol_extension *.
536 const llvm::Type *ProtocolExtensionPtrTy;
537 /// MethodDescriptionTy - LLVM type for struct
538 /// objc_method_description.
539 const llvm::StructType *MethodDescriptionTy;
540 /// MethodDescriptionListTy - LLVM type for struct
541 /// objc_method_description_list.
542 const llvm::StructType *MethodDescriptionListTy;
543 /// MethodDescriptionListPtrTy - LLVM type for struct
544 /// objc_method_description_list *.
545 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000546 /// ProtocolListTy - LLVM type for struct objc_property_list.
547 const llvm::Type *ProtocolListTy;
548 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
549 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000550 /// CategoryTy - LLVM type for struct objc_category.
551 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000552 /// ClassTy - LLVM type for struct objc_class.
553 const llvm::StructType *ClassTy;
554 /// ClassPtrTy - LLVM type for struct objc_class *.
555 const llvm::Type *ClassPtrTy;
556 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
557 const llvm::StructType *ClassExtensionTy;
558 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
559 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000560 // IvarTy - LLVM type for struct objc_ivar.
561 const llvm::StructType *IvarTy;
562 /// IvarListTy - LLVM type for struct objc_ivar_list.
563 const llvm::Type *IvarListTy;
564 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
565 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000566 /// MethodListTy - LLVM type for struct objc_method_list.
567 const llvm::Type *MethodListTy;
568 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
569 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000570
Anders Carlsson9ff22482008-09-09 10:10:21 +0000571 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
572 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573
Anders Carlsson9ff22482008-09-09 10:10:21 +0000574 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000575 llvm::Constant *getExceptionTryEnterFn() {
576 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000577 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000578 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000579 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000580 Params, false),
581 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000582 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000583
584 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000585 llvm::Constant *getExceptionTryExitFn() {
586 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000587 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000588 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000589 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590 Params, false),
591 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000592 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000593
594 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000595 llvm::Constant *getExceptionExtractFn() {
596 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000597 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000599 Params, false),
600 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000601
Chris Lattnerc6406db2009-04-22 02:26:14 +0000602 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000603
Anders Carlsson9ff22482008-09-09 10:10:21 +0000604 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000605 llvm::Constant *getExceptionMatchFn() {
606 std::vector<const llvm::Type*> Params;
607 Params.push_back(ClassPtrTy);
608 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000610 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611 Params, false),
612 "objc_exception_match");
613
Chris Lattnerc6406db2009-04-22 02:26:14 +0000614 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000615
Anders Carlsson9ff22482008-09-09 10:10:21 +0000616 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000617 llvm::Constant *getSetJmpFn() {
618 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000619 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000620 return
Owen Anderson41a75022009-08-13 21:57:51 +0000621 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000622 Params, false),
623 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000624
Chris Lattnerc6406db2009-04-22 02:26:14 +0000625 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000627public:
628 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000629 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000630};
631
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000632/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000633/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000634class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000635public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000636
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000637 // MethodListnfABITy - LLVM for struct _method_list_t
638 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000639
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000640 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
641 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000642
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000643 // ProtocolnfABITy = LLVM for struct _protocol_t
644 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000645
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000646 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
647 const llvm::Type *ProtocolnfABIPtrTy;
648
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000649 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
650 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000651
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000652 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
653 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000654
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000655 // ClassnfABITy - LLVM for struct _class_t
656 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000657
Fariborz Jahanian71394042009-01-23 23:53:38 +0000658 // ClassnfABIPtrTy - LLVM for struct _class_t*
659 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000660
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000661 // IvarnfABITy - LLVM for struct _ivar_t
662 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000663
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000664 // IvarListnfABITy - LLVM for struct _ivar_list_t
665 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000666
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000667 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
668 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000669
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000670 // ClassRonfABITy - LLVM for struct _class_ro_t
671 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000672
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000673 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
674 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000675
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000676 // CategorynfABITy - LLVM for struct _category_t
677 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000678
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000679 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000680
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000681 // MessageRefTy - LLVM for:
682 // struct _message_ref_t {
683 // IMP messenger;
684 // SEL name;
685 // };
686 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000687 // MessageRefCTy - clang type for struct _message_ref_t
688 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000689
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000690 // MessageRefPtrTy - LLVM for struct _message_ref_t*
691 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000692 // MessageRefCPtrTy - clang type for struct _message_ref_t*
693 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000694
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000695 // MessengerTy - Type of the messenger (shown as IMP above)
696 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000697
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000698 // SuperMessageRefTy - LLVM for:
699 // struct _super_message_ref_t {
700 // SUPER_IMP messenger;
701 // SEL name;
702 // };
703 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000705 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
706 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000707
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000708 llvm::Constant *getMessageSendFixupFn() {
709 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
710 std::vector<const llvm::Type*> Params;
711 Params.push_back(ObjectPtrTy);
712 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000714 Params, true),
715 "objc_msgSend_fixup");
716 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000717
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000718 llvm::Constant *getMessageSendFpretFixupFn() {
719 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
720 std::vector<const llvm::Type*> Params;
721 Params.push_back(ObjectPtrTy);
722 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000723 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000724 Params, true),
725 "objc_msgSend_fpret_fixup");
726 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000727
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000728 llvm::Constant *getMessageSendStretFixupFn() {
729 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
730 std::vector<const llvm::Type*> Params;
731 Params.push_back(ObjectPtrTy);
732 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000733 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000734 Params, true),
735 "objc_msgSend_stret_fixup");
736 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000737
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000738 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000739 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000740 // struct _super_message_ref_t*, ...)
741 std::vector<const llvm::Type*> Params;
742 Params.push_back(SuperPtrTy);
743 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000744 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000745 Params, true),
746 "objc_msgSendSuper2_fixup");
747 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000748
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000749 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000751 // struct _super_message_ref_t*, ...)
752 std::vector<const llvm::Type*> Params;
753 Params.push_back(SuperPtrTy);
754 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000755 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000756 Params, true),
757 "objc_msgSendSuper2_stret_fixup");
758 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000759
Chris Lattnera7c00b42009-04-22 02:15:23 +0000760 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000761 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000762 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000763 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000764
Chris Lattnera7c00b42009-04-22 02:15:23 +0000765 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000766
Chris Lattnera7c00b42009-04-22 02:15:23 +0000767 llvm::Constant *getObjCBeginCatchFn() {
768 std::vector<const llvm::Type*> Params;
769 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000770 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000771 Params, false),
772 "objc_begin_catch");
773 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000774
775 const llvm::StructType *EHTypeTy;
776 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000777
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000778 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
779 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000780};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000781
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000782class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000783public:
784 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000785 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000786 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000787 unsigned ivar_bytepos;
788 unsigned ivar_size;
789 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000791
792 // Allow sorting based on byte pos.
793 bool operator<(const GC_IVAR &b) const {
794 return ivar_bytepos < b.ivar_bytepos;
795 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000796 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000798 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000799 public:
800 unsigned skip;
801 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000802 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000803 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000804 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000805
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000806protected:
807 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000808 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000809 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000810 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000811
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000812 // gc ivar layout bitmap calculation helper caches.
813 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
814 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000815
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000816 /// LazySymbols - Symbols to generate a lazy reference for. See
817 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000818 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000819
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000820 /// DefinedSymbols - External symbols which are defined by this
821 /// module. The symbols in this list and LazySymbols are used to add
822 /// special linker symbols which ensure that Objective-C modules are
823 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000824 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000825
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000826 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000827 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000828
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000829 /// MethodVarNames - uniqued method variable names.
830 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000831
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000832 /// DefinedCategoryNames - list of category names in form Class_Category.
833 llvm::SetVector<std::string> DefinedCategoryNames;
834
Daniel Dunbarb036db82008-08-13 03:21:16 +0000835 /// MethodVarTypes - uniqued method type signatures. We have to use
836 /// a StringMap here because have no other unique reference.
837 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000838
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000839 /// MethodDefinitions - map of methods which have been defined in
840 /// this translation unit.
841 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000842
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000843 /// PropertyNames - uniqued method variable names.
844 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000845
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000846 /// ClassReferences - uniqued class references.
847 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000848
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000849 /// SelectorReferences - uniqued selector references.
850 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Daniel Dunbarb036db82008-08-13 03:21:16 +0000852 /// Protocols - Protocols for which an objc_protocol structure has
853 /// been emitted. Forward declarations are handled by creating an
854 /// empty structure whose initializer is filled in when/if defined.
855 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000856
Daniel Dunbarc475d422008-10-29 22:36:39 +0000857 /// DefinedProtocols - Protocols which have actually been
858 /// defined. We should not need this, see FIXME in GenerateProtocol.
859 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000860
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000861 /// DefinedClasses - List of defined classes.
862 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000863
864 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
865 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000867 /// DefinedCategories - List of defined categories.
868 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000869
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000870 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
871 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000873 /// GetNameForMethod - Return a name for the given method.
874 /// \param[out] NameOut - The return value.
875 void GetNameForMethod(const ObjCMethodDecl *OMD,
876 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000877 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000878
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000879 /// GetMethodVarName - Return a unique constant for the given
880 /// selector's name. The return value has type char *.
881 llvm::Constant *GetMethodVarName(Selector Sel);
882 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000883
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000884 /// GetMethodVarType - Return a unique constant for the given
885 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000887 // FIXME: This is a horrible name.
888 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000889 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000891 /// GetPropertyName - Return a unique constant for the given
892 /// name. The return value has type char *.
893 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000894
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000895 // FIXME: This can be dropped once string functions are unified.
896 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
897 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000898
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000899 /// GetClassName - Return a unique constant for the given selector's
900 /// name. The return value has type char *.
901 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000903 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
904
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000905 /// BuildIvarLayout - Builds ivar layout bitmap for the class
906 /// implementation for the __strong or __weak case.
907 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000908 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
909 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000910
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000911 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000912
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000913 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000914 unsigned int BytePos, bool ForStrongLayout,
915 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000916 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000917 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000918 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000919 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000920 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000921 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000922
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000923 /// GetIvarLayoutName - Returns a unique constant for the given
924 /// ivar layout bitmap.
925 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
926 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000927
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000928 /// EmitPropertyList - Emit the given property list. The return
929 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000930 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000931 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000932 const ObjCContainerDecl *OCD,
933 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000934
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000935 /// PushProtocolProperties - Push protocol's property on the input stack.
936 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
937 std::vector<llvm::Constant*> &Properties,
938 const Decl *Container,
939 const ObjCProtocolDecl *PROTO,
940 const ObjCCommonTypesHelper &ObjCTypes);
941
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000942 /// GetProtocolRef - Return a reference to the internal protocol
943 /// description, creating an empty one if it has not been
944 /// defined. The return value has type ProtocolPtrTy.
945 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000946
Daniel Dunbar30c65362009-03-09 20:09:19 +0000947 /// CreateMetadataVar - Create a global variable with internal
948 /// linkage for use by the Objective-C runtime.
949 ///
950 /// This is a convenience wrapper which not only creates the
951 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000952 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000953 ///
954 /// \param Name - The variable name.
955 /// \param Init - The variable initializer; this is also used to
956 /// define the type of the variable.
957 /// \param Section - The section the variable should go into, or 0.
958 /// \param Align - The alignment for the variable, or 0.
959 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000960 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000961 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000962 llvm::Constant *Init,
963 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000964 unsigned Align,
965 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000966
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000967 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000968 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000969 QualType ResultType,
970 llvm::Value *Sel,
971 llvm::Value *Arg0,
972 QualType Arg0Ty,
973 bool IsSuper,
974 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000975 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000976 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000977
Daniel Dunbar5e639272010-04-25 20:39:01 +0000978 /// EmitImageInfo - Emit the image info marker used to encode some module
979 /// level information.
980 void EmitImageInfo();
981
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000982public:
Owen Andersonae86c192009-07-13 04:10:07 +0000983 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000984 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000985
David Chisnall481e3a82010-01-23 02:40:42 +0000986 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000987
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000988 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
989 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000990
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000991 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000992
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000993 /// GetOrEmitProtocol - Get the protocol object for the given
994 /// declaration, emitting it if necessary. The return value has type
995 /// ProtocolPtrTy.
996 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000997
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000998 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
999 /// object for the given declaration, emitting it if needed. These
1000 /// forward references will be filled in with empty bodies if no
1001 /// definition is seen. The return value has type ProtocolPtrTy.
1002 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001003 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1004 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001005
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001006};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001007
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001008class CGObjCMac : public CGObjCCommonMac {
1009private:
1010 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001011
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001012 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001013 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001014 void EmitModuleInfo();
1015
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001016 /// EmitModuleSymols - Emit module symbols, the list of defined
1017 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001018 llvm::Constant *EmitModuleSymbols();
1019
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001020 /// FinishModule - Write out global data structures at the end of
1021 /// processing a translation unit.
1022 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001023
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001024 /// EmitClassExtension - Generate the class extension structure used
1025 /// to store the weak ivar layout and properties. The return value
1026 /// has type ClassExtensionPtrTy.
1027 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1028
1029 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1030 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001031 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001032 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001033
1034 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1035 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001036
1037 /// EmitIvarList - Emit the ivar list for the given
1038 /// implementation. If ForClass is true the list of class ivars
1039 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1040 /// interface ivars will be emitted. The return value has type
1041 /// IvarListPtrTy.
1042 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001043 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001044
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001045 /// EmitMetaClass - Emit a forward reference to the class structure
1046 /// for the metaclass of the given interface. The return value has
1047 /// type ClassPtrTy.
1048 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1049
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001050 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001051 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001052 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1053 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001054 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001055
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001056 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001057
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001058 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001059
1060 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001061 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001062 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001063 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001064 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001065
1066 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001067 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001068 /// - TypeName: The name for the type containing the methods.
1069 /// - IsProtocol: True iff these methods are for a protocol.
1070 /// - ClassMethds: True iff these are class methods.
1071 /// - Required: When true, only "required" methods are
1072 /// listed. Similarly, when false only "optional" methods are
1073 /// listed. For classes this should always be true.
1074 /// - begin, end: The method list to output.
1075 ///
1076 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001077 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001078 const char *Section,
1079 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001080
Daniel Dunbarc475d422008-10-29 22:36:39 +00001081 /// GetOrEmitProtocol - Get the protocol object for the given
1082 /// declaration, emitting it if necessary. The return value has type
1083 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001084 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001085
1086 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1087 /// object for the given declaration, emitting it if needed. These
1088 /// forward references will be filled in with empty bodies if no
1089 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001090 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001091
Daniel Dunbarb036db82008-08-13 03:21:16 +00001092 /// EmitProtocolExtension - Generate the protocol extension
1093 /// structure used to store optional instance and class methods, and
1094 /// protocol properties. The return value has type
1095 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001096 llvm::Constant *
1097 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1098 const ConstantVector &OptInstanceMethods,
1099 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001100
1101 /// EmitProtocolList - Generate the list of referenced
1102 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001103 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001104 ObjCProtocolDecl::protocol_iterator begin,
1105 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001106
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001107 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1108 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001109 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1110 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001111
1112public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001113 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001114
Fariborz Jahanian71394042009-01-23 23:53:38 +00001115 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001116
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001117 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001118 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001119 QualType ResultType,
1120 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001121 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001122 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001123 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001124 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001125
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001126 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001127 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001128 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001129 QualType ResultType,
1130 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001131 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001132 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001133 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001134 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001135 const CallArgList &CallArgs,
1136 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001137
Daniel Dunbarcb463852008-11-01 01:53:16 +00001138 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001139 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001140
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001141 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1142 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001143
1144 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1145 /// untyped one.
1146 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1147 const ObjCMethodDecl *Method);
1148
John McCall2ca705e2010-07-24 00:37:23 +00001149 virtual llvm::Constant *GetEHType(QualType T);
1150
Daniel Dunbar92992502008-08-15 22:20:32 +00001151 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001152
Daniel Dunbar92992502008-08-15 22:20:32 +00001153 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001154
Daniel Dunbarcb463852008-11-01 01:53:16 +00001155 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001156 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001157
Chris Lattnerd4808922009-03-22 21:03:39 +00001158 virtual llvm::Constant *GetPropertyGetFunction();
1159 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001160 virtual llvm::Constant *GetGetStructFunction();
1161 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001162 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001163
John McCallbd309292010-07-06 01:34:17 +00001164 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1165 const ObjCAtTryStmt &S);
1166 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1167 const ObjCAtSynchronizedStmt &S);
1168 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001169 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1170 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001171 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001172 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001173 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001174 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001175 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001176 llvm::Value *src, llvm::Value *dest,
1177 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001178 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001179 llvm::Value *src, llvm::Value *dest,
1180 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001181 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1182 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001183 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1184 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001185 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001186
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001187 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1188 QualType ObjectTy,
1189 llvm::Value *BaseValue,
1190 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001191 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001192 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001193 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001194 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001195};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001197class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001198private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001199 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001200 llvm::GlobalVariable* ObjCEmptyCacheVar;
1201 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001203 /// SuperClassReferences - uniqued super class references.
1204 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001205
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001206 /// MetaClassReferences - uniqued meta class references.
1207 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001208
1209 /// EHTypeReferences - uniqued class ehtype references.
1210 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001211
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001212 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001213 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001214 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001215
Fariborz Jahanian67260552009-11-17 21:37:35 +00001216 /// DefinedMetaClasses - List of defined meta-classes.
1217 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1218
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001219 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001220 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001221 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001222
Fariborz Jahanian71394042009-01-23 23:53:38 +00001223 /// FinishNonFragileABIModule - Write out global data structures at the end of
1224 /// processing a translation unit.
1225 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001226
Daniel Dunbar19573e72009-05-15 21:48:48 +00001227 /// AddModuleClassList - Add the given list of class pointers to the
1228 /// module with the provided symbol and section names.
1229 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1230 const char *SymbolName,
1231 const char *SectionName);
1232
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001233 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1234 unsigned InstanceStart,
1235 unsigned InstanceSize,
1236 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001237 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001238 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001239 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001240 llvm::Constant *ClassRoGV,
1241 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001243 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001244
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001245 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001246
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001247 /// EmitMethodList - Emit the method list for the given
1248 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001249 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001250 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001251 const ConstantVector &Methods);
1252 /// EmitIvarList - Emit the ivar list for the given
1253 /// implementation. If ForClass is true the list of class ivars
1254 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1255 /// interface ivars will be emitted. The return value has type
1256 /// IvarListnfABIPtrTy.
1257 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001258
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001259 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001260 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001261 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001262
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001263 /// GetOrEmitProtocol - Get the protocol object for the given
1264 /// declaration, emitting it if necessary. The return value has type
1265 /// ProtocolPtrTy.
1266 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001268 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1269 /// object for the given declaration, emitting it if needed. These
1270 /// forward references will be filled in with empty bodies if no
1271 /// definition is seen. The return value has type ProtocolPtrTy.
1272 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001274 /// EmitProtocolList - Generate the list of referenced
1275 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001276 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001277 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001278 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001280 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001281 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001282 QualType ResultType,
1283 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001284 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001285 QualType Arg0Ty,
1286 bool IsSuper,
1287 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001288
1289 /// GetClassGlobal - Return the global variable for the Objective-C
1290 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001291 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001293 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001294 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001295 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001296 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001297
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001298 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1299 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001300 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1301 const ObjCInterfaceDecl *ID);
1302
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001303 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1304 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001306 const ObjCInterfaceDecl *ID);
1307
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001308 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1309 /// the given ivar.
1310 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001311 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001312 const ObjCInterfaceDecl *ID,
1313 const ObjCIvarDecl *Ivar);
1314
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001315 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1316 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001317 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1318 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001319
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001320 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001321 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001322 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001323 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001324
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001325 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001326 return "OBJC_METACLASS_$_";
1327 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001328
Daniel Dunbar15894b72009-04-07 05:48:37 +00001329 const char *getClassSymbolPrefix() const {
1330 return "OBJC_CLASS_$_";
1331 }
1332
Daniel Dunbar961202372009-05-03 12:57:56 +00001333 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001334 uint32_t &InstanceStart,
1335 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001336
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001337 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001338 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001339 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1340 return CGM.getContext().Selectors.getSelector(0, &II);
1341 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001342
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001343 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001344 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1345 return CGM.getContext().Selectors.getSelector(1, &II);
1346 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001347
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001348 /// ImplementationIsNonLazy - Check whether the given category or
1349 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001350 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001351
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001352public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001353 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001354 // FIXME. All stubs for now!
1355 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356
Fariborz Jahanian71394042009-01-23 23:53:38 +00001357 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001358 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001359 QualType ResultType,
1360 Selector Sel,
1361 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001362 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001363 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001364 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001365
1366 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001367 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001368 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001369 QualType ResultType,
1370 Selector Sel,
1371 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001372 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001373 llvm::Value *Receiver,
1374 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001375 const CallArgList &CallArgs,
1376 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001377
Fariborz Jahanian71394042009-01-23 23:53:38 +00001378 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001379 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001380
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001381 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1382 bool lvalue = false)
1383 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001384
1385 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1386 /// untyped one.
1387 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1388 const ObjCMethodDecl *Method)
1389 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001390
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001391 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001392
Fariborz Jahanian71394042009-01-23 23:53:38 +00001393 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001394 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001395 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001396
John McCall2ca705e2010-07-24 00:37:23 +00001397 virtual llvm::Constant *GetEHType(QualType T);
1398
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001399 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001400 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001401 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001402 virtual llvm::Constant *GetPropertySetFunction() {
1403 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001404 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001405
David Chisnall168b80f2010-12-26 22:13:16 +00001406 virtual llvm::Constant *GetSetStructFunction() {
1407 return ObjCTypes.getCopyStructFn();
1408 }
1409 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001410 return ObjCTypes.getCopyStructFn();
1411 }
1412
Chris Lattnerd4808922009-03-22 21:03:39 +00001413 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001414 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001415 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001416
John McCallbd309292010-07-06 01:34:17 +00001417 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1418 const ObjCAtTryStmt &S);
1419 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1420 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001421 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001422 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001423 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001424 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001425 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001426 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001427 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001428 llvm::Value *src, llvm::Value *dest,
1429 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001430 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001431 llvm::Value *src, llvm::Value *dest,
1432 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001433 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001434 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001435 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1436 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001437 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001438 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1439 QualType ObjectTy,
1440 llvm::Value *BaseValue,
1441 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001442 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001443 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001444 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001445 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001446};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001447
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001448} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001449
1450/* *** Helper Functions *** */
1451
1452/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001453static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001454 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001455 unsigned idx0,
1456 unsigned idx1) {
1457 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001458 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1459 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001460 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001461 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001462}
1463
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001464/// hasObjCExceptionAttribute - Return true if this class or any super
1465/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001466static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001467 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001468 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001469 return true;
1470 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001471 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001472 return false;
1473}
1474
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001475/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001476
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001477CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001478 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001479 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001480 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001481}
1482
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001483/// GetClass - Return a reference to the class for the given interface
1484/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001485llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001486 const ObjCInterfaceDecl *ID) {
1487 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001488}
1489
1490/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001491llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1492 bool lval) {
1493 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001494}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001495llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001496 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001497 return EmitSelector(Builder, Method->getSelector());
1498}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001499
John McCall2ca705e2010-07-24 00:37:23 +00001500llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1501 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1502 return 0;
1503}
1504
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001505/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001506/*
1507 struct __builtin_CFString {
1508 const int *isa; // point to __CFConstantStringClassReference
1509 int flags;
1510 const char *str;
1511 long length;
1512 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001513*/
1514
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001515/// or Generate a constant NSString object.
1516/*
1517 struct __builtin_NSString {
1518 const int *isa; // point to __NSConstantStringClassReference
1519 const char *str;
1520 unsigned int length;
1521 };
1522*/
1523
Fariborz Jahanian71394042009-01-23 23:53:38 +00001524llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001525 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001526 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1527 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001528 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001529}
1530
1531/// Generates a message send where the super is the receiver. This is
1532/// a message send to self with special delivery semantics indicating
1533/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001534CodeGen::RValue
1535CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001536 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001537 QualType ResultType,
1538 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001539 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001540 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001541 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001542 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001543 const CodeGen::CallArgList &CallArgs,
1544 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001545 // Create and init a super structure; this is a (receiver, class)
1546 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001547 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001548 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001549 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001550 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001551 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001552 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001553
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001554 // If this is a class message the metaclass is passed as the target.
1555 llvm::Value *Target;
1556 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001557 if (isCategoryImpl) {
1558 // Message sent to 'super' in a class method defined in a category
1559 // implementation requires an odd treatment.
1560 // If we are in a class method, we must retrieve the
1561 // _metaclass_ for the current class, pointed at by
1562 // the class's "isa" pointer. The following assumes that
1563 // isa" is the first ivar in a class (which it must be).
1564 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1565 Target = CGF.Builder.CreateStructGEP(Target, 0);
1566 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001567 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001568 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1569 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1570 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1571 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001572 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001573 }
1574 else if (isCategoryImpl)
1575 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1576 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001577 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1578 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1579 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001580 }
Mike Stump18bb9282009-05-16 07:57:57 +00001581 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1582 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001583 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001584 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001585 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001586 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001587 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001588 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001589 EmitSelector(CGF.Builder, Sel),
1590 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001591 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001592}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001593
1594/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001595CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001596 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001597 QualType ResultType,
1598 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001599 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001600 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001601 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001602 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001603 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001604 EmitSelector(CGF.Builder, Sel),
1605 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001606 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001607}
1608
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001609CodeGen::RValue
1610CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001611 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001612 QualType ResultType,
1613 llvm::Value *Sel,
1614 llvm::Value *Arg0,
1615 QualType Arg0Ty,
1616 bool IsSuper,
1617 const CallArgList &CallArgs,
1618 const ObjCMethodDecl *Method,
1619 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001620 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001621 if (!IsSuper)
1622 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001623 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001624 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001625 CGF.getContext().getObjCSelType()));
1626 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001627
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001628 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001629 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001630 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001631 const llvm::FunctionType *FTy =
1632 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001633
Anders Carlsson280e61f12010-06-21 20:59:55 +00001634 if (Method)
1635 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1636 CGM.getContext().getCanonicalType(ResultType) &&
1637 "Result type mismatch!");
1638
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001639 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001640 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001641 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001642 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001643 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1644 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1645 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001646 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001647 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001648 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001649 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001650 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001651 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001652}
1653
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001654static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1655 if (FQT.isObjCGCStrong())
1656 return Qualifiers::Strong;
1657
1658 if (FQT.isObjCGCWeak())
1659 return Qualifiers::Weak;
1660
1661 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1662 return Qualifiers::Strong;
1663
1664 if (const PointerType *PT = FQT->getAs<PointerType>())
1665 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1666
1667 return Qualifiers::GCNone;
1668}
1669
John McCall351762c2011-02-07 10:33:21 +00001670llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1671 const CGBlockInfo &blockInfo) {
1672 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001673 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001674
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001675 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001676 return nullPtr;
1677
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001678 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001679 SkipIvars.clear();
1680 IvarsInfo.clear();
1681 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1682 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1683
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001684 // __isa is the first field in block descriptor and must assume by runtime's
1685 // convention that it is GC'able.
1686 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001687
1688 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1689
1690 // Calculate the basic layout of the block structure.
1691 const llvm::StructLayout *layout =
1692 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1693
1694 // Ignore the optional 'this' capture: C++ objects are not assumed
1695 // to be GC'ed.
1696
1697 // Walk the captured variables.
1698 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1699 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1700 const VarDecl *variable = ci->getVariable();
1701 QualType type = variable->getType();
1702
1703 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1704
1705 // Ignore constant captures.
1706 if (capture.isConstant()) continue;
1707
1708 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1709
1710 // __block variables are passed by their descriptor address.
1711 if (ci->isByRef()) {
1712 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001713 continue;
John McCall351762c2011-02-07 10:33:21 +00001714 }
1715
1716 assert(!type->isArrayType() && "array variable should not be caught");
1717 if (const RecordType *record = type->getAs<RecordType>()) {
1718 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001719 continue;
1720 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001721
John McCall351762c2011-02-07 10:33:21 +00001722 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1723 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1724
1725 if (GCAttr == Qualifiers::Strong)
1726 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1727 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001728 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001729 SkipIvars.push_back(GC_IVAR(fieldOffset,
1730 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001731 }
1732
1733 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001734 return nullPtr;
1735
1736 // Sort on byte position; captures might not be allocated in order,
1737 // and unions can do funny things.
1738 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1739 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001740
1741 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001742 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001743 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1744 printf("\n block variable layout for block: ");
1745 const unsigned char *s = (unsigned char*)BitMap.c_str();
1746 for (unsigned i = 0; i < BitMap.size(); i++)
1747 if (!(s[i] & 0xf0))
1748 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1749 else
1750 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1751 printf("\n");
1752 }
1753
1754 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001755}
1756
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001757llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001758 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001759 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001760 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001761 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1762
Owen Andersonade90fd2009-07-29 18:54:39 +00001763 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001764 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001765}
1766
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001767void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001768 // FIXME: We shouldn't need this, the protocol decl should contain enough
1769 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001770 DefinedProtocols.insert(PD->getIdentifier());
1771
1772 // If we have generated a forward reference to this protocol, emit
1773 // it now. Otherwise do nothing, the protocol objects are lazily
1774 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001775 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001776 GetOrEmitProtocol(PD);
1777}
1778
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001779llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001780 if (DefinedProtocols.count(PD->getIdentifier()))
1781 return GetOrEmitProtocol(PD);
1782 return GetOrEmitProtocolRef(PD);
1783}
1784
Daniel Dunbarb036db82008-08-13 03:21:16 +00001785/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001786// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1787struct _objc_protocol {
1788struct _objc_protocol_extension *isa;
1789char *protocol_name;
1790struct _objc_protocol_list *protocol_list;
1791struct _objc__method_prototype_list *instance_methods;
1792struct _objc__method_prototype_list *class_methods
1793};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001794
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001795See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001796*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001797llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1798 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1799
1800 // Early exit if a defining object has already been generated.
1801 if (Entry && Entry->hasInitializer())
1802 return Entry;
1803
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001804 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001805 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001806 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1807
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001808 // Construct method lists.
1809 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1810 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001811 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001812 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001813 ObjCMethodDecl *MD = *i;
1814 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1815 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1816 OptInstanceMethods.push_back(C);
1817 } else {
1818 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001819 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001820 }
1821
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001822 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001823 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001824 ObjCMethodDecl *MD = *i;
1825 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1826 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1827 OptClassMethods.push_back(C);
1828 } else {
1829 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001831 }
1832
Daniel Dunbarb036db82008-08-13 03:21:16 +00001833 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001834 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001835 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001836 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001837 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001838 PD->protocol_begin(),
1839 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001840 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001841 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001842 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1843 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001844 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001845 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001846 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1847 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001848 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001849 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001850
Daniel Dunbarb036db82008-08-13 03:21:16 +00001851 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001852 // Already created, fix the linkage and update the initializer.
1853 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854 Entry->setInitializer(Init);
1855 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001856 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001857 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001860 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862 // FIXME: Is this necessary? Why only for protocol?
1863 Entry->setAlignment(4);
1864 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001865 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001866
1867 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868}
1869
Daniel Dunbarc475d422008-10-29 22:36:39 +00001870llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1872
1873 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001874 // We use the initializer as a marker of whether this is a forward
1875 // reference or not. At module finalization we add the empty
1876 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001877 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001878 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001879 llvm::GlobalValue::ExternalLinkage,
1880 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001881 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001882 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001883 // FIXME: Is this necessary? Why only for protocol?
1884 Entry->setAlignment(4);
1885 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001886
Daniel Dunbarb036db82008-08-13 03:21:16 +00001887 return Entry;
1888}
1889
1890/*
1891 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001892 uint32_t size;
1893 struct objc_method_description_list *optional_instance_methods;
1894 struct objc_method_description_list *optional_class_methods;
1895 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001896 };
1897*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001898llvm::Constant *
1899CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1900 const ConstantVector &OptInstanceMethods,
1901 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001902 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001903 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001904 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001905 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001906 Values[1] =
1907 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001908 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001909 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1910 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001911 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001912 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001913 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1914 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001915 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001916 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001917
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001918 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001919 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001920 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001921 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001922
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001923 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001924 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001925
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001926 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001927 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001928 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001929 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001930}
1931
1932/*
1933 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001934 struct objc_protocol_list *next;
1935 long count;
1936 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001937 };
1938*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001939llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001940CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001941 ObjCProtocolDecl::protocol_iterator begin,
1942 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001943 std::vector<llvm::Constant*> ProtocolRefs;
1944
Daniel Dunbardec75f82008-08-21 21:57:41 +00001945 for (; begin != end; ++begin)
1946 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001947
1948 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001949 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001950 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001951
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001952 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001953 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001954
1955 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001956 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001957 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001958 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001959 ProtocolRefs.size() - 1);
1960 Values[2] =
1961 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1962 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001963 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001964
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001965 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001966 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001967 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001968 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001969 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001970}
1971
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001972void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1973 std::vector<llvm::Constant*> &Properties,
1974 const Decl *Container,
1975 const ObjCProtocolDecl *PROTO,
1976 const ObjCCommonTypesHelper &ObjCTypes) {
1977 std::vector<llvm::Constant*> Prop(2);
1978 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1979 E = PROTO->protocol_end(); P != E; ++P)
1980 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1981 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1982 E = PROTO->prop_end(); I != E; ++I) {
1983 const ObjCPropertyDecl *PD = *I;
1984 if (!PropertySet.insert(PD->getIdentifier()))
1985 continue;
1986 Prop[0] = GetPropertyName(PD->getIdentifier());
1987 Prop[1] = GetPropertyTypeString(PD, Container);
1988 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1989 }
1990}
1991
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001993 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 const char * const name;
1995 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001996 };
1997
1998 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001999 uint32_t entsize; // sizeof (struct _objc_property)
2000 uint32_t prop_count;
2001 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002002 };
2003*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002004llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002005 const Decl *Container,
2006 const ObjCContainerDecl *OCD,
2007 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002008 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002009 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002010 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2011 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002012 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002013 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002014 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00002015 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002016 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002017 Prop));
2018 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002019 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002020 for (ObjCInterfaceDecl::all_protocol_iterator
2021 P = OID->all_referenced_protocol_begin(),
2022 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002023 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2024 ObjCTypes);
2025 }
2026 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2027 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2028 E = CD->protocol_end(); P != E; ++P)
2029 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2030 ObjCTypes);
2031 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002032
2033 // Return null for empty list.
2034 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002035 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002036
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002037 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002038 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002039 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002040 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2041 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002043 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002044 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002045 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002046
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002047 llvm::GlobalVariable *GV =
2048 CreateMetadataVar(Name, Init,
2049 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002050 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002051 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002052 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002053 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002054}
2055
2056/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002057 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002058 int count;
2059 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002060 };
2061*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002062llvm::Constant *
2063CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2064 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002065 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002066 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2067 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002068 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002069 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002070 Desc);
2071}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002072
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002073llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002074 const char *Section,
2075 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002076 // Return null for empty list.
2077 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002078 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002079
2080 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002081 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002082 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002083 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002084 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002085 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002086
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002087 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002088 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002089 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002090}
2091
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002092/*
2093 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002094 char *category_name;
2095 char *class_name;
2096 struct _objc_method_list *instance_methods;
2097 struct _objc_method_list *class_methods;
2098 struct _objc_protocol_list *protocols;
2099 uint32_t size; // <rdar://4585769>
2100 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002101 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002102*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002103void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002104 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002105
Mike Stump18bb9282009-05-16 07:57:57 +00002106 // FIXME: This is poor design, the OCD should have a pointer to the category
2107 // decl. Additionally, note that Category can be null for the @implementation
2108 // w/o an @interface case. Sema should just create one for us as it does for
2109 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002110 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002111 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002112 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002113
2114 llvm::SmallString<256> ExtName;
2115 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2116 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002117
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002118 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002119 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002120 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002121 // Instance methods should always be defined.
2122 InstanceMethods.push_back(GetMethodConstant(*i));
2123 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002124 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002125 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002126 // Class methods should always be defined.
2127 ClassMethods.push_back(GetMethodConstant(*i));
2128 }
2129
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002130 std::vector<llvm::Constant*> Values(7);
2131 Values[0] = GetClassName(OCD->getIdentifier());
2132 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002133 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002134 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002135 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002136 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002137 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002138 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002139 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002140 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002141 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002142 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002143 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002144 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002145 Category->protocol_begin(),
2146 Category->protocol_end());
2147 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002148 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002149 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002150 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002151
2152 // If there is no category @interface then there can be no properties.
2153 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002154 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002155 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002156 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002157 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002158 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002159
Owen Anderson0e0189d2009-07-27 22:29:56 +00002160 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002161 Values);
2162
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002163 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002164 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002165 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002166 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002167 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002168 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002169}
2170
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002171// FIXME: Get from somewhere?
2172enum ClassFlags {
2173 eClassFlags_Factory = 0x00001,
2174 eClassFlags_Meta = 0x00002,
2175 // <rdr://5142207>
2176 eClassFlags_HasCXXStructors = 0x02000,
2177 eClassFlags_Hidden = 0x20000,
2178 eClassFlags_ABI2_Hidden = 0x00010,
2179 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2180};
2181
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002182/*
2183 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002184 Class isa;
2185 Class super_class;
2186 const char *name;
2187 long version;
2188 long info;
2189 long instance_size;
2190 struct _objc_ivar_list *ivars;
2191 struct _objc_method_list *methods;
2192 struct _objc_cache *cache;
2193 struct _objc_protocol_list *protocols;
2194 // Objective-C 1.0 extensions (<rdr://4585769>)
2195 const char *ivar_layout;
2196 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002197 };
2198
2199 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002200*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002201void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002202 DefinedSymbols.insert(ID->getIdentifier());
2203
Chris Lattner86d7d912008-11-24 03:54:41 +00002204 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002205 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002206 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002207 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002208 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002209 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002210 Interface->all_referenced_protocol_begin(),
2211 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002212 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002213 if (ID->getNumIvarInitializers())
2214 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002215 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002216 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002217
2218 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002219 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002220 Flags |= eClassFlags_Hidden;
2221
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002222 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002223 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002224 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002225 // Instance methods should always be defined.
2226 InstanceMethods.push_back(GetMethodConstant(*i));
2227 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002228 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002229 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002230 // Class methods should always be defined.
2231 ClassMethods.push_back(GetMethodConstant(*i));
2232 }
2233
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002234 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002235 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002236 ObjCPropertyImplDecl *PID = *i;
2237
2238 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2239 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2240
2241 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2242 if (llvm::Constant *C = GetMethodConstant(MD))
2243 InstanceMethods.push_back(C);
2244 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2245 if (llvm::Constant *C = GetMethodConstant(MD))
2246 InstanceMethods.push_back(C);
2247 }
2248 }
2249
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002251 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002252 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002253 // Record a reference to the super class.
2254 LazySymbols.insert(Super->getIdentifier());
2255
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002257 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002258 ObjCTypes.ClassPtrTy);
2259 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002260 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002261 }
2262 Values[ 2] = GetClassName(ID->getIdentifier());
2263 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002264 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2265 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2266 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002267 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002268 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002269 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002270 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002271 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002272 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002273 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002274 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002275 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002276 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002277 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002278 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002279 std::string Name("\01L_OBJC_CLASS_");
2280 Name += ClassName;
2281 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2282 // Check for a forward reference.
2283 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2284 if (GV) {
2285 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2286 "Forward metaclass reference has incorrect type.");
2287 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2288 GV->setInitializer(Init);
2289 GV->setSection(Section);
2290 GV->setAlignment(4);
2291 CGM.AddUsedGlobal(GV);
2292 }
2293 else
2294 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002295 DefinedClasses.push_back(GV);
2296}
2297
2298llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2299 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002300 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002301 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002302 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002303
John McCall457a04e2010-10-22 21:05:15 +00002304 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002305 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002306
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002307 std::vector<llvm::Constant*> Values(12);
2308 // The isa for the metaclass is the root of the hierarchy.
2309 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2310 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2311 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002312 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002313 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002315 // The super class for the metaclass is emitted as the name of the
2316 // super class. The runtime fixes this up to point to the
2317 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002318 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002319 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002320 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002321 ObjCTypes.ClassPtrTy);
2322 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002323 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002324 }
2325 Values[ 2] = GetClassName(ID->getIdentifier());
2326 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002327 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2328 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2329 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002330 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002331 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002332 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002333 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002334 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002335 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002336 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002337 Values[ 9] = Protocols;
2338 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002339 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002341 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002342 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002343 Values);
2344
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002345 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002346 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002347
2348 // Check for a forward reference.
2349 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2350 if (GV) {
2351 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2352 "Forward metaclass reference has incorrect type.");
2353 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2354 GV->setInitializer(Init);
2355 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002356 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002357 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002358 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002359 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002360 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002361 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002362 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002363
2364 return GV;
2365}
2366
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002367llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002368 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002369
Mike Stump18bb9282009-05-16 07:57:57 +00002370 // FIXME: Should we look these up somewhere other than the module. Its a bit
2371 // silly since we only generate these while processing an implementation, so
2372 // exactly one pointer would work if know when we entered/exitted an
2373 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002374
2375 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002376 // Previously, metaclass with internal linkage may have been defined.
2377 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2379 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002380 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2381 "Forward metaclass reference has incorrect type.");
2382 return GV;
2383 } else {
2384 // Generate as an external reference to keep a consistent
2385 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002386 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002387 llvm::GlobalValue::ExternalLinkage,
2388 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002389 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002390 }
2391}
2392
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002393llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2394 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2395
2396 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2397 true)) {
2398 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2399 "Forward class metadata reference has incorrect type.");
2400 return GV;
2401 } else {
2402 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2403 llvm::GlobalValue::ExternalLinkage,
2404 0,
2405 Name);
2406 }
2407}
2408
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002409/*
2410 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002411 uint32_t size;
2412 const char *weak_ivar_layout;
2413 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002414 };
2415*/
2416llvm::Constant *
2417CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002418 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002419 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420
2421 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002422 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002423 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002424 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002425 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002426
2427 // Return null if no extension bits are used.
2428 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002429 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002430
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002431 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002432 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002433 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002434 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002435 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002436}
2437
2438/*
2439 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002440 char *ivar_name;
2441 char *ivar_type;
2442 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443 };
2444
2445 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002446 int ivar_count;
2447 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002448 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002449*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002450llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002451 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002452 std::vector<llvm::Constant*> Ivars, Ivar(3);
2453
2454 // When emitting the root class GCC emits ivar entries for the
2455 // actual class structure. It is not clear if we need to follow this
2456 // behavior; for now lets try and get away with not doing it. If so,
2457 // the cleanest solution would be to make up an ObjCInterfaceDecl
2458 // for the class.
2459 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002460 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002461
2462 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002463 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002464
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002465 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002466 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002467
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002468 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2469 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002470 // Ignore unnamed bit-fields.
2471 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002472 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002473 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2474 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002475 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002476 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002477 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002478 }
2479
2480 // Return null for empty list.
2481 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002482 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002483
2484 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002485 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002486 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002487 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002488 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002489 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002491 llvm::GlobalVariable *GV;
2492 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002493 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002494 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002495 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002496 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002497 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002498 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002499 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002500 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002501}
2502
2503/*
2504 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002505 SEL method_name;
2506 char *method_types;
2507 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002508 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002509
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002510 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002511 struct objc_method_list *obsolete;
2512 int count;
2513 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002514 };
2515*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002516
2517/// GetMethodConstant - Return a struct objc_method constant for the
2518/// given method if it has been defined. The result is null if the
2519/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002520llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002521 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002522 if (!Fn)
2523 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002524
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002525 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002526 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002527 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002528 ObjCTypes.SelectorPtrTy);
2529 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002530 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002531 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002532}
2533
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002534llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002535 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002536 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002537 // Return null for empty list.
2538 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002539 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002540
2541 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002542 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002543 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002544 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002545 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002546 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002547 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002548
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002549 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002550 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002551 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002552}
2553
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002554llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002555 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002556 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002557 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002558
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002559 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002560 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002561 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002562 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002563 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002564 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002565 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002566 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002567 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002568
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002569 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002570}
2571
Daniel Dunbar30c65362009-03-09 20:09:19 +00002572llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002573CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002574 llvm::Constant *Init,
2575 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002576 unsigned Align,
2577 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002578 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002579 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002580 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002581 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002582 if (Section)
2583 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002584 if (Align)
2585 GV->setAlignment(Align);
2586 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002587 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002588 return GV;
2589}
2590
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002591llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002592 // Abuse this interface function as a place to finalize.
2593 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002594 return NULL;
2595}
2596
Chris Lattnerd4808922009-03-22 21:03:39 +00002597llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002598 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002599}
2600
Chris Lattnerd4808922009-03-22 21:03:39 +00002601llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002602 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002603}
2604
David Chisnall168b80f2010-12-26 22:13:16 +00002605llvm::Constant *CGObjCMac::GetGetStructFunction() {
2606 return ObjCTypes.getCopyStructFn();
2607}
2608llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002609 return ObjCTypes.getCopyStructFn();
2610}
2611
Chris Lattnerd4808922009-03-22 21:03:39 +00002612llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002613 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002614}
2615
John McCallbd309292010-07-06 01:34:17 +00002616void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2617 return EmitTryOrSynchronizedStmt(CGF, S);
2618}
2619
2620void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2621 const ObjCAtSynchronizedStmt &S) {
2622 return EmitTryOrSynchronizedStmt(CGF, S);
2623}
2624
John McCall65bea082010-07-21 06:59:36 +00002625namespace {
John McCallcda666c2010-07-21 07:22:38 +00002626 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002627 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002628 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002629 llvm::Value *CallTryExitVar;
2630 llvm::Value *ExceptionData;
2631 ObjCTypesHelper &ObjCTypes;
2632 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002633 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002634 llvm::Value *CallTryExitVar,
2635 llvm::Value *ExceptionData,
2636 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002637 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002638 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2639
2640 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2641 // Check whether we need to call objc_exception_try_exit.
2642 // In optimized code, this branch will always be folded.
2643 llvm::BasicBlock *FinallyCallExit =
2644 CGF.createBasicBlock("finally.call_exit");
2645 llvm::BasicBlock *FinallyNoCallExit =
2646 CGF.createBasicBlock("finally.no_call_exit");
2647 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2648 FinallyCallExit, FinallyNoCallExit);
2649
2650 CGF.EmitBlock(FinallyCallExit);
2651 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2652 ->setDoesNotThrow();
2653
2654 CGF.EmitBlock(FinallyNoCallExit);
2655
2656 if (isa<ObjCAtTryStmt>(S)) {
2657 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002658 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2659 // Save the current cleanup destination in case there's
2660 // control flow inside the finally statement.
2661 llvm::Value *CurCleanupDest =
2662 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2663
John McCall65bea082010-07-21 06:59:36 +00002664 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2665
John McCallcebe0ca2010-08-11 00:16:14 +00002666 if (CGF.HaveInsertPoint()) {
2667 CGF.Builder.CreateStore(CurCleanupDest,
2668 CGF.getNormalCleanupDestSlot());
2669 } else {
2670 // Currently, the end of the cleanup must always exist.
2671 CGF.EnsureInsertPoint();
2672 }
2673 }
John McCall65bea082010-07-21 06:59:36 +00002674 } else {
2675 // Emit objc_sync_exit(expr); as finally's sole statement for
2676 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002677 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002678 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2679 ->setDoesNotThrow();
2680 }
2681 }
2682 };
John McCall42227ed2010-07-31 23:20:56 +00002683
2684 class FragileHazards {
2685 CodeGenFunction &CGF;
2686 llvm::SmallVector<llvm::Value*, 20> Locals;
2687 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2688
2689 llvm::InlineAsm *ReadHazard;
2690 llvm::InlineAsm *WriteHazard;
2691
2692 llvm::FunctionType *GetAsmFnType();
2693
2694 void collectLocals();
2695 void emitReadHazard(CGBuilderTy &Builder);
2696
2697 public:
2698 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002699
John McCall42227ed2010-07-31 23:20:56 +00002700 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002701 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002702 };
2703}
2704
2705/// Create the fragile-ABI read and write hazards based on the current
2706/// state of the function, which is presumed to be immediately prior
2707/// to a @try block. These hazards are used to maintain correct
2708/// semantics in the face of optimization and the fragile ABI's
2709/// cavalier use of setjmp/longjmp.
2710FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2711 collectLocals();
2712
2713 if (Locals.empty()) return;
2714
2715 // Collect all the blocks in the function.
2716 for (llvm::Function::iterator
2717 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2718 BlocksBeforeTry.insert(&*I);
2719
2720 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2721
2722 // Create a read hazard for the allocas. This inhibits dead-store
2723 // optimizations and forces the values to memory. This hazard is
2724 // inserted before any 'throwing' calls in the protected scope to
2725 // reflect the possibility that the variables might be read from the
2726 // catch block if the call throws.
2727 {
2728 std::string Constraint;
2729 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2730 if (I) Constraint += ',';
2731 Constraint += "*m";
2732 }
2733
2734 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2735 }
2736
2737 // Create a write hazard for the allocas. This inhibits folding
2738 // loads across the hazard. This hazard is inserted at the
2739 // beginning of the catch path to reflect the possibility that the
2740 // variables might have been written within the protected scope.
2741 {
2742 std::string Constraint;
2743 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2744 if (I) Constraint += ',';
2745 Constraint += "=*m";
2746 }
2747
2748 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2749 }
2750}
2751
2752/// Emit a write hazard at the current location.
2753void FragileHazards::emitWriteHazard() {
2754 if (Locals.empty()) return;
2755
2756 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2757 ->setDoesNotThrow();
2758}
2759
John McCall42227ed2010-07-31 23:20:56 +00002760void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2761 assert(!Locals.empty());
2762 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2763 ->setDoesNotThrow();
2764}
2765
2766/// Emit read hazards in all the protected blocks, i.e. all the blocks
2767/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002768void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002769 if (Locals.empty()) return;
2770
2771 CGBuilderTy Builder(CGF.getLLVMContext());
2772
2773 // Iterate through all blocks, skipping those prior to the try.
2774 for (llvm::Function::iterator
2775 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2776 llvm::BasicBlock &BB = *FI;
2777 if (BlocksBeforeTry.count(&BB)) continue;
2778
2779 // Walk through all the calls in the block.
2780 for (llvm::BasicBlock::iterator
2781 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2782 llvm::Instruction &I = *BI;
2783
2784 // Ignore instructions that aren't non-intrinsic calls.
2785 // These are the only calls that can possibly call longjmp.
2786 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2787 if (isa<llvm::IntrinsicInst>(I))
2788 continue;
2789
2790 // Ignore call sites marked nounwind. This may be questionable,
2791 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2792 llvm::CallSite CS(&I);
2793 if (CS.doesNotThrow()) continue;
2794
John McCall2dd7d442010-08-04 05:59:32 +00002795 // Insert a read hazard before the call. This will ensure that
2796 // any writes to the locals are performed before making the
2797 // call. If the call throws, then this is sufficient to
2798 // guarantee correctness as long as it doesn't also write to any
2799 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002800 Builder.SetInsertPoint(&BB, BI);
2801 emitReadHazard(Builder);
2802 }
2803 }
2804}
2805
2806static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2807 if (V) S.insert(V);
2808}
2809
2810void FragileHazards::collectLocals() {
2811 // Compute a set of allocas to ignore.
2812 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2813 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2814 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2815 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2816
2817 // Collect all the allocas currently in the function. This is
2818 // probably way too aggressive.
2819 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2820 for (llvm::BasicBlock::iterator
2821 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2822 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2823 Locals.push_back(&*I);
2824}
2825
2826llvm::FunctionType *FragileHazards::GetAsmFnType() {
2827 std::vector<const llvm::Type *> Tys(Locals.size());
2828 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2829 Tys[I] = Locals[I]->getType();
2830 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002831}
2832
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002833/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002834
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002835 Objective-C setjmp-longjmp (sjlj) Exception Handling
2836 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002837
John McCallbd309292010-07-06 01:34:17 +00002838 A catch buffer is a setjmp buffer plus:
2839 - a pointer to the exception that was caught
2840 - a pointer to the previous exception data buffer
2841 - two pointers of reserved storage
2842 Therefore catch buffers form a stack, with a pointer to the top
2843 of the stack kept in thread-local storage.
2844
2845 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2846 objc_exception_try_exit pops the given catch buffer, which is
2847 required to be the top of the EH stack.
2848 objc_exception_throw pops the top of the EH stack, writes the
2849 thrown exception into the appropriate field, and longjmps
2850 to the setjmp buffer. It crashes the process (with a printf
2851 and an abort()) if there are no catch buffers on the stack.
2852 objc_exception_extract just reads the exception pointer out of the
2853 catch buffer.
2854
2855 There's no reason an implementation couldn't use a light-weight
2856 setjmp here --- something like __builtin_setjmp, but API-compatible
2857 with the heavyweight setjmp. This will be more important if we ever
2858 want to implement correct ObjC/C++ exception interactions for the
2859 fragile ABI.
2860
2861 Note that for this use of setjmp/longjmp to be correct, we may need
2862 to mark some local variables volatile: if a non-volatile local
2863 variable is modified between the setjmp and the longjmp, it has
2864 indeterminate value. For the purposes of LLVM IR, it may be
2865 sufficient to make loads and stores within the @try (to variables
2866 declared outside the @try) volatile. This is necessary for
2867 optimized correctness, but is not currently being done; this is
2868 being tracked as rdar://problem/8160285
2869
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002870 The basic framework for a @try-catch-finally is as follows:
2871 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002872 objc_exception_data d;
2873 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002874 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002875
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002876 objc_exception_try_enter(&d);
2877 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002878 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002879 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002880 // exception path
2881 id _caught = objc_exception_extract(&d);
2882
2883 // enter new try scope for handlers
2884 if (!setjmp(d.jmp_buf)) {
2885 ... match exception and execute catch blocks ...
2886
2887 // fell off end, rethrow.
2888 _rethrow = _caught;
2889 ... jump-through-finally to finally_rethrow ...
2890 } else {
2891 // exception in catch block
2892 _rethrow = objc_exception_extract(&d);
2893 _call_try_exit = false;
2894 ... jump-through-finally to finally_rethrow ...
2895 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002896 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002897 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002898
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002899 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002900 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002901 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002902
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002903 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002904 ... dispatch to finally destination ...
2905
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002906 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002907 objc_exception_throw(_rethrow);
2908
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002909 finally_end:
2910 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002911
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002912 This framework differs slightly from the one gcc uses, in that gcc
2913 uses _rethrow to determine if objc_exception_try_exit should be called
2914 and if the object should be rethrown. This breaks in the face of
2915 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002916
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002917 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002918
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002919 - If there are no catch blocks, then we avoid emitting the second
2920 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002921
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002922 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2923 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002924
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002925 - FIXME: If there is no @finally block we can do a few more
2926 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002927
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002928 Rethrows and Jumps-Through-Finally
2929 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002930
John McCallbd309292010-07-06 01:34:17 +00002931 '@throw;' is supported by pushing the currently-caught exception
2932 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002933
John McCallbd309292010-07-06 01:34:17 +00002934 Branches through the @finally block are handled with an ordinary
2935 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2936 exceptions are not compatible with C++ exceptions, and this is
2937 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002938
John McCallbd309292010-07-06 01:34:17 +00002939 @synchronized(expr) { stmt; } is emitted as if it were:
2940 id synch_value = expr;
2941 objc_sync_enter(synch_value);
2942 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002943*/
2944
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002945void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2946 const Stmt &S) {
2947 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002948
2949 // A destination for the fall-through edges of the catch handlers to
2950 // jump to.
2951 CodeGenFunction::JumpDest FinallyEnd =
2952 CGF.getJumpDestInCurrentScope("finally.end");
2953
2954 // A destination for the rethrow edge of the catch handlers to jump
2955 // to.
2956 CodeGenFunction::JumpDest FinallyRethrow =
2957 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002958
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002959 // For @synchronized, call objc_sync_enter(sync.expr). The
2960 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002961 // @synchronized. We can't avoid a temp here because we need the
2962 // value to be preserved. If the backend ever does liveness
2963 // correctly after setjmp, this will be unnecessary.
2964 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002965 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002966 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002967 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2968 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002969 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2970 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002971
2972 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2973 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002974 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002975
John McCall2dd7d442010-08-04 05:59:32 +00002976 // Allocate memory for the setjmp buffer. This needs to be kept
2977 // live throughout the try and catch blocks.
2978 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2979 "exceptiondata.ptr");
2980
John McCall42227ed2010-07-31 23:20:56 +00002981 // Create the fragile hazards. Note that this will not capture any
2982 // of the allocas required for exception processing, but will
2983 // capture the current basic block (which extends all the way to the
2984 // setjmp call) as "before the @try".
2985 FragileHazards Hazards(CGF);
2986
John McCallbd309292010-07-06 01:34:17 +00002987 // Create a flag indicating whether the cleanup needs to call
2988 // objc_exception_try_exit. This is true except when
2989 // - no catches match and we're branching through the cleanup
2990 // just to rethrow the exception, or
2991 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002992 // The setjmp-safety rule here is that we should always store to this
2993 // variable in a place that dominates the branch through the cleanup
2994 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002995 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002996 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002997
John McCall9916e3f2010-10-04 23:42:51 +00002998 // A slot containing the exception to rethrow. Only needed when we
2999 // have both a @catch and a @finally.
3000 llvm::Value *PropagatingExnVar = 0;
3001
John McCallbd309292010-07-06 01:34:17 +00003002 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003003 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003004 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003005 CallTryExitVar,
3006 ExceptionData,
3007 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003008
3009 // Enter a try block:
3010 // - Call objc_exception_try_enter to push ExceptionData on top of
3011 // the EH stack.
3012 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3013 ->setDoesNotThrow();
3014
3015 // - Call setjmp on the exception data buffer.
3016 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3017 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3018 llvm::Value *SetJmpBuffer =
3019 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3020 llvm::CallInst *SetJmpResult =
3021 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3022 SetJmpResult->setDoesNotThrow();
3023
3024 // If setjmp returned 0, enter the protected block; otherwise,
3025 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003026 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3027 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003028 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003029 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3030 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003031
John McCallbd309292010-07-06 01:34:17 +00003032 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003033 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003034 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003035 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003036 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003037
3038 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003039
John McCallbd309292010-07-06 01:34:17 +00003040 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003041 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003042
John McCall42227ed2010-07-31 23:20:56 +00003043 // Don't optimize loads of the in-scope locals across this point.
3044 Hazards.emitWriteHazard();
3045
John McCallbd309292010-07-06 01:34:17 +00003046 // For a @synchronized (or a @try with no catches), just branch
3047 // through the cleanup to the rethrow block.
3048 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3049 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003050 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003051 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003052
3053 // Otherwise, we have to match against the caught exceptions.
3054 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003055 // Retrieve the exception object. We may emit multiple blocks but
3056 // nothing can cross this so the value is already in SSA form.
3057 llvm::CallInst *Caught =
3058 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3059 ExceptionData, "caught");
3060 Caught->setDoesNotThrow();
3061
John McCallbd309292010-07-06 01:34:17 +00003062 // Push the exception to rethrow onto the EH value stack for the
3063 // benefit of any @throws in the handlers.
3064 CGF.ObjCEHValueStack.push_back(Caught);
3065
Douglas Gregor96c79492010-04-23 22:50:49 +00003066 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003067
John McCall2dd7d442010-08-04 05:59:32 +00003068 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003069
John McCall2dd7d442010-08-04 05:59:32 +00003070 llvm::BasicBlock *CatchBlock = 0;
3071 llvm::BasicBlock *CatchHandler = 0;
3072 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003073 // Save the currently-propagating exception before
3074 // objc_exception_try_enter clears the exception slot.
3075 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3076 "propagating_exception");
3077 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3078
John McCall2dd7d442010-08-04 05:59:32 +00003079 // Enter a new exception try block (in case a @catch block
3080 // throws an exception).
3081 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3082 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003083
John McCall2dd7d442010-08-04 05:59:32 +00003084 llvm::CallInst *SetJmpResult =
3085 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3086 "setjmp.result");
3087 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003088
John McCall2dd7d442010-08-04 05:59:32 +00003089 llvm::Value *Threw =
3090 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3091
3092 CatchBlock = CGF.createBasicBlock("catch");
3093 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3094 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3095
3096 CGF.EmitBlock(CatchBlock);
3097 }
3098
3099 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003101 // Handle catch list. As a special case we check if everything is
3102 // matched and avoid generating code for falling off the end if
3103 // so.
3104 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003105 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3106 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003107
Douglas Gregor46a572b2010-04-26 16:46:50 +00003108 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003109 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003110
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003111 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003112 if (!CatchParam) {
3113 AllMatched = true;
3114 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003115 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003116
John McCallbd309292010-07-06 01:34:17 +00003117 // catch(id e) always matches under this ABI, since only
3118 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003119 // FIXME: For the time being we also match id<X>; this should
3120 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003121 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003122 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003123 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003124
John McCallbd309292010-07-06 01:34:17 +00003125 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003126 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003127 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3128
Anders Carlsson9396a892008-09-11 09:15:33 +00003129 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003130 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003131 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003134 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003136
Anders Carlsson9396a892008-09-11 09:15:33 +00003137 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003138
3139 // The scope of the catch variable ends right here.
3140 CatchVarCleanups.ForceCleanup();
3141
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003142 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003143 break;
3144 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003145
Steve Naroff7cae42b2009-07-10 23:34:53 +00003146 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003147 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003148
3149 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003150 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3151 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003152
3153 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003154 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003155
John McCallbd309292010-07-06 01:34:17 +00003156 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003157 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3158 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003159 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003160
John McCallbd309292010-07-06 01:34:17 +00003161 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3162 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003163
3164 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003165 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003166
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003167 // Emit the @catch block.
3168 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003169
3170 // Collect any cleanups for the catch variable. The scope lasts until
3171 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003172 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003173
John McCall1c9c3fd2010-10-15 04:57:14 +00003174 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003175 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003176
John McCallbd309292010-07-06 01:34:17 +00003177 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003178 llvm::Value *Tmp =
3179 CGF.Builder.CreateBitCast(Caught,
3180 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003181 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003182 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003183
Anders Carlsson9396a892008-09-11 09:15:33 +00003184 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003185
3186 // We're done with the catch variable.
3187 CatchVarCleanups.ForceCleanup();
3188
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003189 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003190
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003191 CGF.EmitBlock(NextCatchBlock);
3192 }
3193
John McCallbd309292010-07-06 01:34:17 +00003194 CGF.ObjCEHValueStack.pop_back();
3195
John McCall2dd7d442010-08-04 05:59:32 +00003196 // If nothing wanted anything to do with the caught exception,
3197 // kill the extract call.
3198 if (Caught->use_empty())
3199 Caught->eraseFromParent();
3200
3201 if (!AllMatched)
3202 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3203
3204 if (HasFinally) {
3205 // Emit the exception handler for the @catch blocks.
3206 CGF.EmitBlock(CatchHandler);
3207
3208 // In theory we might now need a write hazard, but actually it's
3209 // unnecessary because there's no local-accessing code between
3210 // the try's write hazard and here.
3211 //Hazards.emitWriteHazard();
3212
John McCall9916e3f2010-10-04 23:42:51 +00003213 // Extract the new exception and save it to the
3214 // propagating-exception slot.
3215 assert(PropagatingExnVar);
3216 llvm::CallInst *NewCaught =
3217 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3218 ExceptionData, "caught");
3219 NewCaught->setDoesNotThrow();
3220 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3221
John McCall2dd7d442010-08-04 05:59:32 +00003222 // Don't pop the catch handler; the throw already did.
3223 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003224 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003225 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003226 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003227
John McCall42227ed2010-07-31 23:20:56 +00003228 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003229 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003230
John McCallbd309292010-07-06 01:34:17 +00003231 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003232 CGF.Builder.restoreIP(TryFallthroughIP);
3233 if (CGF.HaveInsertPoint())
3234 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003235 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003236 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003237
John McCallbd309292010-07-06 01:34:17 +00003238 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003239 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003240 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003241 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003242 // If we have a propagating-exception variable, check it.
3243 llvm::Value *PropagatingExn;
3244 if (PropagatingExnVar) {
3245 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003246
John McCall9916e3f2010-10-04 23:42:51 +00003247 // Otherwise, just look in the buffer for the exception to throw.
3248 } else {
3249 llvm::CallInst *Caught =
3250 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3251 ExceptionData);
3252 Caught->setDoesNotThrow();
3253 PropagatingExn = Caught;
3254 }
3255
3256 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003257 ->setDoesNotThrow();
3258 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003259 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003260
John McCall42227ed2010-07-31 23:20:56 +00003261 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003262}
3263
3264void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003265 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003266 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003267
Anders Carlssone005aa12008-09-09 16:16:55 +00003268 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3269 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003270 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003271 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3272 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003273 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003274 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003275 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003276 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003277
John McCallbd309292010-07-06 01:34:17 +00003278 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3279 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003280 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003281
3282 // Clear the insertion point to indicate we are in unreachable code.
3283 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003284}
3285
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003286/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003287/// object: objc_read_weak (id *src)
3288///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003289llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003290 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003291 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003292 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3293 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3294 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003295 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003296 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003297 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003298 return read_weak;
3299}
3300
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003301/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3302/// objc_assign_weak (id src, id *dst)
3303///
3304void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003305 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003306 const llvm::Type * SrcTy = src->getType();
3307 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003308 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003309 assert(Size <= 8 && "does not support size > 8");
3310 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003311 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003312 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3313 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003314 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3315 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003316 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003317 src, dst, "weakassign");
3318 return;
3319}
3320
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003321/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3322/// objc_assign_global (id src, id *dst)
3323///
3324void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003325 llvm::Value *src, llvm::Value *dst,
3326 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003327 const llvm::Type * SrcTy = src->getType();
3328 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003329 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003330 assert(Size <= 8 && "does not support size > 8");
3331 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003332 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003333 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3334 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003335 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3336 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003337 if (!threadlocal)
3338 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3339 src, dst, "globalassign");
3340 else
3341 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3342 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003343 return;
3344}
3345
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003346/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003347/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003348///
3349void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003350 llvm::Value *src, llvm::Value *dst,
3351 llvm::Value *ivarOffset) {
3352 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003353 const llvm::Type * SrcTy = src->getType();
3354 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003355 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003356 assert(Size <= 8 && "does not support size > 8");
3357 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003358 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003359 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3360 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003361 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3362 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003363 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3364 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003365 return;
3366}
3367
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003368/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3369/// objc_assign_strongCast (id src, id *dst)
3370///
3371void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003372 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003373 const llvm::Type * SrcTy = src->getType();
3374 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003375 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003376 assert(Size <= 8 && "does not support size > 8");
3377 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003378 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003379 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3380 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003381 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3382 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003383 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003384 src, dst, "weakassign");
3385 return;
3386}
3387
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003388void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 llvm::Value *DestPtr,
3390 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003391 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003392 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3393 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003394 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003395 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003396 return;
3397}
3398
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003399/// EmitObjCValueForIvar - Code Gen for ivar reference.
3400///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003401LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3402 QualType ObjectTy,
3403 llvm::Value *BaseValue,
3404 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003405 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003406 const ObjCInterfaceDecl *ID =
3407 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003408 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3409 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003410}
3411
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003412llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003413 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003414 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003415 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003416 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003417 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3418 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003419}
3420
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003421/* *** Private Interface *** */
3422
3423/// EmitImageInfo - Emit the image info marker used to encode some module
3424/// level information.
3425///
3426/// See: <rdr://4810609&4810587&4810587>
3427/// struct IMAGE_INFO {
3428/// unsigned version;
3429/// unsigned flags;
3430/// };
3431enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003432 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003433 eImageInfo_GarbageCollected = (1 << 1),
3434 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003435 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3436
Daniel Dunbar5e639272010-04-25 20:39:01 +00003437 // A flag indicating that the module has no instances of a @synthesize of a
3438 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003439 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003440};
3441
Daniel Dunbar5e639272010-04-25 20:39:01 +00003442void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003443 unsigned version = 0; // Version is unused?
3444 unsigned flags = 0;
3445
3446 // FIXME: Fix and continue?
3447 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3448 flags |= eImageInfo_GarbageCollected;
3449 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3450 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003451
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003452 // We never allow @synthesize of a superclass property.
3453 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003454
Chris Lattner5e016ae2010-06-27 07:15:29 +00003455 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3456
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003457 // Emitted as int[2];
3458 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003459 llvm::ConstantInt::get(Int32Ty, version),
3460 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003461 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003462 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003463
3464 const char *Section;
3465 if (ObjCABI == 1)
3466 Section = "__OBJC, __image_info,regular";
3467 else
3468 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003469 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003470 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003471 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003472 Section,
3473 0,
3474 true);
3475 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003476}
3477
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003478
3479// struct objc_module {
3480// unsigned long version;
3481// unsigned long size;
3482// const char *name;
3483// Symtab symtab;
3484// };
3485
3486// FIXME: Get from somewhere
3487static const int ModuleVersion = 7;
3488
3489void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003490 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003491
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003492 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003493 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3494 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003495 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003496 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003497 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003498 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003499 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003500 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003501 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003502}
3503
3504llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003505 unsigned NumClasses = DefinedClasses.size();
3506 unsigned NumCategories = DefinedCategories.size();
3507
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003508 // Return null if no symbols were defined.
3509 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003510 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003511
3512 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003513 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003514 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003515 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3516 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003517
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003518 // The runtime expects exactly the list of defined classes followed
3519 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003520 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003521 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003522 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003523 ObjCTypes.Int8PtrTy);
3524 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003525 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003526 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003527 ObjCTypes.Int8PtrTy);
3528
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003529 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003530 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003531 NumClasses + NumCategories),
3532 Symbols);
3533
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003534 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003535
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003536 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003537 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3538 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003539 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003540 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003541}
3542
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003543llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003544 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003545 LazySymbols.insert(ID->getIdentifier());
3546
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003547 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003548
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003549 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003550 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003551 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003552 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003553 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003554 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3555 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003556 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003557 }
3558
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003559 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003560}
3561
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003562llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3563 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003564 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003565
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003566 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003567 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003568 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003569 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003570 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003571 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3572 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003573 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003574 }
3575
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003576 if (lvalue)
3577 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003578 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003579}
3580
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003581llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003582 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003583
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003584 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003585 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003586 llvm::ConstantArray::get(VMContext,
3587 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003588 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003589 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003590
Owen Anderson170229f2009-07-14 23:10:40 +00003591 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003592}
3593
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003594llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3595 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3596 I = MethodDefinitions.find(MD);
3597 if (I != MethodDefinitions.end())
3598 return I->second;
3599
3600 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3601 // MD isn't emitted yet because it comes from PCH.
3602 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3603 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3604 return MethodDefinitions[MD];
3605 }
3606
3607 return NULL;
3608}
3609
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003610/// GetIvarLayoutName - Returns a unique constant for the given
3611/// ivar layout bitmap.
3612llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003613 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003614 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003615}
3616
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003617void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003618 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003619 bool ForStrongLayout,
3620 bool &HasUnion) {
3621 const RecordDecl *RD = RT->getDecl();
3622 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003623 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003624 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003625 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003626 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003627
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003628 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3629 ForStrongLayout, HasUnion);
3630}
3631
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003632void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003633 const llvm::StructLayout *Layout,
3634 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003635 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003636 unsigned int BytePos, bool ForStrongLayout,
3637 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003638 bool IsUnion = (RD && RD->isUnion());
3639 uint64_t MaxUnionIvarSize = 0;
3640 uint64_t MaxSkippedUnionIvarSize = 0;
3641 FieldDecl *MaxField = 0;
3642 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003643 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003644 uint64_t MaxFieldOffset = 0;
3645 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003646 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003647
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003648 if (RecFields.empty())
3649 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003650 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3651 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3652
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003653 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003654 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003655 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003656 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003657 // Note that 'i' here is actually the field index inside RD of Field,
3658 // although this dependency is hidden.
3659 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3660 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003661 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003662 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003663
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003664 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003665 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003666 LastFieldBitfieldOrUnnamed = Field;
3667 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003668 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003669 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003670
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003671 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003672 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003673 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003674 if (FQT->isUnionType())
3675 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003676
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003677 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003678 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003679 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003680 continue;
3681 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003682
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003683 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003684 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003685 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003686 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003687 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003688 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003689 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3690 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003691 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003692 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003693 FQT = CArray->getElementType();
3694 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003695
3696 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003697 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003698 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003699 int OldIndex = IvarsInfo.size() - 1;
3700 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003701
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003702 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003703 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003704 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003705
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003706 // Replicate layout information for each array element. Note that
3707 // one element is already done.
3708 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003709 for (int FirstIndex = IvarsInfo.size() - 1,
3710 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003711 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003712 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3713 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3714 IvarsInfo[i].ivar_size));
3715 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3716 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3717 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003718 }
3719 continue;
3720 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003721 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003722 // At this point, we are done with Record/Union and array there of.
3723 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003724 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003725
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003726 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003727 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3728 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003729 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003730 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003731 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003732 MaxUnionIvarSize = UnionIvarSize;
3733 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003734 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003735 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003736 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003737 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003738 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003739 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003740 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003741 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3742 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003743 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003744 // FIXME: Why the asymmetry? We divide by word size in bits on other
3745 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003746 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003747 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003748 MaxSkippedUnionIvarSize = UnionIvarSize;
3749 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003750 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003751 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003752 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003753 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003754 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003755 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003756 }
3757 }
3758 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003759
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003760 if (LastFieldBitfieldOrUnnamed) {
3761 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3762 // Last field was a bitfield. Must update skip info.
3763 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3764 uint64_t BitFieldSize =
3765 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3766 GC_IVAR skivar;
3767 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3768 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3769 + ((BitFieldSize % ByteSizeInBits) != 0);
3770 SkipIvars.push_back(skivar);
3771 } else {
3772 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3773 // Last field was unnamed. Must update skip info.
3774 unsigned FieldSize
3775 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3776 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3777 FieldSize / ByteSizeInBits));
3778 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003779 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003780
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003781 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003782 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003783 MaxUnionIvarSize));
3784 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003785 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003786 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003787}
3788
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003789/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3790/// the computations and returning the layout bitmap (for ivar or blocks) in
3791/// the given argument BitMap string container. Routine reads
3792/// two containers, IvarsInfo and SkipIvars which are assumed to be
3793/// filled already by the caller.
3794llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003795 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003796 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003797
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003798 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003799 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003800 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003801 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003802 if (IvarsInfo[0].ivar_bytepos == 0) {
3803 WordsToSkip = 0;
3804 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003805 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003806 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3807 WordsToScan = IvarsInfo[0].ivar_size;
3808 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003809 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003810 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003811 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003812 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003813 // consecutive 'scanned' object pointers.
3814 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003815 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003816 // Skip over 'gc'able object pointer which lay over each other.
3817 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3818 continue;
3819 // Must skip over 1 or more words. We save current skip/scan values
3820 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003821 SKIP_SCAN SkScan;
3822 SkScan.skip = WordsToSkip;
3823 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003824 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003825
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003826 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003827 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3828 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003829 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003830 WordsToSkip = 0;
3831 WordsToScan = IvarsInfo[i].ivar_size;
3832 }
3833 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003834 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003835 SKIP_SCAN SkScan;
3836 SkScan.skip = WordsToSkip;
3837 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003838 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003839 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003840
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003841 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003842 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003843 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003844 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003845 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003846 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003847 IvarsInfo[LastIndex].ivar_bytepos +
3848 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003849 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003850 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003851 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003852 SKIP_SCAN SkScan;
3853 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3854 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003855 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003856 }
3857 }
3858 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3859 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003860 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003861 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003862 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3863 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3864 // 0xM0 followed by 0x0N detected.
3865 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3866 for (int j = i+1; j < SkipScan; j++)
3867 SkipScanIvars[j] = SkipScanIvars[j+1];
3868 --SkipScan;
3869 }
3870 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003871
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003872 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003873 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003874 unsigned char byte;
3875 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3876 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3877 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3878 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003879
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003880 // first skip big.
3881 for (unsigned int ix = 0; ix < skip_big; ix++)
3882 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003883
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003884 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003885 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003886 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003887 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003888 byte |= 0xf;
3889 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003890 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003891 byte |= scan_small;
3892 scan_small = 0;
3893 }
3894 BitMap += byte;
3895 }
3896 // next scan big
3897 for (unsigned int ix = 0; ix < scan_big; ix++)
3898 BitMap += (unsigned char)(0x0f);
3899 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003900 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003901 byte = scan_small;
3902 BitMap += byte;
3903 }
3904 }
3905 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003906 unsigned char zero = 0;
3907 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003908
3909 llvm::GlobalVariable * Entry =
3910 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3911 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3912 "__TEXT,__cstring,cstring_literals",
3913 1, true);
3914 return getConstantGEP(VMContext, Entry, 0, 0);
3915}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003916
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003917/// BuildIvarLayout - Builds ivar layout bitmap for the class
3918/// implementation for the __strong or __weak case.
3919/// The layout map displays which words in ivar list must be skipped
3920/// and which must be scanned by GC (see below). String is built of bytes.
3921/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3922/// of words to skip and right nibble is count of words to scan. So, each
3923/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3924/// represented by a 0x00 byte which also ends the string.
3925/// 1. when ForStrongLayout is true, following ivars are scanned:
3926/// - id, Class
3927/// - object *
3928/// - __strong anything
3929///
3930/// 2. When ForStrongLayout is false, following ivars are scanned:
3931/// - __weak anything
3932///
3933llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3934 const ObjCImplementationDecl *OMD,
3935 bool ForStrongLayout) {
3936 bool hasUnion = false;
3937
3938 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3939 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3940 return llvm::Constant::getNullValue(PtrTy);
3941
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003942 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003943 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003944 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003945
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003946 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003947 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3948 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3949
3950 if (RecFields.empty())
3951 return llvm::Constant::getNullValue(PtrTy);
3952
3953 SkipIvars.clear();
3954 IvarsInfo.clear();
3955
3956 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3957 if (IvarsInfo.empty())
3958 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003959 // Sort on byte position in case we encounterred a union nested in
3960 // the ivar list.
3961 if (hasUnion && !IvarsInfo.empty())
3962 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3963 if (hasUnion && !SkipIvars.empty())
3964 std::sort(SkipIvars.begin(), SkipIvars.end());
3965
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003966 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003967 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003968
3969 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003970 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003971 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003972 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003973 const unsigned char *s = (unsigned char*)BitMap.c_str();
3974 for (unsigned i = 0; i < BitMap.size(); i++)
3975 if (!(s[i] & 0xf0))
3976 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3977 else
3978 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3979 printf("\n");
3980 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003981 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003982}
3983
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003984llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003985 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3986
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003987 // FIXME: Avoid std::string copying.
3988 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003989 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003990 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003991 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003992 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003993
Owen Anderson170229f2009-07-14 23:10:40 +00003994 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003995}
3996
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003997// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003998llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003999 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4000}
4001
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004002llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004003 std::string TypeStr;
4004 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4005
4006 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004007
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004008 if (!Entry)
4009 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004010 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004011 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004012 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004013
Owen Anderson170229f2009-07-14 23:10:40 +00004014 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004015}
4016
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004017llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004018 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00004019 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4020 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004021
4022 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4023
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004024 if (!Entry)
4025 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004026 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00004027 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004028 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004029
Owen Anderson170229f2009-07-14 23:10:40 +00004030 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004031}
4032
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004033// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004034llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004035 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004036
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004037 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004038 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004039 llvm::ConstantArray::get(VMContext,
4040 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004041 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004042 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004043
Owen Anderson170229f2009-07-14 23:10:40 +00004044 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004045}
4046
4047// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004048// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004049llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004050CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4051 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004052 std::string TypeStr;
4053 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004054 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4055}
4056
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004057void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004058 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004059 llvm::SmallVectorImpl<char> &Name) {
4060 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004061 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004062 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4063 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004064 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004065 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004066 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004067 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004068}
4069
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004070void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004071 EmitModuleInfo();
4072
Daniel Dunbarc475d422008-10-29 22:36:39 +00004073 // Emit the dummy bodies for any protocols which were referenced but
4074 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004075 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004076 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4077 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004078 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004079
Daniel Dunbarc475d422008-10-29 22:36:39 +00004080 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004081 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004082 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004083 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004084 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004085 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004086 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004087 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004088 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004089 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004090 }
4091
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004092 // Add assembler directives to add lazy undefined symbol references
4093 // for classes which are referenced but not defined. This is
4094 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004095 //
4096 // FIXME: It would be nice if we had an LLVM construct for this.
4097 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4098 llvm::SmallString<256> Asm;
4099 Asm += CGM.getModule().getModuleInlineAsm();
4100 if (!Asm.empty() && Asm.back() != '\n')
4101 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004102
Daniel Dunbard027a922009-09-07 00:20:42 +00004103 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004104 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4105 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004106 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4107 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004108 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004109 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004110 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004111 }
4112
4113 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4114 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4115 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4116 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004117
Daniel Dunbard027a922009-09-07 00:20:42 +00004118 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004119 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004120}
4121
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004123 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004124 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004125 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004126 ObjCABI = 2;
4127}
4128
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004129/* *** */
4130
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004131ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004132 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004133 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4134 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004136 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004137 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004138 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004139 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004140 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004141
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004142 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004143 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004144 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004145
Mike Stump18bb9282009-05-16 07:57:57 +00004146 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4147 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004148 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004149 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004150
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004151 // I'm not sure I like this. The implicit coordination is a bit
4152 // gross. We should solve this in a reasonable fashion because this
4153 // is a pretty common task (match some runtime data structure with
4154 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004156 // FIXME: This is leaked.
4157 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004158
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004159 // struct _objc_super {
4160 // id self;
4161 // Class cls;
4162 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004163 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004164 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004165 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004166 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004167 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004168 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004169 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004170 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004171 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004172
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004173 SuperCTy = Ctx.getTagDeclType(RD);
4174 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004175
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004176 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004177 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4178
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004179 // struct _prop_t {
4180 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004181 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004182 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004183 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004185 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004186
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004187 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004188 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004189 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004190 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004191 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004192 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004193 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004194 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004195 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004197 PropertyListTy);
4198 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004199 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004200
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004201 // struct _objc_method {
4202 // SEL _cmd;
4203 // char *method_type;
4204 // char *_imp;
4205 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004206 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004207 Int8PtrTy,
4208 Int8PtrTy,
4209 NULL);
4210 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004211
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004212 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004213 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004214 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004215 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004216}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004217
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004218ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004219 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004220 // struct _objc_method_description {
4221 // SEL name;
4222 // char *types;
4223 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004224 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004225 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004226 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004227 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004228 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004229 MethodDescriptionTy);
4230
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004231 // struct _objc_method_description_list {
4232 // int count;
4233 // struct _objc_method_description[1];
4234 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004235 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004236 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004237 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004238 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004239 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004240 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004241
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004242 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004243 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004244 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004245
Daniel Dunbarb036db82008-08-13 03:21:16 +00004246 // Protocol description structures
4247
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004248 // struct _objc_protocol_extension {
4249 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4250 // struct _objc_method_description_list *optional_instance_methods;
4251 // struct _objc_method_description_list *optional_class_methods;
4252 // struct _objc_property_list *instance_properties;
4253 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004254 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004255 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004256 MethodDescriptionListPtrTy,
4257 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004258 PropertyListPtrTy,
4259 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004260 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004261 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004262
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004263 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004264 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004265
Daniel Dunbarc475d422008-10-29 22:36:39 +00004266 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004267
Owen Andersonc36edfe2009-08-13 23:27:53 +00004268 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4269 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004270
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004271 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004272 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004273 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004274 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004275 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004276 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004277 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4278
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004279 // struct _objc_protocol {
4280 // struct _objc_protocol_extension *isa;
4281 // char *protocol_name;
4282 // struct _objc_protocol **_objc_protocol_list;
4283 // struct _objc_method_description_list *instance_methods;
4284 // struct _objc_method_description_list *class_methods;
4285 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004286 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004287 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004288 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004289 MethodDescriptionListPtrTy,
4290 MethodDescriptionListPtrTy,
4291 NULL);
4292 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4293
4294 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004295 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004296 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004297 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004298 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004299
4300 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004301 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004302 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004303
4304 // Class description structures
4305
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004306 // struct _objc_ivar {
4307 // char *ivar_name;
4308 // char *ivar_type;
4309 // int ivar_offset;
4310 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004311 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004312 Int8PtrTy,
4313 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004314 NULL);
4315 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4316
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004317 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004318 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004319 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004320 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004321
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004322 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004323 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004324 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004325 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004326
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004327 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004328 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004329 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004330 Int8PtrTy,
4331 PropertyListPtrTy,
4332 NULL);
4333 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004334 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004335
Owen Andersonc36edfe2009-08-13 23:27:53 +00004336 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004337
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004338 // struct _objc_class {
4339 // Class isa;
4340 // Class super_class;
4341 // char *name;
4342 // long version;
4343 // long info;
4344 // long instance_size;
4345 // struct _objc_ivar_list *ivars;
4346 // struct _objc_method_list *methods;
4347 // struct _objc_cache *cache;
4348 // struct _objc_protocol_list *protocols;
4349 // char *ivar_layout;
4350 // struct _objc_class_ext *ext;
4351 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004352 T = llvm::StructType::get(VMContext,
4353 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004354 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004355 Int8PtrTy,
4356 LongTy,
4357 LongTy,
4358 LongTy,
4359 IvarListPtrTy,
4360 MethodListPtrTy,
4361 CachePtrTy,
4362 ProtocolListPtrTy,
4363 Int8PtrTy,
4364 ClassExtensionPtrTy,
4365 NULL);
4366 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004368 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4369 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004370 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004371
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004372 // struct _objc_category {
4373 // char *category_name;
4374 // char *class_name;
4375 // struct _objc_method_list *instance_method;
4376 // struct _objc_method_list *class_method;
4377 // uint32_t size; // sizeof(struct _objc_category)
4378 // struct _objc_property_list *instance_properties;// category's @property
4379 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004380 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004381 Int8PtrTy,
4382 MethodListPtrTy,
4383 MethodListPtrTy,
4384 ProtocolListPtrTy,
4385 IntTy,
4386 PropertyListPtrTy,
4387 NULL);
4388 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4389
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004390 // Global metadata structures
4391
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004392 // struct _objc_symtab {
4393 // long sel_ref_cnt;
4394 // SEL *refs;
4395 // short cls_def_cnt;
4396 // short cat_def_cnt;
4397 // char *defs[cls_def_cnt + cat_def_cnt];
4398 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004399 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004400 SelectorPtrTy,
4401 ShortTy,
4402 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004403 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004404 NULL);
4405 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004406 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004407
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004408 // struct _objc_module {
4409 // long version;
4410 // long size; // sizeof(struct _objc_module)
4411 // char *name;
4412 // struct _objc_symtab* symtab;
4413 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004414 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004415 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004416 LongTy,
4417 Int8PtrTy,
4418 SymtabPtrTy,
4419 NULL);
4420 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004421
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004422
Mike Stump18bb9282009-05-16 07:57:57 +00004423 // FIXME: This is the size of the setjmp buffer and should be target
4424 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004425 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004426
Anders Carlsson9ff22482008-09-09 10:10:21 +00004427 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004428 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004429 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004430
4431 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004432 llvm::StructType::get(VMContext,
4433 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4434 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004435 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004436 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004437 ExceptionDataTy);
4438
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004439}
4440
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004441ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004442 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004443 // struct _method_list_t {
4444 // uint32_t entsize; // sizeof(struct _objc_method)
4445 // uint32_t method_count;
4446 // struct _objc_method method_list[method_count];
4447 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004448 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004449 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004450 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004451 NULL);
4452 CGM.getModule().addTypeName("struct.__method_list_t",
4453 MethodListnfABITy);
4454 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004455 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004456
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004457 // struct _protocol_t {
4458 // id isa; // NULL
4459 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004460 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004461 // const struct method_list_t * const instance_methods;
4462 // const struct method_list_t * const class_methods;
4463 // const struct method_list_t *optionalInstanceMethods;
4464 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004465 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004466 // const uint32_t size; // sizeof(struct _protocol_t)
4467 // const uint32_t flags; // = 0
4468 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004470 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004471 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004472
Owen Anderson758428f2009-08-05 23:18:46 +00004473 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004474 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004475 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004476 ProtocolListTyHolder),
4477 MethodListnfABIPtrTy,
4478 MethodListnfABIPtrTy,
4479 MethodListnfABIPtrTy,
4480 MethodListnfABIPtrTy,
4481 PropertyListPtrTy,
4482 IntTy,
4483 IntTy,
4484 NULL);
4485 CGM.getModule().addTypeName("struct._protocol_t",
4486 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004487
4488 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004489 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004490
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004491 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004492 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004493 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004494 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004495 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004496 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004497 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004498 NULL);
4499 CGM.getModule().addTypeName("struct._objc_protocol_list",
4500 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004501 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004502 ProtocolListnfABITy);
4503
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004504 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004505 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004506
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004507 // struct _ivar_t {
4508 // unsigned long int *offset; // pointer to ivar offset location
4509 // char *name;
4510 // char *type;
4511 // uint32_t alignment;
4512 // uint32_t size;
4513 // }
Mike Stump11289f42009-09-09 15:08:12 +00004514 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004515 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004516 Int8PtrTy,
4517 Int8PtrTy,
4518 IntTy,
4519 IntTy,
4520 NULL);
4521 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004522
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004523 // struct _ivar_list_t {
4524 // uint32 entsize; // sizeof(struct _ivar_t)
4525 // uint32 count;
4526 // struct _iver_t list[count];
4527 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004528 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004529 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004530 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004531 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004532 NULL);
4533 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004534
Owen Anderson9793f0e2009-07-29 22:16:19 +00004535 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004536
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004537 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004538 // uint32_t const flags;
4539 // uint32_t const instanceStart;
4540 // uint32_t const instanceSize;
4541 // uint32_t const reserved; // only when building for 64bit targets
4542 // const uint8_t * const ivarLayout;
4543 // const char *const name;
4544 // const struct _method_list_t * const baseMethods;
4545 // const struct _objc_protocol_list *const baseProtocols;
4546 // const struct _ivar_list_t *const ivars;
4547 // const uint8_t * const weakIvarLayout;
4548 // const struct _prop_list_t * const properties;
4549 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004550
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004551 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004552 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004553 IntTy,
4554 IntTy,
4555 Int8PtrTy,
4556 Int8PtrTy,
4557 MethodListnfABIPtrTy,
4558 ProtocolListnfABIPtrTy,
4559 IvarListnfABIPtrTy,
4560 Int8PtrTy,
4561 PropertyListPtrTy,
4562 NULL);
4563 CGM.getModule().addTypeName("struct._class_ro_t",
4564 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004565
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004566 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4567 std::vector<const llvm::Type*> Params;
4568 Params.push_back(ObjectPtrTy);
4569 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004570 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004571 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4572
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004573 // struct _class_t {
4574 // struct _class_t *isa;
4575 // struct _class_t * const superclass;
4576 // void *cache;
4577 // IMP *vtable;
4578 // struct class_ro_t *ro;
4579 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004580
Owen Andersonc36edfe2009-08-13 23:27:53 +00004581 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004582 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004583 llvm::StructType::get(VMContext,
4584 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004585 llvm::PointerType::getUnqual(ClassTyHolder),
4586 CachePtrTy,
4587 llvm::PointerType::getUnqual(ImpnfABITy),
4588 llvm::PointerType::getUnqual(ClassRonfABITy),
4589 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004590 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4591
4592 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004593 ClassnfABITy);
4594
Fariborz Jahanian71394042009-01-23 23:53:38 +00004595 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004596 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004598 // struct _category_t {
4599 // const char * const name;
4600 // struct _class_t *const cls;
4601 // const struct _method_list_t * const instance_methods;
4602 // const struct _method_list_t * const class_methods;
4603 // const struct _protocol_list_t * const protocols;
4604 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004605 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004606 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004607 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004608 MethodListnfABIPtrTy,
4609 MethodListnfABIPtrTy,
4610 ProtocolListnfABIPtrTy,
4611 PropertyListPtrTy,
4612 NULL);
4613 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004614
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004615 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004616 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4617 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004619 // MessageRefTy - LLVM for:
4620 // struct _message_ref_t {
4621 // IMP messenger;
4622 // SEL name;
4623 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004625 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004626 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004627 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004628 SourceLocation(),
4629 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004630 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004631 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004632 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004633 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004634 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004635
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004636 MessageRefCTy = Ctx.getTagDeclType(RD);
4637 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4638 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004639
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004640 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004641 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004642
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004643 // SuperMessageRefTy - LLVM for:
4644 // struct _super_message_ref_t {
4645 // SUPER_IMP messenger;
4646 // SEL name;
4647 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004648 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004649 SelectorPtrTy,
4650 NULL);
4651 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004653 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004654 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4655
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004656
4657 // struct objc_typeinfo {
4658 // const void** vtable; // objc_ehtype_vtable + 2
4659 // const char* name; // c++ typeinfo string
4660 // Class cls;
4661 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004662 EHTypeTy = llvm::StructType::get(VMContext,
4663 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004664 Int8PtrTy,
4665 ClassnfABIPtrTy,
4666 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004667 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004668 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004669}
4670
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004671llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004672 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004673
Fariborz Jahanian71394042009-01-23 23:53:38 +00004674 return NULL;
4675}
4676
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004677void CGObjCNonFragileABIMac::AddModuleClassList(const
4678 std::vector<llvm::GlobalValue*>
4679 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004680 const char *SymbolName,
4681 const char *SectionName) {
4682 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683
Daniel Dunbar19573e72009-05-15 21:48:48 +00004684 if (!NumClasses)
4685 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004686
Daniel Dunbar19573e72009-05-15 21:48:48 +00004687 std::vector<llvm::Constant*> Symbols(NumClasses);
4688 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004689 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004690 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004691 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004692 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004693 NumClasses),
4694 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004695
Daniel Dunbar19573e72009-05-15 21:48:48 +00004696 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004697 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004698 llvm::GlobalValue::InternalLinkage,
4699 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004700 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004701 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004702 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004703 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004704}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004705
Fariborz Jahanian71394042009-01-23 23:53:38 +00004706void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4707 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004708
Daniel Dunbar19573e72009-05-15 21:48:48 +00004709 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004710 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004711 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004712 "\01L_OBJC_LABEL_CLASS_$",
4713 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004714
Fariborz Jahanian67260552009-11-17 21:37:35 +00004715 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4716 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4717 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4718 continue;
4719 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004720 }
4721
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004722 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4723 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4724 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4725 continue;
4726 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4727 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004728
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004729 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004730 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4731 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004732
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004733 // Build list of all implemented category addresses in array
4734 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004735 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004736 "\01L_OBJC_LABEL_CATEGORY_$",
4737 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004738 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004739 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4740 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004741
Daniel Dunbar5e639272010-04-25 20:39:01 +00004742 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004743}
4744
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004745/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004746/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004747/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004748/// message dispatch call for all the rest.
4749///
4750bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004751 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4752 default:
4753 assert(0 && "Invalid dispatch method!");
4754 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004755 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004756 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004757 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004758 case CodeGenOptions::Mixed:
4759 break;
4760 }
4761
4762 // If so, see whether this selector is in the white-list of things which must
4763 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004764 if (NonLegacyDispatchMethods.empty()) {
4765 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4766 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4767 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4768 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4769 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4770 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4771 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4772 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4773 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4774 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004775
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004776 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4777 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4778 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4779 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4780 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4781 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4782 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4783 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004784 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004785 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004786 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4787 &CGM.getContext().Idents.get("objects"),
4788 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004789 };
4790 NonLegacyDispatchMethods.insert(
4791 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004792 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004793
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004794 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004795}
4796
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004797// Metadata flags
4798enum MetaDataDlags {
4799 CLS = 0x0,
4800 CLS_META = 0x1,
4801 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004802 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004803 CLS_EXCEPTION = 0x20
4804};
4805/// BuildClassRoTInitializer - generate meta-data for:
4806/// struct _class_ro_t {
4807/// uint32_t const flags;
4808/// uint32_t const instanceStart;
4809/// uint32_t const instanceSize;
4810/// uint32_t const reserved; // only when building for 64bit targets
4811/// const uint8_t * const ivarLayout;
4812/// const char *const name;
4813/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004814/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004815/// const struct _ivar_list_t *const ivars;
4816/// const uint8_t * const weakIvarLayout;
4817/// const struct _prop_list_t * const properties;
4818/// }
4819///
4820llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004821 unsigned flags,
4822 unsigned InstanceStart,
4823 unsigned InstanceSize,
4824 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004825 std::string ClassName = ID->getNameAsString();
4826 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004827 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4828 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4829 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004830 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004831 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4832 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004833 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004834 // const struct _method_list_t * const baseMethods;
4835 std::vector<llvm::Constant*> Methods;
4836 std::string MethodListName("\01l_OBJC_$_");
4837 if (flags & CLS_META) {
4838 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004839 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004840 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004841 // Class methods should always be defined.
4842 Methods.push_back(GetMethodConstant(*i));
4843 }
4844 } else {
4845 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004846 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004847 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004848 // Instance methods should always be defined.
4849 Methods.push_back(GetMethodConstant(*i));
4850 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004851 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004852 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004853 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004854
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004855 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4856 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004857
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004858 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4859 if (llvm::Constant *C = GetMethodConstant(MD))
4860 Methods.push_back(C);
4861 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4862 if (llvm::Constant *C = GetMethodConstant(MD))
4863 Methods.push_back(C);
4864 }
4865 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004866 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004867 Values[ 5] = EmitMethodList(MethodListName,
4868 "__DATA, __objc_const", Methods);
4869
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004870 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4871 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004872 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004873 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004874 OID->all_referenced_protocol_begin(),
4875 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004877 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004878 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004879 else
4880 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004881 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4882 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004883 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004884 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004885 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004886 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4887 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004888 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004889 Values);
4890 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004891 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4892 llvm::GlobalValue::InternalLinkage,
4893 Init,
4894 (flags & CLS_META) ?
4895 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4896 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004897 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004898 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004899 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004900 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004901
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004902}
4903
4904/// BuildClassMetaData - This routine defines that to-level meta-data
4905/// for the given ClassName for:
4906/// struct _class_t {
4907/// struct _class_t *isa;
4908/// struct _class_t * const superclass;
4909/// void *cache;
4910/// IMP *vtable;
4911/// struct class_ro_t *ro;
4912/// }
4913///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004914llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004915 std::string &ClassName,
4916 llvm::Constant *IsAGV,
4917 llvm::Constant *SuperClassGV,
4918 llvm::Constant *ClassRoGV,
4919 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004920 std::vector<llvm::Constant*> Values(5);
4921 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004922 Values[1] = SuperClassGV;
4923 if (!Values[1])
4924 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004925 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4926 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4927 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004929 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004930 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4931 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004932 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004933 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004934 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004935 if (HiddenVisibility)
4936 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004937 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004938}
4939
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004940bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004941CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004942 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004943}
4944
Daniel Dunbar961202372009-05-03 12:57:56 +00004945void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004946 uint32_t &InstanceStart,
4947 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004948 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004949 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004950
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004951 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004952 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004953
4954 // If there are no fields, the start is the same as the end.
4955 if (!RL.getFieldCount())
4956 InstanceStart = InstanceSize;
4957 else
4958 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004959}
4960
Fariborz Jahanian71394042009-01-23 23:53:38 +00004961void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4962 std::string ClassName = ID->getNameAsString();
4963 if (!ObjCEmptyCacheVar) {
4964 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965 CGM.getModule(),
4966 ObjCTypes.CacheTy,
4967 false,
4968 llvm::GlobalValue::ExternalLinkage,
4969 0,
4970 "_objc_empty_cache");
4971
Fariborz Jahanian71394042009-01-23 23:53:38 +00004972 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004973 CGM.getModule(),
4974 ObjCTypes.ImpnfABITy,
4975 false,
4976 llvm::GlobalValue::ExternalLinkage,
4977 0,
4978 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004979 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004980 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004981 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004982 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004983 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004984 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004985 uint32_t InstanceSize = InstanceStart;
4986 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004987 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4988 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004989
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004990 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004991
4992 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004993 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004994 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004995 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004996 if (ID->getNumIvarInitializers())
4997 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004998 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004999 // class is root
5000 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005001 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005002 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005003 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005004 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005005 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5006 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5007 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005008 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005009 if (Root->hasAttr<WeakImportAttr>())
5010 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005011 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005012 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005013 ObjCMetaClassName +
5014 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005015 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005016 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5017 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005018 }
5019 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5020 InstanceStart,
5021 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005022 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005023 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005024 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5025 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005026 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005027
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005028 // Metadata for the class
5029 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005030 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005031 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005032 if (ID->getNumIvarInitializers())
5033 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005034
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005035 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005036 flags |= CLS_EXCEPTION;
5037
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005038 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005039 flags |= CLS_ROOT;
5040 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005041 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005042 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005043 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005044 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005045 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005046 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5047 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005048 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005049 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005050 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005051 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005052 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005053 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005054
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005055 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005056 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005057 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5058 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005059 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005060
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005061 // Determine if this class is also "non-lazy".
5062 if (ImplementationIsNonLazy(ID))
5063 DefinedNonLazyClasses.push_back(ClassMD);
5064
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005065 // Force the definition of the EHType if necessary.
5066 if (flags & CLS_EXCEPTION)
5067 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005068}
5069
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005070/// GenerateProtocolRef - This routine is called to generate code for
5071/// a protocol reference expression; as in:
5072/// @code
5073/// @protocol(Proto1);
5074/// @endcode
5075/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5076/// which will hold address of the protocol meta-data.
5077///
5078llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079 const ObjCProtocolDecl *PD) {
5080
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005081 // This routine is called for @protocol only. So, we must build definition
5082 // of protocol's meta-data (not a reference to it!)
5083 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005084 llvm::Constant *Init =
5085 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5086 ObjCTypes.ExternalProtocolPtrTy);
5087
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005088 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005089 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005090
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005091 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5092 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005093 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005094 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005095 CGM.getModule(),
5096 Init->getType(), false,
5097 llvm::GlobalValue::WeakAnyLinkage,
5098 Init,
5099 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005100 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5101 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005102 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005103 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005104}
5105
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005106/// GenerateCategory - Build metadata for a category implementation.
5107/// struct _category_t {
5108/// const char * const name;
5109/// struct _class_t *const cls;
5110/// const struct _method_list_t * const instance_methods;
5111/// const struct _method_list_t * const class_methods;
5112/// const struct _protocol_list_t * const protocols;
5113/// const struct _prop_list_t * const properties;
5114/// }
5115///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005116void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005117 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005118 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005119 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5120 "_$_" + OCD->getNameAsString());
5121 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005122 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005123
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005124 std::vector<llvm::Constant*> Values(6);
5125 Values[0] = GetClassName(OCD->getIdentifier());
5126 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005127 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005128 if (Interface->hasAttr<WeakImportAttr>())
5129 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5130
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005131 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005132 std::vector<llvm::Constant*> Methods;
5133 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005135 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005136
5137 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005138 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005139 // Instance methods should always be defined.
5140 Methods.push_back(GetMethodConstant(*i));
5141 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142
5143 Values[2] = EmitMethodList(MethodListName,
5144 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005145 Methods);
5146
5147 MethodListName = Prefix;
5148 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5149 OCD->getNameAsString();
5150 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005151 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005152 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005153 // Class methods should always be defined.
5154 Methods.push_back(GetMethodConstant(*i));
5155 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005156
5157 Values[3] = EmitMethodList(MethodListName,
5158 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005159 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005160 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005161 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005162 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005163 llvm::SmallString<256> ExtName;
5164 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5165 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005166 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005167 + Interface->getName() + "_$_"
5168 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005169 Category->protocol_begin(),
5170 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005171 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5172 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005173 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005174 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5175 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005176 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005177
5178 llvm::Constant *Init =
5179 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005180 Values);
5181 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005182 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005183 false,
5184 llvm::GlobalValue::InternalLinkage,
5185 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005186 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005187 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005188 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005189 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005190 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005191 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005192
5193 // Determine if this category is also "non-lazy".
5194 if (ImplementationIsNonLazy(OCD))
5195 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005196}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005197
5198/// GetMethodConstant - Return a struct objc_method constant for the
5199/// given method if it has been defined. The result is null if the
5200/// method has not been defined. The return value has type MethodPtrTy.
5201llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005202 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005203 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005204 if (!Fn)
5205 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005206
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005207 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005208 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005209 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005210 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005211 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005212 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005213 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005214}
5215
5216/// EmitMethodList - Build meta-data for method declarations
5217/// struct _method_list_t {
5218/// uint32_t entsize; // sizeof(struct _objc_method)
5219/// uint32_t method_count;
5220/// struct _objc_method method_list[method_count];
5221/// }
5222///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005223llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5224 const char *Section,
5225 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005226 // Return null for empty list.
5227 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005228 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005229
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005230 std::vector<llvm::Constant*> Values(3);
5231 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005232 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005233 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005234 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005235 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005236 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005237 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005238 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005239 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005240
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005241 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005242 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005243 llvm::GlobalValue::InternalLinkage,
5244 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005245 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005246 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005247 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005248 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005249 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005250 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005251 ObjCTypes.MethodListnfABIPtrTy);
5252}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005253
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005254/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5255/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005256llvm::GlobalVariable *
5257CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5258 const ObjCIvarDecl *Ivar) {
5259 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005260 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005261 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005262 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005263 CGM.getModule().getGlobalVariable(Name);
5264 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005265 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005266 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005267 false,
5268 llvm::GlobalValue::ExternalLinkage,
5269 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005270 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005271 return IvarOffsetGV;
5272}
5273
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005274llvm::Constant *
5275CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5276 const ObjCIvarDecl *Ivar,
5277 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005278 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005279 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005280 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005281 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005282 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005283
Mike Stump18bb9282009-05-16 07:57:57 +00005284 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5285 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005286 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5287 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005288 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005289 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005290 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005291 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005292 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005293 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005294}
5295
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005296/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005297/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005298/// IvarListnfABIPtrTy.
5299/// struct _ivar_t {
5300/// unsigned long int *offset; // pointer to ivar offset location
5301/// char *name;
5302/// char *type;
5303/// uint32_t alignment;
5304/// uint32_t size;
5305/// }
5306/// struct _ivar_list_t {
5307/// uint32 entsize; // sizeof(struct _ivar_t)
5308/// uint32 count;
5309/// struct _iver_t list[count];
5310/// }
5311///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005312
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005313llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005314 const ObjCImplementationDecl *ID) {
5315
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005316 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005317
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005318 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5319 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005320
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005321 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005322
Daniel Dunbarae032262009-04-20 00:33:43 +00005323 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005324 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005325 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005327 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5328 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005329 // Ignore unnamed bit-fields.
5330 if (!IVD->getDeclName())
5331 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005332 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005333 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005334 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5335 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005336 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005337 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005338 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005339 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005340 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005341 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005342 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005343 // NOTE. Size of a bitfield does not match gcc's, because of the
5344 // way bitfields are treated special in each. But I am told that
5345 // 'size' for bitfield ivars is ignored by the runtime so it does
5346 // not matter. If it matters, there is enough info to get the
5347 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005348 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005349 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005350 }
5351 // Return null for empty list.
5352 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005353 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005354 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005355 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005356 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5357 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005358 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005359 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005360 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005361 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005362 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5363 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005364 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005365 llvm::GlobalValue::InternalLinkage,
5366 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005367 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005368 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005369 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005370 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005371
Chris Lattnerf56501c2009-07-17 23:57:13 +00005372 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005373 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005374}
5375
5376llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005377 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005378 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005379
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005380 if (!Entry) {
5381 // We use the initializer as a marker of whether this is a forward
5382 // reference or not. At module finalization we add the empty
5383 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005384 Entry =
5385 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5386 llvm::GlobalValue::ExternalLinkage,
5387 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005388 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005389 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005390 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005391
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005392 return Entry;
5393}
5394
5395/// GetOrEmitProtocol - Generate the protocol meta-data:
5396/// @code
5397/// struct _protocol_t {
5398/// id isa; // NULL
5399/// const char * const protocol_name;
5400/// const struct _protocol_list_t * protocol_list; // super protocols
5401/// const struct method_list_t * const instance_methods;
5402/// const struct method_list_t * const class_methods;
5403/// const struct method_list_t *optionalInstanceMethods;
5404/// const struct method_list_t *optionalClassMethods;
5405/// const struct _prop_list_t * properties;
5406/// const uint32_t size; // sizeof(struct _protocol_t)
5407/// const uint32_t flags; // = 0
5408/// }
5409/// @endcode
5410///
5411
5412llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005415
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005416 // Early exit if a defining object has already been generated.
5417 if (Entry && Entry->hasInitializer())
5418 return Entry;
5419
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005420 // Construct method lists.
5421 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5422 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005423 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005424 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005425 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005426 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005427 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5428 OptInstanceMethods.push_back(C);
5429 } else {
5430 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005431 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005432 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005433
5434 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005435 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005436 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005437 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005438 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5439 OptClassMethods.push_back(C);
5440 } else {
5441 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005442 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005443 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005444
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005445 std::vector<llvm::Constant*> Values(10);
5446 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005447 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005448 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005449 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5450 PD->protocol_begin(),
5451 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005453 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005454 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005455 "__DATA, __objc_const",
5456 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005458 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 "__DATA, __objc_const",
5460 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005461 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005462 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005463 "__DATA, __objc_const",
5464 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005465 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005466 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005467 "__DATA, __objc_const",
5468 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005469 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005470 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005472 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005473 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005474 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005475 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005476 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005477
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005478 if (Entry) {
5479 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005480 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005481 Entry->setInitializer(Init);
5482 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005484 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5485 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5486 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005487 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005488 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005490 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005491 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005492 CGM.AddUsedGlobal(Entry);
5493
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005494 // Use this protocol meta-data to build protocol list table in section
5495 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005496 llvm::GlobalVariable *PTGV =
5497 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5498 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5499 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005500 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005501 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005502 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005503 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005504 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005505 return Entry;
5506}
5507
5508/// EmitProtocolList - Generate protocol list meta-data:
5509/// @code
5510/// struct _protocol_list_t {
5511/// long protocol_count; // Note, this is 32/64 bit
5512/// struct _protocol_t[protocol_count];
5513/// }
5514/// @endcode
5515///
5516llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005517CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5518 ObjCProtocolDecl::protocol_iterator begin,
5519 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005520 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005521
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005522 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005523 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005524 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005525
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005526 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005527 llvm::SmallString<256> TmpName;
5528 Name.toVector(TmpName);
5529 llvm::GlobalVariable *GV =
5530 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005531 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005532 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005534 for (; begin != end; ++begin)
5535 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5536
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005537 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005538 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005539 ObjCTypes.ProtocolnfABIPtrTy));
5540
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005541 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005542 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005543 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005545 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005546 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005547 ProtocolRefs.size()),
5548 ProtocolRefs);
5549
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005550 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005551 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005552 llvm::GlobalValue::InternalLinkage,
5553 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005554 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005555 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005556 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005557 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005558 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005559 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005560 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005561}
5562
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005563/// GetMethodDescriptionConstant - This routine build following meta-data:
5564/// struct _objc_method {
5565/// SEL _cmd;
5566/// char *method_type;
5567/// char *_imp;
5568/// }
5569
5570llvm::Constant *
5571CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5572 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005573 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005574 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5575 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005576 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005577 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005578 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005579 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005580}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005581
5582/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5583/// This code gen. amounts to generating code for:
5584/// @code
5585/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5586/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005587///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005588LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005589 CodeGen::CodeGenFunction &CGF,
5590 QualType ObjectTy,
5591 llvm::Value *BaseValue,
5592 const ObjCIvarDecl *Ivar,
5593 unsigned CVRQualifiers) {
5594 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005595 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5596 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005597}
5598
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005599llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005600 CodeGen::CodeGenFunction &CGF,
5601 const ObjCInterfaceDecl *Interface,
5602 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005603 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005604}
5605
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005606CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005607 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005608 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005609 QualType ResultType,
5610 Selector Sel,
5611 llvm::Value *Receiver,
5612 QualType Arg0Ty,
5613 bool IsSuper,
5614 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005615 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5616 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005617 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005618 llvm::Value *Arg0 = Receiver;
5619 if (!IsSuper)
5620 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005621
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005622 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005623 // FIXME. This is too much work to get the ABI-specific result type needed to
5624 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005625 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005626 = Types.getFunctionInfo(ResultType, CallArgList(),
5627 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005628 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005629 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005630 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner396639d2010-08-18 16:09:06 +00005631 if (IsSuper) {
5632 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5633 Name += "objc_msgSendSuper2_stret_fixup";
5634 } else {
5635 Fn = ObjCTypes.getMessageSendStretFixupFn();
5636 Name += "objc_msgSend_stret_fixup";
5637 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005638 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5639 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5640 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005641 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005642 if (IsSuper) {
5643 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5644 Name += "objc_msgSendSuper2_fixup";
5645 } else {
5646 Fn = ObjCTypes.getMessageSendFixupFn();
5647 Name += "objc_msgSend_fixup";
5648 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005649 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005650 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005651 Name += '_';
5652 std::string SelName(Sel.getAsString());
5653 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005654 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005655 if (SelName[i] == ':')
5656 SelName[i] = '_';
5657 Name += SelName;
5658 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5659 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005660 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005661 std::vector<llvm::Constant*> Values(2);
5662 Values[0] = Fn;
5663 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005664 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005665 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005666 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005667 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005668 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005669 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005670 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005671 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005672 }
5673 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005674
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005675 CallArgList ActualArgs;
5676 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005677 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005678 ObjCTypes.MessageRefCPtrTy));
5679 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005680 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005681 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005682 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5683 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005684 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005685 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005686 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005687 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005688}
5689
5690/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005691CodeGen::RValue
5692CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005693 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005694 QualType ResultType,
5695 Selector Sel,
5696 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005697 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005698 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005699 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005700 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005701 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5702 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005704 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005705 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005706 Receiver, CGF.getContext().getObjCIdType(),
5707 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005708}
5709
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005710llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005711CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005712 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5713
Daniel Dunbara6468342009-03-02 05:18:14 +00005714 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005716 false, llvm::GlobalValue::ExternalLinkage,
5717 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005718 }
5719
5720 return GV;
5721}
5722
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005723llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5724 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005725 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005726
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005727 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005728 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005729 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005730 Entry =
5731 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005732 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005733 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005734 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005735 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005736 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005737 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005738 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005739 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005740 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005741
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005742 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005743}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005744
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005745llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005746CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005747 const ObjCInterfaceDecl *ID) {
5748 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005749
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005750 if (!Entry) {
5751 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5752 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005753 Entry =
5754 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005755 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005756 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005757 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005758 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005759 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005760 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005761 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005762 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005763 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005764
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005765 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005766}
5767
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005768/// EmitMetaClassRef - Return a Value * of the address of _class_t
5769/// meta-data
5770///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005771llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5772 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005773 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5774 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005775 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005776
Daniel Dunbar15894b72009-04-07 05:48:37 +00005777 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005778 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005779 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005780 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005781 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005782 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005783 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005784 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005785 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005786 ObjCTypes.ClassnfABIPtrTy));
5787
Daniel Dunbare60aa052009-04-15 19:03:14 +00005788 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005789 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005790
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005791 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005792}
5793
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005794/// GetClass - Return a reference to the class for the given interface
5795/// decl.
5796llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5797 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005798 if (ID->hasAttr<WeakImportAttr>()) {
5799 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5800 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5801 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5802 }
5803
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005804 return EmitClassRef(Builder, ID);
5805}
5806
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005807/// Generates a message send where the super is the receiver. This is
5808/// a message send to self with special delivery semantics indicating
5809/// which class's method should be called.
5810CodeGen::RValue
5811CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005812 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005813 QualType ResultType,
5814 Selector Sel,
5815 const ObjCInterfaceDecl *Class,
5816 bool isCategoryImpl,
5817 llvm::Value *Receiver,
5818 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005819 const CodeGen::CallArgList &CallArgs,
5820 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005821 // ...
5822 // Create and init a super structure; this is a (receiver, class)
5823 // pair we will pass to objc_msgSendSuper.
5824 llvm::Value *ObjCSuper =
5825 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005826
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005827 llvm::Value *ReceiverAsObject =
5828 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5829 CGF.Builder.CreateStore(ReceiverAsObject,
5830 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005831
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005832 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005833 llvm::Value *Target;
5834 if (IsClassMessage) {
5835 if (isCategoryImpl) {
5836 // Message sent to "super' in a class method defined in
5837 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005838 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005839 Target = CGF.Builder.CreateStructGEP(Target, 0);
5840 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005841 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005842 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005843 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005844 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005845
Mike Stump18bb9282009-05-16 07:57:57 +00005846 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5847 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005848 const llvm::Type *ClassTy =
5849 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5850 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5851 CGF.Builder.CreateStore(Target,
5852 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005853
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005854 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005855 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5856 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005857 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005858 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005859 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005860 ObjCSuper, ObjCTypes.SuperPtrCTy,
5861 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005862}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005864llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005865 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005866 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005867
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005868 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005869 llvm::Constant *Casted =
5870 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5871 ObjCTypes.SelectorPtrTy);
5872 Entry =
5873 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5874 llvm::GlobalValue::InternalLinkage,
5875 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005876 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005877 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005878 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005879
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005880 if (lval)
5881 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005882 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005883}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005884/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005885/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005886///
5887void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005888 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005889 llvm::Value *dst,
5890 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005891 const llvm::Type * SrcTy = src->getType();
5892 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005893 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005894 assert(Size <= 8 && "does not support size > 8");
5895 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5896 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005897 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5898 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005899 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5900 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005901 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5902 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005903 return;
5904}
5905
5906/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5907/// objc_assign_strongCast (id src, id *dst)
5908///
5909void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005910 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005911 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005912 const llvm::Type * SrcTy = src->getType();
5913 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005914 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005915 assert(Size <= 8 && "does not support size > 8");
5916 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005917 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005918 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5919 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005920 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5921 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005922 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005923 src, dst, "weakassign");
5924 return;
5925}
5926
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005927void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005928 CodeGen::CodeGenFunction &CGF,
5929 llvm::Value *DestPtr,
5930 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005931 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005932 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5933 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005934 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005935 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005936 return;
5937}
5938
Fariborz Jahanian06292952009-02-16 22:52:32 +00005939/// EmitObjCWeakRead - Code gen for loading value of a __weak
5940/// object: objc_read_weak (id *src)
5941///
5942llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005944 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005945 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005946 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5947 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005948 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005949 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005950 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005951 return read_weak;
5952}
5953
5954/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5955/// objc_assign_weak (id src, id *dst)
5956///
5957void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005958 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005959 const llvm::Type * SrcTy = src->getType();
5960 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005961 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005962 assert(Size <= 8 && "does not support size > 8");
5963 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5964 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005965 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5966 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005967 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5968 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005969 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005970 src, dst, "weakassign");
5971 return;
5972}
5973
5974/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5975/// objc_assign_global (id src, id *dst)
5976///
5977void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005978 llvm::Value *src, llvm::Value *dst,
5979 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005980 const llvm::Type * SrcTy = src->getType();
5981 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005982 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005983 assert(Size <= 8 && "does not support size > 8");
5984 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5985 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005986 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5987 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005988 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5989 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005990 if (!threadlocal)
5991 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5992 src, dst, "globalassign");
5993 else
5994 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5995 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005996 return;
5997}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005998
John McCallc20acd32010-07-21 00:41:47 +00005999namespace {
John McCallcda666c2010-07-21 07:22:38 +00006000 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00006001 llvm::Value *SyncExitFn;
6002 llvm::Value *SyncArg;
6003 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6004 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6005
6006 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6007 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6008 }
6009 };
6010}
6011
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006012void
John McCallbd309292010-07-06 01:34:17 +00006013CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6014 const ObjCAtSynchronizedStmt &S) {
6015 // Evaluate the lock operand. This should dominate the cleanup.
6016 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006017
John McCallbd309292010-07-06 01:34:17 +00006018 // Acquire the lock.
6019 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6020 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6021 ->setDoesNotThrow();
6022
6023 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00006024 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6025 ObjCTypes.getSyncExitFn(),
6026 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006027
John McCallbd309292010-07-06 01:34:17 +00006028 // Emit the body of the statement.
6029 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006030
John McCallbd309292010-07-06 01:34:17 +00006031 // Pop the lock-release cleanup.
6032 CGF.PopCleanupBlock();
6033}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006034
John McCallbd309292010-07-06 01:34:17 +00006035namespace {
6036 struct CatchHandler {
6037 const VarDecl *Variable;
6038 const Stmt *Body;
6039 llvm::BasicBlock *Block;
6040 llvm::Value *TypeInfo;
6041 };
John McCall5c08ab92010-07-13 22:12:14 +00006042
John McCallcda666c2010-07-21 07:22:38 +00006043 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00006044 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6045 MightThrow(MightThrow), Fn(Fn) {}
6046 bool MightThrow;
6047 llvm::Value *Fn;
6048
6049 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6050 if (!MightThrow) {
6051 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6052 return;
6053 }
6054
6055 CGF.EmitCallOrInvoke(Fn, 0, 0);
6056 }
6057 };
John McCallbd309292010-07-06 01:34:17 +00006058}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006059
John McCall2ca705e2010-07-24 00:37:23 +00006060llvm::Constant *
6061CGObjCNonFragileABIMac::GetEHType(QualType T) {
6062 // There's a particular fixed type info for 'id'.
6063 if (T->isObjCIdType() ||
6064 T->isObjCQualifiedIdType()) {
6065 llvm::Constant *IDEHType =
6066 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6067 if (!IDEHType)
6068 IDEHType =
6069 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6070 false,
6071 llvm::GlobalValue::ExternalLinkage,
6072 0, "OBJC_EHTYPE_id");
6073 return IDEHType;
6074 }
6075
6076 // All other types should be Objective-C interface pointer types.
6077 const ObjCObjectPointerType *PT =
6078 T->getAs<ObjCObjectPointerType>();
6079 assert(PT && "Invalid @catch type.");
6080 const ObjCInterfaceType *IT = PT->getInterfaceType();
6081 assert(IT && "Invalid @catch type.");
6082 return GetInterfaceEHType(IT->getDecl(), false);
6083}
6084
John McCallbd309292010-07-06 01:34:17 +00006085void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6086 const ObjCAtTryStmt &S) {
6087 // Jump destination for falling out of catch bodies.
6088 CodeGenFunction::JumpDest Cont;
6089 if (S.getNumCatchStmts())
6090 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006091
John McCallbd309292010-07-06 01:34:17 +00006092 CodeGenFunction::FinallyInfo FinallyInfo;
6093 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6094 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6095 ObjCTypes.getObjCBeginCatchFn(),
6096 ObjCTypes.getObjCEndCatchFn(),
6097 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006098
John McCallbd309292010-07-06 01:34:17 +00006099 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006100
John McCallbd309292010-07-06 01:34:17 +00006101 // Enter the catch, if there is one.
6102 if (S.getNumCatchStmts()) {
6103 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6104 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006105 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006106
John McCallbd309292010-07-06 01:34:17 +00006107 Handlers.push_back(CatchHandler());
6108 CatchHandler &Handler = Handlers.back();
6109 Handler.Variable = CatchDecl;
6110 Handler.Body = CatchStmt->getCatchBody();
6111 Handler.Block = CGF.createBasicBlock("catch");
6112
6113 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006114 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006115 Handler.TypeInfo = 0; // catch-all
6116 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006117 break;
6118 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006119
John McCall2ca705e2010-07-24 00:37:23 +00006120 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006121 }
John McCallbd309292010-07-06 01:34:17 +00006122
6123 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6124 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6125 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006126 }
John McCallbd309292010-07-06 01:34:17 +00006127
6128 // Emit the try body.
6129 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006130
John McCallbd309292010-07-06 01:34:17 +00006131 // Leave the try.
6132 if (S.getNumCatchStmts())
6133 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006134
John McCallbd309292010-07-06 01:34:17 +00006135 // Remember where we were.
6136 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006137
John McCallbd309292010-07-06 01:34:17 +00006138 // Emit the handlers.
6139 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6140 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006141
John McCallbd309292010-07-06 01:34:17 +00006142 CGF.EmitBlock(Handler.Block);
6143 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006144
John McCallbd309292010-07-06 01:34:17 +00006145 // Enter the catch.
6146 llvm::CallInst *Exn =
6147 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6148 "exn.adjusted");
6149 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006150
John McCallbd309292010-07-06 01:34:17 +00006151 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006152 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006153 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6154 EndCatchMightThrow,
6155 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006156
John McCallbd309292010-07-06 01:34:17 +00006157 // Bind the catch parameter if it exists.
6158 if (const VarDecl *CatchParam = Handler.Variable) {
6159 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6160 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006161
John McCall1c9c3fd2010-10-15 04:57:14 +00006162 CGF.EmitAutoVarDecl(*CatchParam);
John McCallbd309292010-07-06 01:34:17 +00006163 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006164 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006165
John McCallbd309292010-07-06 01:34:17 +00006166 CGF.ObjCEHValueStack.push_back(Exn);
6167 CGF.EmitStmt(Handler.Body);
6168 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006169
John McCallbd309292010-07-06 01:34:17 +00006170 // Leave the earlier cleanup.
6171 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006172
John McCallbd309292010-07-06 01:34:17 +00006173 CGF.EmitBranchThroughCleanup(Cont);
6174 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006175
John McCallbd309292010-07-06 01:34:17 +00006176 // Go back to the try-statement fallthrough.
6177 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006178
John McCallbd309292010-07-06 01:34:17 +00006179 // Pop out of the normal cleanup on the finally.
6180 if (S.getFinallyStmt())
6181 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006182
John McCallad5d61e2010-07-23 21:56:41 +00006183 if (Cont.isValid())
6184 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006185}
6186
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006187/// EmitThrowStmt - Generate code for a throw statement.
6188void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6189 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006190 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006191 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006192 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6193 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006194 llvm::Value *Args[] = { Exception };
6195 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6196 Args, Args+1)
6197 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006198 } else {
John McCall17afe452010-10-16 08:21:07 +00006199 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6200 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006201 }
6202
John McCall17afe452010-10-16 08:21:07 +00006203 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006204 CGF.Builder.ClearInsertionPoint();
6205}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006206
John McCall2ca705e2010-07-24 00:37:23 +00006207llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006208CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006209 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006210 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006211
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006212 // If we don't need a definition, return the entry if found or check
6213 // if we use an external reference.
6214 if (!ForDefinition) {
6215 if (Entry)
6216 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006217
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006218 // If this type (or a super class) has the __objc_exception__
6219 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006220 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006221 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006222 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006223 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006224 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006225 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006226 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006227 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006228
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006229 // Otherwise we need to either make a new entry or fill in the
6230 // initializer.
6231 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006232 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006233 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006234 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006235 CGM.getModule().getGlobalVariable(VTableName);
6236 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006237 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6238 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006239 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006240 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006241
Chris Lattner5e016ae2010-06-27 07:15:29 +00006242 llvm::Value *VTableIdx =
6243 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006244
6245 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006246 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006247 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006248 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006249 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006250 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006251
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006252 if (Entry) {
6253 Entry->setInitializer(Init);
6254 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006255 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006256 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006257 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006258 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006259 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006260 }
6261
John McCall457a04e2010-10-22 21:05:15 +00006262 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006263 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006264 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6265 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006266
6267 if (ForDefinition) {
6268 Entry->setSection("__DATA,__objc_const");
6269 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6270 } else {
6271 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6272 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006273
6274 return Entry;
6275}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006276
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006277/* *** */
6278
Daniel Dunbarb036db82008-08-13 03:21:16 +00006279CodeGen::CGObjCRuntime *
6280CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006281 return new CGObjCMac(CGM);
6282}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006283
6284CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006285CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006286 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006287}