blob: 54d0ff21ea44a4123d944002e4d7b585eeb6a862 [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 McCallbd309292010-07-06 01:34:17 +000019#include "CGException.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!");
80
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000109
Daniel Dunbarf166a522010-08-21 03:44:13 +0000110 if (!Ivar->isBitField()) {
111 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
112 LV.getQuals().addCVRQualifiers(CVRQualifiers);
113 return LV;
114 }
Daniel Dunbar961202372009-05-03 12:57:56 +0000115
Daniel Dunbara70fab82010-09-02 23:53:31 +0000116 // We need to compute an access strategy for this bit-field. We are given the
117 // offset to the first byte in the bit-field, the sub-byte offset is taken
118 // from the original layout. We reuse the normal bit-field access strategy by
119 // treating this as an access to a struct where the bit-field is in byte 0,
120 // and adjust the containing type size as appropriate.
121 //
122 // FIXME: Note that currently we make a very conservative estimate of the
123 // alignment of the bit-field, because (a) it is not clear what guarantees the
124 // runtime makes us, and (b) we don't have a way to specify that the struct is
125 // at an alignment plus offset.
126 //
127 // Note, there is a subtle invariant here: we can only call this routine on
128 // non-synthesized ivars but we may be called for synthesized ivars. However,
129 // a synthesized ivar can never be a bit-field, so this is safe.
130 const ASTRecordLayout &RL =
131 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
132 uint64_t TypeSizeInBits = RL.getSize();
133 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
134 uint64_t BitOffset = FieldBitOffset % 8;
135 uint64_t ContainingTypeAlign = 8;
136 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000137 uint64_t BitFieldSize =
138 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000139
Daniel Dunbardc406b82010-04-05 21:36:35 +0000140 // Allocate a new CGBitFieldInfo object to describe this access.
141 //
142 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
143 // layout object. However, this is blocked on other cleanups to the
144 // Objective-C code, so for now we just live with allocating a bunch of these
145 // objects.
Daniel Dunbara70fab82010-09-02 23:53:31 +0000146 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
147 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
148 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbardc406b82010-04-05 21:36:35 +0000149
Daniel Dunbar26d2c392010-08-21 04:05:54 +0000150 return LValue::MakeBitfield(V, *Info,
151 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000152}
153
154///
155
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000156namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000157
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000158typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000159
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160// FIXME: We should find a nicer way to make the labels for metadata, string
161// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000162
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000163class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000164protected:
165 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000166
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000167private:
168 llvm::Constant *getMessageSendFn() const {
169 // id objc_msgSend (id, SEL, ...)
170 std::vector<const llvm::Type*> Params;
171 Params.push_back(ObjectPtrTy);
172 Params.push_back(SelectorPtrTy);
173 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000174 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
175 Params, true),
176 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000177 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000178
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000179 llvm::Constant *getMessageSendStretFn() const {
180 // id objc_msgSend_stret (id, SEL, ...)
181 std::vector<const llvm::Type*> Params;
182 Params.push_back(ObjectPtrTy);
183 Params.push_back(SelectorPtrTy);
184 return
Owen Anderson41a75022009-08-13 21:57:51 +0000185 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000186 Params, true),
187 "objc_msgSend_stret");
188
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000189 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000190
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000191 llvm::Constant *getMessageSendFpretFn() const {
192 // FIXME: This should be long double on x86_64?
193 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
194 std::vector<const llvm::Type*> Params;
195 Params.push_back(ObjectPtrTy);
196 Params.push_back(SelectorPtrTy);
197 return
Owen Anderson41a75022009-08-13 21:57:51 +0000198 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
199 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000200 Params,
201 true),
202 "objc_msgSend_fpret");
203
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000204 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000205
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000206 llvm::Constant *getMessageSendSuperFn() const {
207 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
208 const char *SuperName = "objc_msgSendSuper";
209 std::vector<const llvm::Type*> Params;
210 Params.push_back(SuperPtrTy);
211 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000212 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000213 Params, true),
214 SuperName);
215 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000216
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000217 llvm::Constant *getMessageSendSuperFn2() const {
218 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
219 const char *SuperName = "objc_msgSendSuper2";
220 std::vector<const llvm::Type*> Params;
221 Params.push_back(SuperPtrTy);
222 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000223 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000224 Params, true),
225 SuperName);
226 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000227
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000228 llvm::Constant *getMessageSendSuperStretFn() const {
229 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
230 // SEL op, ...)
231 std::vector<const llvm::Type*> Params;
232 Params.push_back(Int8PtrTy);
233 Params.push_back(SuperPtrTy);
234 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000235 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000236 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000237 Params, true),
238 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000239 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000240
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000241 llvm::Constant *getMessageSendSuperStretFn2() const {
242 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
243 // SEL op, ...)
244 std::vector<const llvm::Type*> Params;
245 Params.push_back(Int8PtrTy);
246 Params.push_back(SuperPtrTy);
247 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000248 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000249 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000250 Params, true),
251 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000252 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000253
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000254 llvm::Constant *getMessageSendSuperFpretFn() const {
255 // There is no objc_msgSendSuper_fpret? How can that work?
256 return getMessageSendSuperFn();
257 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000258
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000259 llvm::Constant *getMessageSendSuperFpretFn2() const {
260 // There is no objc_msgSendSuper_fpret? How can that work?
261 return getMessageSendSuperFn2();
262 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000263
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000264protected:
265 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000266
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000267public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000268 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000269 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000270
Daniel Dunbar5d715592008-08-12 05:28:47 +0000271 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
272 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000273
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000274 /// PtrObjectPtrTy - LLVM type for id *
275 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000276
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000277 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000278 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000279 /// ProtocolPtrTy - LLVM type for external protocol handles
280 /// (typeof(Protocol))
281 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000282
Daniel Dunbarc722b852008-08-30 03:02:31 +0000283 // SuperCTy - clang type for struct objc_super.
284 QualType SuperCTy;
285 // SuperPtrCTy - clang type for struct objc_super *.
286 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000287
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000288 /// SuperTy - LLVM type for struct objc_super.
289 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000290 /// SuperPtrTy - LLVM type for struct objc_super *.
291 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000292
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000293 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
294 /// in GCC parlance).
295 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000296
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000297 /// PropertyListTy - LLVM type for struct objc_property_list
298 /// (_prop_list_t in GCC parlance).
299 const llvm::StructType *PropertyListTy;
300 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
301 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000302
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000303 // MethodTy - LLVM type for struct objc_method.
304 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000305
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000306 /// CacheTy - LLVM type for struct objc_cache.
307 const llvm::Type *CacheTy;
308 /// CachePtrTy - LLVM type for struct objc_cache *.
309 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000310
Chris Lattnerce8754e2009-04-22 02:44:54 +0000311 llvm::Constant *getGetPropertyFn() {
312 CodeGen::CodeGenTypes &Types = CGM.getTypes();
313 ASTContext &Ctx = CGM.getContext();
314 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000315 llvm::SmallVector<CanQualType,4> Params;
316 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
317 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000318 Params.push_back(IdType);
319 Params.push_back(SelType);
320 Params.push_back(Ctx.LongTy);
321 Params.push_back(Ctx.BoolTy);
322 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000323 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000324 FunctionType::ExtInfo()),
325 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000326 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
327 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000328
Chris Lattnerce8754e2009-04-22 02:44:54 +0000329 llvm::Constant *getSetPropertyFn() {
330 CodeGen::CodeGenTypes &Types = CGM.getTypes();
331 ASTContext &Ctx = CGM.getContext();
332 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000333 llvm::SmallVector<CanQualType,6> Params;
334 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
335 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000336 Params.push_back(IdType);
337 Params.push_back(SelType);
338 Params.push_back(Ctx.LongTy);
339 Params.push_back(IdType);
340 Params.push_back(Ctx.BoolTy);
341 Params.push_back(Ctx.BoolTy);
342 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000343 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000344 FunctionType::ExtInfo()),
345 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000346 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
347 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000348
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000349
350 llvm::Constant *getCopyStructFn() {
351 CodeGen::CodeGenTypes &Types = CGM.getTypes();
352 ASTContext &Ctx = CGM.getContext();
353 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
354 llvm::SmallVector<CanQualType,5> Params;
355 Params.push_back(Ctx.VoidPtrTy);
356 Params.push_back(Ctx.VoidPtrTy);
357 Params.push_back(Ctx.LongTy);
358 Params.push_back(Ctx.BoolTy);
359 Params.push_back(Ctx.BoolTy);
360 const llvm::FunctionType *FTy =
361 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
362 FunctionType::ExtInfo()),
363 false);
364 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
365 }
366
Chris Lattnerce8754e2009-04-22 02:44:54 +0000367 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000368 CodeGen::CodeGenTypes &Types = CGM.getTypes();
369 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000370 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000371 llvm::SmallVector<CanQualType,1> Params;
372 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000373 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000374 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000375 FunctionType::ExtInfo()),
376 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000379
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000380 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000381 llvm::Constant *getGcReadWeakFn() {
382 // id objc_read_weak (id *)
383 std::vector<const llvm::Type*> Args;
384 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000385 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000386 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000387 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000388 }
389
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000390 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000391 llvm::Constant *getGcAssignWeakFn() {
392 // id objc_assign_weak (id, id *)
393 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
394 Args.push_back(ObjectPtrTy->getPointerTo());
395 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000396 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000397 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
398 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000399
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000400 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000401 llvm::Constant *getGcAssignGlobalFn() {
402 // id objc_assign_global(id, id *)
403 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
404 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000405 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000406 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
408 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000409
Fariborz Jahanian217af242010-07-20 20:30:03 +0000410 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
411 llvm::Constant *getGcAssignThreadLocalFn() {
412 // id objc_assign_threadlocal(id src, id * dest)
413 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
414 Args.push_back(ObjectPtrTy->getPointerTo());
415 llvm::FunctionType *FTy =
416 llvm::FunctionType::get(ObjectPtrTy, Args, false);
417 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
418 }
419
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000420 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000421 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000422 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000423 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
424 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000425 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000426 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000427 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000428 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
429 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000430
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000431 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
432 llvm::Constant *GcMemmoveCollectableFn() {
433 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
434 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
435 Args.push_back(Int8PtrTy);
436 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000437 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000438 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
439 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000440
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000441 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000442 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000443 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000444 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
445 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000446 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000447 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000448 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
449 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000450
451 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000452 llvm::Constant *getExceptionThrowFn() {
453 // void objc_exception_throw(id)
454 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
455 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000456 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000457 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
458 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000459
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000460 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
461 llvm::Constant *getExceptionRethrowFn() {
462 // void objc_exception_rethrow(void)
463 std::vector<const llvm::Type*> Args;
464 llvm::FunctionType *FTy =
465 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
466 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
467 }
468
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000469 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000470 llvm::Constant *getSyncEnterFn() {
471 // void objc_sync_enter (id)
472 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
473 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000474 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000475 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
476 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000477
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000478 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000479 llvm::Constant *getSyncExitFn() {
480 // void objc_sync_exit (id)
481 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
482 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000483 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000484 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
485 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000486
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
489 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000490
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendFn2(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000494
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
497 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000498
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendStretFn2(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
501 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000502
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
505 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000506
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000507 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
508 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
509 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000510
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000511 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
512 ~ObjCCommonTypesHelper(){}
513};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000514
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000515/// ObjCTypesHelper - Helper class that encapsulates lazy
516/// construction of varies types used during ObjC generation.
517class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000518public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000519 /// SymtabTy - LLVM type for struct objc_symtab.
520 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000521 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
522 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000523 /// ModuleTy - LLVM type for struct objc_module.
524 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000525
Daniel Dunbarb036db82008-08-13 03:21:16 +0000526 /// ProtocolTy - LLVM type for struct objc_protocol.
527 const llvm::StructType *ProtocolTy;
528 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
529 const llvm::Type *ProtocolPtrTy;
530 /// ProtocolExtensionTy - LLVM type for struct
531 /// objc_protocol_extension.
532 const llvm::StructType *ProtocolExtensionTy;
533 /// ProtocolExtensionTy - LLVM type for struct
534 /// objc_protocol_extension *.
535 const llvm::Type *ProtocolExtensionPtrTy;
536 /// MethodDescriptionTy - LLVM type for struct
537 /// objc_method_description.
538 const llvm::StructType *MethodDescriptionTy;
539 /// MethodDescriptionListTy - LLVM type for struct
540 /// objc_method_description_list.
541 const llvm::StructType *MethodDescriptionListTy;
542 /// MethodDescriptionListPtrTy - LLVM type for struct
543 /// objc_method_description_list *.
544 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000545 /// ProtocolListTy - LLVM type for struct objc_property_list.
546 const llvm::Type *ProtocolListTy;
547 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
548 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000549 /// CategoryTy - LLVM type for struct objc_category.
550 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000551 /// ClassTy - LLVM type for struct objc_class.
552 const llvm::StructType *ClassTy;
553 /// ClassPtrTy - LLVM type for struct objc_class *.
554 const llvm::Type *ClassPtrTy;
555 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
556 const llvm::StructType *ClassExtensionTy;
557 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
558 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000559 // IvarTy - LLVM type for struct objc_ivar.
560 const llvm::StructType *IvarTy;
561 /// IvarListTy - LLVM type for struct objc_ivar_list.
562 const llvm::Type *IvarListTy;
563 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
564 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000565 /// MethodListTy - LLVM type for struct objc_method_list.
566 const llvm::Type *MethodListTy;
567 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
568 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000569
Anders Carlsson9ff22482008-09-09 10:10:21 +0000570 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
571 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000572
Anders Carlsson9ff22482008-09-09 10:10:21 +0000573 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000574 llvm::Constant *getExceptionTryEnterFn() {
575 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000576 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000577 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000578 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000579 Params, false),
580 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000581 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000582
583 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000584 llvm::Constant *getExceptionTryExitFn() {
585 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000586 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000587 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000588 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000589 Params, false),
590 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000591 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000592
593 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000594 llvm::Constant *getExceptionExtractFn() {
595 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000596 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
597 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000598 Params, false),
599 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000600
Chris Lattnerc6406db2009-04-22 02:26:14 +0000601 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Anders Carlsson9ff22482008-09-09 10:10:21 +0000603 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000604 llvm::Constant *getExceptionMatchFn() {
605 std::vector<const llvm::Type*> Params;
606 Params.push_back(ClassPtrTy);
607 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000608 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000609 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000610 Params, false),
611 "objc_exception_match");
612
Chris Lattnerc6406db2009-04-22 02:26:14 +0000613 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000614
Anders Carlsson9ff22482008-09-09 10:10:21 +0000615 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000616 llvm::Constant *getSetJmpFn() {
617 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000618 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000619 return
Owen Anderson41a75022009-08-13 21:57:51 +0000620 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000621 Params, false),
622 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000623
Chris Lattnerc6406db2009-04-22 02:26:14 +0000624 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000626public:
627 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000628 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000629};
630
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000631/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000632/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000633class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000634public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000635
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000636 // MethodListnfABITy - LLVM for struct _method_list_t
637 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000639 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
640 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000641
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000642 // ProtocolnfABITy = LLVM for struct _protocol_t
643 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000645 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
646 const llvm::Type *ProtocolnfABIPtrTy;
647
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000648 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
649 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000650
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000651 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
652 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000653
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000654 // ClassnfABITy - LLVM for struct _class_t
655 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000656
Fariborz Jahanian71394042009-01-23 23:53:38 +0000657 // ClassnfABIPtrTy - LLVM for struct _class_t*
658 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000659
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000660 // IvarnfABITy - LLVM for struct _ivar_t
661 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000662
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000663 // IvarListnfABITy - LLVM for struct _ivar_list_t
664 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000665
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000666 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
667 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000668
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000669 // ClassRonfABITy - LLVM for struct _class_ro_t
670 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000671
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000672 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
673 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000674
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000675 // CategorynfABITy - LLVM for struct _category_t
676 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000677
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000678 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000679
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000680 // MessageRefTy - LLVM for:
681 // struct _message_ref_t {
682 // IMP messenger;
683 // SEL name;
684 // };
685 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000686 // MessageRefCTy - clang type for struct _message_ref_t
687 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000688
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000689 // MessageRefPtrTy - LLVM for struct _message_ref_t*
690 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000691 // MessageRefCPtrTy - clang type for struct _message_ref_t*
692 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000693
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000694 // MessengerTy - Type of the messenger (shown as IMP above)
695 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000696
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000697 // SuperMessageRefTy - LLVM for:
698 // struct _super_message_ref_t {
699 // SUPER_IMP messenger;
700 // SEL name;
701 // };
702 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000703
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000704 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
705 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000706
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000707 llvm::Constant *getMessageSendFixupFn() {
708 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
709 std::vector<const llvm::Type*> Params;
710 Params.push_back(ObjectPtrTy);
711 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000712 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000713 Params, true),
714 "objc_msgSend_fixup");
715 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000716
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000717 llvm::Constant *getMessageSendFpretFixupFn() {
718 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
719 std::vector<const llvm::Type*> Params;
720 Params.push_back(ObjectPtrTy);
721 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000723 Params, true),
724 "objc_msgSend_fpret_fixup");
725 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000726
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000727 llvm::Constant *getMessageSendStretFixupFn() {
728 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
729 std::vector<const llvm::Type*> Params;
730 Params.push_back(ObjectPtrTy);
731 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000732 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000733 Params, true),
734 "objc_msgSend_stret_fixup");
735 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000736
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000737 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000738 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000739 // struct _super_message_ref_t*, ...)
740 std::vector<const llvm::Type*> Params;
741 Params.push_back(SuperPtrTy);
742 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000743 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000744 Params, true),
745 "objc_msgSendSuper2_fixup");
746 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000747
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000748 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000750 // struct _super_message_ref_t*, ...)
751 std::vector<const llvm::Type*> Params;
752 Params.push_back(SuperPtrTy);
753 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000754 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000755 Params, true),
756 "objc_msgSendSuper2_stret_fixup");
757 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000758
Chris Lattnera7c00b42009-04-22 02:15:23 +0000759 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000760 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000761 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000762 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000763
Chris Lattnera7c00b42009-04-22 02:15:23 +0000764 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000765
Chris Lattnera7c00b42009-04-22 02:15:23 +0000766 llvm::Constant *getObjCBeginCatchFn() {
767 std::vector<const llvm::Type*> Params;
768 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000770 Params, false),
771 "objc_begin_catch");
772 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000773
774 const llvm::StructType *EHTypeTy;
775 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000776
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000777 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
778 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000779};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000780
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000781class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000782public:
783 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000784 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000785 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000786 unsigned ivar_bytepos;
787 unsigned ivar_size;
788 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000789 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000790
791 // Allow sorting based on byte pos.
792 bool operator<(const GC_IVAR &b) const {
793 return ivar_bytepos < b.ivar_bytepos;
794 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000795 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000796
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000797 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000798 public:
799 unsigned skip;
800 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000801 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000802 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000803 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000804
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000805protected:
806 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000807 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000808 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000809 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000810
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000811 // gc ivar layout bitmap calculation helper caches.
812 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
813 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000814
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000815 /// LazySymbols - Symbols to generate a lazy reference for. See
816 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000817 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000818
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000819 /// DefinedSymbols - External symbols which are defined by this
820 /// module. The symbols in this list and LazySymbols are used to add
821 /// special linker symbols which ensure that Objective-C modules are
822 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000823 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000824
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000825 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000826 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000827
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000828 /// MethodVarNames - uniqued method variable names.
829 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000830
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000831 /// DefinedCategoryNames - list of category names in form Class_Category.
832 llvm::SetVector<std::string> DefinedCategoryNames;
833
Daniel Dunbarb036db82008-08-13 03:21:16 +0000834 /// MethodVarTypes - uniqued method type signatures. We have to use
835 /// a StringMap here because have no other unique reference.
836 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000837
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000838 /// MethodDefinitions - map of methods which have been defined in
839 /// this translation unit.
840 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000841
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000842 /// PropertyNames - uniqued method variable names.
843 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000844
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000845 /// ClassReferences - uniqued class references.
846 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000847
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000848 /// SelectorReferences - uniqued selector references.
849 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000850
Daniel Dunbarb036db82008-08-13 03:21:16 +0000851 /// Protocols - Protocols for which an objc_protocol structure has
852 /// been emitted. Forward declarations are handled by creating an
853 /// empty structure whose initializer is filled in when/if defined.
854 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Daniel Dunbarc475d422008-10-29 22:36:39 +0000856 /// DefinedProtocols - Protocols which have actually been
857 /// defined. We should not need this, see FIXME in GenerateProtocol.
858 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000859
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000860 /// DefinedClasses - List of defined classes.
861 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000862
863 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
864 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000865
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000866 /// DefinedCategories - List of defined categories.
867 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000868
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000869 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
870 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000871
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000872 /// GetNameForMethod - Return a name for the given method.
873 /// \param[out] NameOut - The return value.
874 void GetNameForMethod(const ObjCMethodDecl *OMD,
875 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000876 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000877
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000878 /// GetMethodVarName - Return a unique constant for the given
879 /// selector's name. The return value has type char *.
880 llvm::Constant *GetMethodVarName(Selector Sel);
881 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000882
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000883 /// GetMethodVarType - Return a unique constant for the given
884 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000885
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000886 // FIXME: This is a horrible name.
887 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000888 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000889
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000890 /// GetPropertyName - Return a unique constant for the given
891 /// name. The return value has type char *.
892 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000894 // FIXME: This can be dropped once string functions are unified.
895 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
896 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000897
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000898 /// GetClassName - Return a unique constant for the given selector's
899 /// name. The return value has type char *.
900 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000901
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000902 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
903
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000904 /// BuildIvarLayout - Builds ivar layout bitmap for the class
905 /// implementation for the __strong or __weak case.
906 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000907 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
908 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000909
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000910 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000911
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000912 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913 unsigned int BytePos, bool ForStrongLayout,
914 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000915 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000916 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000917 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000918 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000919 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000920 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000921
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000922 /// GetIvarLayoutName - Returns a unique constant for the given
923 /// ivar layout bitmap.
924 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
925 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000926
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000927 /// EmitPropertyList - Emit the given property list. The return
928 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000929 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000930 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000931 const ObjCContainerDecl *OCD,
932 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000933
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000934 /// PushProtocolProperties - Push protocol's property on the input stack.
935 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
936 std::vector<llvm::Constant*> &Properties,
937 const Decl *Container,
938 const ObjCProtocolDecl *PROTO,
939 const ObjCCommonTypesHelper &ObjCTypes);
940
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000941 /// GetProtocolRef - Return a reference to the internal protocol
942 /// description, creating an empty one if it has not been
943 /// defined. The return value has type ProtocolPtrTy.
944 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000945
Daniel Dunbar30c65362009-03-09 20:09:19 +0000946 /// CreateMetadataVar - Create a global variable with internal
947 /// linkage for use by the Objective-C runtime.
948 ///
949 /// This is a convenience wrapper which not only creates the
950 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000951 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000952 ///
953 /// \param Name - The variable name.
954 /// \param Init - The variable initializer; this is also used to
955 /// define the type of the variable.
956 /// \param Section - The section the variable should go into, or 0.
957 /// \param Align - The alignment for the variable, or 0.
958 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000959 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000960 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000961 llvm::Constant *Init,
962 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000963 unsigned Align,
964 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000965
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000966 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000967 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000968 QualType ResultType,
969 llvm::Value *Sel,
970 llvm::Value *Arg0,
971 QualType Arg0Ty,
972 bool IsSuper,
973 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000974 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000975 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000976
Daniel Dunbar5e639272010-04-25 20:39:01 +0000977 /// EmitImageInfo - Emit the image info marker used to encode some module
978 /// level information.
979 void EmitImageInfo();
980
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000981public:
Owen Andersonae86c192009-07-13 04:10:07 +0000982 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000983 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000984
David Chisnall481e3a82010-01-23 02:40:42 +0000985 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000986
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000987 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
988 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000989
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000990 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000991
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000992 /// GetOrEmitProtocol - Get the protocol object for the given
993 /// declaration, emitting it if necessary. The return value has type
994 /// ProtocolPtrTy.
995 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000996
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000997 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
998 /// object for the given declaration, emitting it if needed. These
999 /// forward references will be filled in with empty bodies if no
1000 /// definition is seen. The return value has type ProtocolPtrTy.
1001 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001002 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1003 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1004
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001005};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001006
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001007class CGObjCMac : public CGObjCCommonMac {
1008private:
1009 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001010
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001011 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001012 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001013 void EmitModuleInfo();
1014
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001015 /// EmitModuleSymols - Emit module symbols, the list of defined
1016 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001017 llvm::Constant *EmitModuleSymbols();
1018
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001019 /// FinishModule - Write out global data structures at the end of
1020 /// processing a translation unit.
1021 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001022
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001023 /// EmitClassExtension - Generate the class extension structure used
1024 /// to store the weak ivar layout and properties. The return value
1025 /// has type ClassExtensionPtrTy.
1026 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1027
1028 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1029 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001030 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001031 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001032
1033 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1034 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001035
1036 /// EmitIvarList - Emit the ivar list for the given
1037 /// implementation. If ForClass is true the list of class ivars
1038 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1039 /// interface ivars will be emitted. The return value has type
1040 /// IvarListPtrTy.
1041 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001042 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001043
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001044 /// EmitMetaClass - Emit a forward reference to the class structure
1045 /// for the metaclass of the given interface. The return value has
1046 /// type ClassPtrTy.
1047 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1048
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001049 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001050 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001051 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1052 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001053 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001054
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001055 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001056
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001057 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001058
1059 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001060 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001061 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001062 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001063 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001064
1065 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001066 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001067 /// - TypeName: The name for the type containing the methods.
1068 /// - IsProtocol: True iff these methods are for a protocol.
1069 /// - ClassMethds: True iff these are class methods.
1070 /// - Required: When true, only "required" methods are
1071 /// listed. Similarly, when false only "optional" methods are
1072 /// listed. For classes this should always be true.
1073 /// - begin, end: The method list to output.
1074 ///
1075 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001076 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001077 const char *Section,
1078 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001079
Daniel Dunbarc475d422008-10-29 22:36:39 +00001080 /// GetOrEmitProtocol - Get the protocol object for the given
1081 /// declaration, emitting it if necessary. The return value has type
1082 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001083 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001084
1085 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1086 /// object for the given declaration, emitting it if needed. These
1087 /// forward references will be filled in with empty bodies if no
1088 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001089 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001090
Daniel Dunbarb036db82008-08-13 03:21:16 +00001091 /// EmitProtocolExtension - Generate the protocol extension
1092 /// structure used to store optional instance and class methods, and
1093 /// protocol properties. The return value has type
1094 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001095 llvm::Constant *
1096 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1097 const ConstantVector &OptInstanceMethods,
1098 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001099
1100 /// EmitProtocolList - Generate the list of referenced
1101 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001102 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001103 ObjCProtocolDecl::protocol_iterator begin,
1104 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001105
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001106 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1107 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001108 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1109 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001110
1111public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001112 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001113
Fariborz Jahanian71394042009-01-23 23:53:38 +00001114 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001115
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001116 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001117 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001118 QualType ResultType,
1119 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001120 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001121 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001122 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001123 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001124
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001125 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001126 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001127 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001128 QualType ResultType,
1129 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001130 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001131 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001132 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001133 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001134 const CallArgList &CallArgs,
1135 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001136
Daniel Dunbarcb463852008-11-01 01:53:16 +00001137 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001138 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001139
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001140 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1141 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001142
1143 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1144 /// untyped one.
1145 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1146 const ObjCMethodDecl *Method);
1147
John McCall2ca705e2010-07-24 00:37:23 +00001148 virtual llvm::Constant *GetEHType(QualType T);
1149
Daniel Dunbar92992502008-08-15 22:20:32 +00001150 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001151
Daniel Dunbar92992502008-08-15 22:20:32 +00001152 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001153
Daniel Dunbarcb463852008-11-01 01:53:16 +00001154 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001155 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156
Chris Lattnerd4808922009-03-22 21:03:39 +00001157 virtual llvm::Constant *GetPropertyGetFunction();
1158 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001159 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001160 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161
John McCallbd309292010-07-06 01:34:17 +00001162 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1163 const ObjCAtTryStmt &S);
1164 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1165 const ObjCAtSynchronizedStmt &S);
1166 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001167 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1168 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001169 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001170 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001171 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001172 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001173 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001174 llvm::Value *src, llvm::Value *dest,
1175 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001176 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001177 llvm::Value *src, llvm::Value *dest,
1178 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001179 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1180 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001181 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1182 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001183 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001184
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001185 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1186 QualType ObjectTy,
1187 llvm::Value *BaseValue,
1188 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001189 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001190 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001191 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001192 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001193};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001194
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001195class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001196private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001197 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001198 llvm::GlobalVariable* ObjCEmptyCacheVar;
1199 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001200
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001201 /// SuperClassReferences - uniqued super class references.
1202 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001203
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001204 /// MetaClassReferences - uniqued meta class references.
1205 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001206
1207 /// EHTypeReferences - uniqued class ehtype references.
1208 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001210 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001211 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001212 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001213
Fariborz Jahanian67260552009-11-17 21:37:35 +00001214 /// DefinedMetaClasses - List of defined meta-classes.
1215 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1216
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001217 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001218 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001219 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001220
Fariborz Jahanian71394042009-01-23 23:53:38 +00001221 /// FinishNonFragileABIModule - Write out global data structures at the end of
1222 /// processing a translation unit.
1223 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001224
Daniel Dunbar19573e72009-05-15 21:48:48 +00001225 /// AddModuleClassList - Add the given list of class pointers to the
1226 /// module with the provided symbol and section names.
1227 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1228 const char *SymbolName,
1229 const char *SectionName);
1230
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001231 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1232 unsigned InstanceStart,
1233 unsigned InstanceSize,
1234 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001235 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001236 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001237 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001238 llvm::Constant *ClassRoGV,
1239 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001240
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001241 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001243 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001244
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001245 /// EmitMethodList - Emit the method list for the given
1246 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001247 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001248 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001249 const ConstantVector &Methods);
1250 /// EmitIvarList - Emit the ivar list for the given
1251 /// implementation. If ForClass is true the list of class ivars
1252 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1253 /// interface ivars will be emitted. The return value has type
1254 /// IvarListnfABIPtrTy.
1255 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001257 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001258 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001259 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001260
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001261 /// GetOrEmitProtocol - Get the protocol object for the given
1262 /// declaration, emitting it if necessary. The return value has type
1263 /// ProtocolPtrTy.
1264 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001265
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001266 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1267 /// object for the given declaration, emitting it if needed. These
1268 /// forward references will be filled in with empty bodies if no
1269 /// definition is seen. The return value has type ProtocolPtrTy.
1270 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001271
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001272 /// EmitProtocolList - Generate the list of referenced
1273 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001274 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001275 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001276 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001277
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001278 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001279 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001280 QualType ResultType,
1281 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001282 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001283 QualType Arg0Ty,
1284 bool IsSuper,
1285 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001286
1287 /// GetClassGlobal - Return the global variable for the Objective-C
1288 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001289 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001290
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001291 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001292 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001293 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001294 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001295
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001296 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1297 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001298 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1299 const ObjCInterfaceDecl *ID);
1300
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001301 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1302 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001303 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001304 const ObjCInterfaceDecl *ID);
1305
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001306 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1307 /// the given ivar.
1308 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001309 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310 const ObjCInterfaceDecl *ID,
1311 const ObjCIvarDecl *Ivar);
1312
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001313 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1314 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001315 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1316 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001317
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001318 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001319 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001320 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001321 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001322
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001324 return "OBJC_METACLASS_$_";
1325 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001326
Daniel Dunbar15894b72009-04-07 05:48:37 +00001327 const char *getClassSymbolPrefix() const {
1328 return "OBJC_CLASS_$_";
1329 }
1330
Daniel Dunbar961202372009-05-03 12:57:56 +00001331 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001332 uint32_t &InstanceStart,
1333 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001334
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001335 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001336 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001337 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1338 return CGM.getContext().Selectors.getSelector(0, &II);
1339 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001340
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001341 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001342 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1343 return CGM.getContext().Selectors.getSelector(1, &II);
1344 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001345
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001346 /// ImplementationIsNonLazy - Check whether the given category or
1347 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001348 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001349
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001350public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001351 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001352 // FIXME. All stubs for now!
1353 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001354
Fariborz Jahanian71394042009-01-23 23:53:38 +00001355 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001356 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001357 QualType ResultType,
1358 Selector Sel,
1359 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001360 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001361 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001362 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001363
1364 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001365 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001366 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001367 QualType ResultType,
1368 Selector Sel,
1369 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001370 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001371 llvm::Value *Receiver,
1372 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001373 const CallArgList &CallArgs,
1374 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001375
Fariborz Jahanian71394042009-01-23 23:53:38 +00001376 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001377 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001378
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001379 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1380 bool lvalue = false)
1381 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001382
1383 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1384 /// untyped one.
1385 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1386 const ObjCMethodDecl *Method)
1387 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001388
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001389 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001390
Fariborz Jahanian71394042009-01-23 23:53:38 +00001391 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001392 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001393 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001394
John McCall2ca705e2010-07-24 00:37:23 +00001395 virtual llvm::Constant *GetEHType(QualType T);
1396
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001397 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001398 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001400 virtual llvm::Constant *GetPropertySetFunction() {
1401 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001402 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001403
1404 virtual llvm::Constant *GetCopyStructFunction() {
1405 return ObjCTypes.getCopyStructFn();
1406 }
1407
Chris Lattnerd4808922009-03-22 21:03:39 +00001408 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001409 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001410 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001411
John McCallbd309292010-07-06 01:34:17 +00001412 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1413 const ObjCAtTryStmt &S);
1414 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1415 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001417 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001418 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001419 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001420 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001421 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001422 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001423 llvm::Value *src, llvm::Value *dest,
1424 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001425 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001426 llvm::Value *src, llvm::Value *dest,
1427 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001428 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001429 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001430 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1431 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001432 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001433 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1434 QualType ObjectTy,
1435 llvm::Value *BaseValue,
1436 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001437 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001438 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001439 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001440 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001441};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001442
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001443} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001444
1445/* *** Helper Functions *** */
1446
1447/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001448static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001449 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001450 unsigned idx0,
1451 unsigned idx1) {
1452 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001453 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1454 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001455 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001456 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001457}
1458
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001459/// hasObjCExceptionAttribute - Return true if this class or any super
1460/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001461static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001462 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001463 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001464 return true;
1465 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001466 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001467 return false;
1468}
1469
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001470/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001471
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001472CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001473 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001474 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001475 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001476}
1477
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001478/// GetClass - Return a reference to the class for the given interface
1479/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001480llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001481 const ObjCInterfaceDecl *ID) {
1482 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001483}
1484
1485/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001486llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1487 bool lval) {
1488 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001489}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001490llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001491 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001492 return EmitSelector(Builder, Method->getSelector());
1493}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001494
John McCall2ca705e2010-07-24 00:37:23 +00001495llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1496 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1497 return 0;
1498}
1499
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001500/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001501/*
1502 struct __builtin_CFString {
1503 const int *isa; // point to __CFConstantStringClassReference
1504 int flags;
1505 const char *str;
1506 long length;
1507 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001508*/
1509
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001510/// or Generate a constant NSString object.
1511/*
1512 struct __builtin_NSString {
1513 const int *isa; // point to __NSConstantStringClassReference
1514 const char *str;
1515 unsigned int length;
1516 };
1517*/
1518
Fariborz Jahanian71394042009-01-23 23:53:38 +00001519llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001520 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001521 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1522 CGM.GetAddrOfConstantCFString(SL) :
1523 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001524}
1525
1526/// Generates a message send where the super is the receiver. This is
1527/// a message send to self with special delivery semantics indicating
1528/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001529CodeGen::RValue
1530CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001531 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001532 QualType ResultType,
1533 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001534 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001535 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001536 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001537 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001538 const CodeGen::CallArgList &CallArgs,
1539 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001540 // Create and init a super structure; this is a (receiver, class)
1541 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001542 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001543 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001544 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001545 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001546 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001547 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001548
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001549 // If this is a class message the metaclass is passed as the target.
1550 llvm::Value *Target;
1551 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001552 if (isCategoryImpl) {
1553 // Message sent to 'super' in a class method defined in a category
1554 // implementation requires an odd treatment.
1555 // If we are in a class method, we must retrieve the
1556 // _metaclass_ for the current class, pointed at by
1557 // the class's "isa" pointer. The following assumes that
1558 // isa" is the first ivar in a class (which it must be).
1559 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1560 Target = CGF.Builder.CreateStructGEP(Target, 0);
1561 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001562 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001563 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1564 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1565 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1566 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001567 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001568 }
1569 else if (isCategoryImpl)
1570 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1571 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001572 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1573 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1574 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001575 }
Mike Stump18bb9282009-05-16 07:57:57 +00001576 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1577 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001578 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001579 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001580 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001581 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001582 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001583 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001584 EmitSelector(CGF.Builder, Sel),
1585 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001586 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001587}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001588
1589/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001590CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001591 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001592 QualType ResultType,
1593 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001594 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001595 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001596 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001597 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001598 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001599 EmitSelector(CGF.Builder, Sel),
1600 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001601 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001602}
1603
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001604CodeGen::RValue
1605CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001606 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001607 QualType ResultType,
1608 llvm::Value *Sel,
1609 llvm::Value *Arg0,
1610 QualType Arg0Ty,
1611 bool IsSuper,
1612 const CallArgList &CallArgs,
1613 const ObjCMethodDecl *Method,
1614 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001615 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001616 if (!IsSuper)
1617 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001618 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001619 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001620 CGF.getContext().getObjCSelType()));
1621 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001622
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001623 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001624 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001625 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001626 const llvm::FunctionType *FTy =
1627 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001628
Anders Carlsson280e61f12010-06-21 20:59:55 +00001629 if (Method)
1630 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1631 CGM.getContext().getCanonicalType(ResultType) &&
1632 "Result type mismatch!");
1633
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001634 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001635 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001636 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001637 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001638 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1639 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1640 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001641 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001642 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001643 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001644 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001645 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001646 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001647}
1648
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001649static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1650 if (FQT.isObjCGCStrong())
1651 return Qualifiers::Strong;
1652
1653 if (FQT.isObjCGCWeak())
1654 return Qualifiers::Weak;
1655
1656 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1657 return Qualifiers::Strong;
1658
1659 if (const PointerType *PT = FQT->getAs<PointerType>())
1660 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1661
1662 return Qualifiers::GCNone;
1663}
1664
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001665llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001666 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1667 llvm::Constant *NullPtr =
1668 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian89f37212010-08-05 15:52:12 +00001669 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
1670 DeclRefs.empty())
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001671 return NullPtr;
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001672 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001673 SkipIvars.clear();
1674 IvarsInfo.clear();
1675 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1676 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1677
1678 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1679 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1680 const ValueDecl *VD = BDRE->getDecl();
1681 CharUnits Offset = CGF.BlockDecls[VD];
1682 uint64_t FieldOffset = Offset.getQuantity();
1683 QualType Ty = VD->getType();
1684 assert(!Ty->isArrayType() &&
1685 "Array block variable should have been caught");
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001686 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001687 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1688 FieldOffset,
1689 true,
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001690 hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001691 continue;
1692 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001693
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001694 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1695 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1696 // __block variables are passed by their descriptior address. So, size
1697 // must reflect this.
1698 if (BDRE->isByRef())
1699 FieldSize = WordSizeInBits;
1700 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1701 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1702 FieldSize / WordSizeInBits));
1703 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1704 SkipIvars.push_back(GC_IVAR(FieldOffset,
1705 FieldSize / ByteSizeInBits));
1706 }
1707
1708 if (IvarsInfo.empty())
1709 return NullPtr;
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001710 // Sort on byte position in case we encounterred a union nested in
1711 // block variable type's aggregate type.
1712 if (hasUnion && !IvarsInfo.empty())
1713 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1714 if (hasUnion && !SkipIvars.empty())
1715 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001716
1717 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001718 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001719 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1720 printf("\n block variable layout for block: ");
1721 const unsigned char *s = (unsigned char*)BitMap.c_str();
1722 for (unsigned i = 0; i < BitMap.size(); i++)
1723 if (!(s[i] & 0xf0))
1724 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1725 else
1726 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1727 printf("\n");
1728 }
1729
1730 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001731}
1732
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001733llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001734 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001735 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001736 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001737 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1738
Owen Andersonade90fd2009-07-29 18:54:39 +00001739 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001740 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001741}
1742
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001743void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001744 // FIXME: We shouldn't need this, the protocol decl should contain enough
1745 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001746 DefinedProtocols.insert(PD->getIdentifier());
1747
1748 // If we have generated a forward reference to this protocol, emit
1749 // it now. Otherwise do nothing, the protocol objects are lazily
1750 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001751 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 GetOrEmitProtocol(PD);
1753}
1754
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001755llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001756 if (DefinedProtocols.count(PD->getIdentifier()))
1757 return GetOrEmitProtocol(PD);
1758 return GetOrEmitProtocolRef(PD);
1759}
1760
Daniel Dunbarb036db82008-08-13 03:21:16 +00001761/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001762// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1763struct _objc_protocol {
1764struct _objc_protocol_extension *isa;
1765char *protocol_name;
1766struct _objc_protocol_list *protocol_list;
1767struct _objc__method_prototype_list *instance_methods;
1768struct _objc__method_prototype_list *class_methods
1769};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001770
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001771See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001772*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001773llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1774 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1775
1776 // Early exit if a defining object has already been generated.
1777 if (Entry && Entry->hasInitializer())
1778 return Entry;
1779
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001780 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001781 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001782 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1783
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001784 // Construct method lists.
1785 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1786 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001787 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001788 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001789 ObjCMethodDecl *MD = *i;
1790 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1791 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1792 OptInstanceMethods.push_back(C);
1793 } else {
1794 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001795 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001796 }
1797
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001798 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001799 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001800 ObjCMethodDecl *MD = *i;
1801 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1802 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1803 OptClassMethods.push_back(C);
1804 } else {
1805 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001806 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001807 }
1808
Daniel Dunbarb036db82008-08-13 03:21:16 +00001809 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001810 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001811 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001813 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001814 PD->protocol_begin(),
1815 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001816 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001817 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001818 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1819 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001820 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001821 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001822 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1823 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001824 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001825 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001826
Daniel Dunbarb036db82008-08-13 03:21:16 +00001827 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001828 // Already created, fix the linkage and update the initializer.
1829 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001830 Entry->setInitializer(Init);
1831 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001832 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001833 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001834 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001835 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001836 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001837 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001838 // FIXME: Is this necessary? Why only for protocol?
1839 Entry->setAlignment(4);
1840 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001841 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001842
1843 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001844}
1845
Daniel Dunbarc475d422008-10-29 22:36:39 +00001846llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001847 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1848
1849 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001850 // We use the initializer as a marker of whether this is a forward
1851 // reference or not. At module finalization we add the empty
1852 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001853 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001854 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001855 llvm::GlobalValue::ExternalLinkage,
1856 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001857 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001859 // FIXME: Is this necessary? Why only for protocol?
1860 Entry->setAlignment(4);
1861 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001862
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863 return Entry;
1864}
1865
1866/*
1867 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001868 uint32_t size;
1869 struct objc_method_description_list *optional_instance_methods;
1870 struct objc_method_description_list *optional_class_methods;
1871 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001872 };
1873*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001874llvm::Constant *
1875CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1876 const ConstantVector &OptInstanceMethods,
1877 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001878 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001879 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001880 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001881 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001882 Values[1] =
1883 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001884 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001885 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1886 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001888 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001889 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1890 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001891 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001892 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001893
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001894 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001895 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001896 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001897 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001898
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001899 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001900 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001901
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001902 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001903 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001904 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001905 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001906}
1907
1908/*
1909 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001910 struct objc_protocol_list *next;
1911 long count;
1912 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001913 };
1914*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001915llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001916CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001917 ObjCProtocolDecl::protocol_iterator begin,
1918 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001919 std::vector<llvm::Constant*> ProtocolRefs;
1920
Daniel Dunbardec75f82008-08-21 21:57:41 +00001921 for (; begin != end; ++begin)
1922 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923
1924 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001925 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001926 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001927
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001928 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001929 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001930
1931 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001932 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001933 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001934 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001935 ProtocolRefs.size() - 1);
1936 Values[2] =
1937 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1938 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001939 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001941 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001942 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001943 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001944 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001945 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001946}
1947
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001948void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1949 std::vector<llvm::Constant*> &Properties,
1950 const Decl *Container,
1951 const ObjCProtocolDecl *PROTO,
1952 const ObjCCommonTypesHelper &ObjCTypes) {
1953 std::vector<llvm::Constant*> Prop(2);
1954 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1955 E = PROTO->protocol_end(); P != E; ++P)
1956 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1957 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1958 E = PROTO->prop_end(); I != E; ++I) {
1959 const ObjCPropertyDecl *PD = *I;
1960 if (!PropertySet.insert(PD->getIdentifier()))
1961 continue;
1962 Prop[0] = GetPropertyName(PD->getIdentifier());
1963 Prop[1] = GetPropertyTypeString(PD, Container);
1964 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1965 }
1966}
1967
Daniel Dunbarb036db82008-08-13 03:21:16 +00001968/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001969 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001970 const char * const name;
1971 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001972 };
1973
1974 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001975 uint32_t entsize; // sizeof (struct _objc_property)
1976 uint32_t prop_count;
1977 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001978 };
1979*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001980llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001981 const Decl *Container,
1982 const ObjCContainerDecl *OCD,
1983 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001984 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001985 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1987 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001988 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001989 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001990 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001991 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001992 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001993 Prop));
1994 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001995 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001996 for (ObjCInterfaceDecl::all_protocol_iterator
1997 P = OID->all_referenced_protocol_begin(),
1998 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001999 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2000 ObjCTypes);
2001 }
2002 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2003 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2004 E = CD->protocol_end(); P != E; ++P)
2005 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2006 ObjCTypes);
2007 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002008
2009 // Return null for empty list.
2010 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002011 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002012
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002013 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002014 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002015 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002016 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2017 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002018 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002019 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002020 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002021 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002022
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002023 llvm::GlobalVariable *GV =
2024 CreateMetadataVar(Name, Init,
2025 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002026 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002027 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002028 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002029 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002030}
2031
2032/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002033 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002034 int count;
2035 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002036 };
2037*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002038llvm::Constant *
2039CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2040 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002041 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2043 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002044 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002045 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002046 Desc);
2047}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002048
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002049llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002050 const char *Section,
2051 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002052 // Return null for empty list.
2053 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002054 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002055
2056 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002057 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002058 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002059 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002060 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002061 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002062
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002063 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002064 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002065 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002066}
2067
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002068/*
2069 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002070 char *category_name;
2071 char *class_name;
2072 struct _objc_method_list *instance_methods;
2073 struct _objc_method_list *class_methods;
2074 struct _objc_protocol_list *protocols;
2075 uint32_t size; // <rdar://4585769>
2076 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002077 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002078*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002079void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002080 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002081
Mike Stump18bb9282009-05-16 07:57:57 +00002082 // FIXME: This is poor design, the OCD should have a pointer to the category
2083 // decl. Additionally, note that Category can be null for the @implementation
2084 // w/o an @interface case. Sema should just create one for us as it does for
2085 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002086 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002087 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002088 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002089
2090 llvm::SmallString<256> ExtName;
2091 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2092 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002093
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002094 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002095 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002096 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002097 // Instance methods should always be defined.
2098 InstanceMethods.push_back(GetMethodConstant(*i));
2099 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002100 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002101 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002102 // Class methods should always be defined.
2103 ClassMethods.push_back(GetMethodConstant(*i));
2104 }
2105
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002106 std::vector<llvm::Constant*> Values(7);
2107 Values[0] = GetClassName(OCD->getIdentifier());
2108 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002109 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002110 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002111 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002112 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002113 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002114 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002115 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002116 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002117 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002118 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002119 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002120 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002121 Category->protocol_begin(),
2122 Category->protocol_end());
2123 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002124 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002125 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002126 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002127
2128 // If there is no category @interface then there can be no properties.
2129 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002130 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002131 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002132 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002133 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002134 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002135
Owen Anderson0e0189d2009-07-27 22:29:56 +00002136 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002137 Values);
2138
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002140 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002141 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002142 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002143 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002144 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002145}
2146
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002147// FIXME: Get from somewhere?
2148enum ClassFlags {
2149 eClassFlags_Factory = 0x00001,
2150 eClassFlags_Meta = 0x00002,
2151 // <rdr://5142207>
2152 eClassFlags_HasCXXStructors = 0x02000,
2153 eClassFlags_Hidden = 0x20000,
2154 eClassFlags_ABI2_Hidden = 0x00010,
2155 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2156};
2157
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002158/*
2159 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002160 Class isa;
2161 Class super_class;
2162 const char *name;
2163 long version;
2164 long info;
2165 long instance_size;
2166 struct _objc_ivar_list *ivars;
2167 struct _objc_method_list *methods;
2168 struct _objc_cache *cache;
2169 struct _objc_protocol_list *protocols;
2170 // Objective-C 1.0 extensions (<rdr://4585769>)
2171 const char *ivar_layout;
2172 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002173 };
2174
2175 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002176*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002177void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002178 DefinedSymbols.insert(ID->getIdentifier());
2179
Chris Lattner86d7d912008-11-24 03:54:41 +00002180 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002181 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002182 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002183 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002184 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002185 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002186 Interface->all_referenced_protocol_begin(),
2187 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002188 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002189 if (ID->getNumIvarInitializers())
2190 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002191 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002192 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002193
2194 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002195 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002196 Flags |= eClassFlags_Hidden;
2197
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002198 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002199 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002200 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002201 // Instance methods should always be defined.
2202 InstanceMethods.push_back(GetMethodConstant(*i));
2203 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002204 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002205 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002206 // Class methods should always be defined.
2207 ClassMethods.push_back(GetMethodConstant(*i));
2208 }
2209
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002210 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002211 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002212 ObjCPropertyImplDecl *PID = *i;
2213
2214 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2215 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2216
2217 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2218 if (llvm::Constant *C = GetMethodConstant(MD))
2219 InstanceMethods.push_back(C);
2220 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2221 if (llvm::Constant *C = GetMethodConstant(MD))
2222 InstanceMethods.push_back(C);
2223 }
2224 }
2225
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002226 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002227 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002228 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002229 // Record a reference to the super class.
2230 LazySymbols.insert(Super->getIdentifier());
2231
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002232 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002233 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002234 ObjCTypes.ClassPtrTy);
2235 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002236 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002237 }
2238 Values[ 2] = GetClassName(ID->getIdentifier());
2239 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002240 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2241 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2242 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002243 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002244 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002245 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002246 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002247 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002248 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002249 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002251 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002252 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002253 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002254 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002255 std::string Name("\01L_OBJC_CLASS_");
2256 Name += ClassName;
2257 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2258 // Check for a forward reference.
2259 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2260 if (GV) {
2261 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2262 "Forward metaclass reference has incorrect type.");
2263 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2264 GV->setInitializer(Init);
2265 GV->setSection(Section);
2266 GV->setAlignment(4);
2267 CGM.AddUsedGlobal(GV);
2268 }
2269 else
2270 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002271 DefinedClasses.push_back(GV);
2272}
2273
2274llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2275 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002276 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002277 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002278 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002279
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002280 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002281 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002282
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283 std::vector<llvm::Constant*> Values(12);
2284 // The isa for the metaclass is the root of the hierarchy.
2285 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2286 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2287 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002288 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002289 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002290 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002291 // The super class for the metaclass is emitted as the name of the
2292 // super class. The runtime fixes this up to point to the
2293 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002294 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002295 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002296 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002297 ObjCTypes.ClassPtrTy);
2298 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002299 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002300 }
2301 Values[ 2] = GetClassName(ID->getIdentifier());
2302 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002303 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2304 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2305 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002306 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002307 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002308 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002309 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002310 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002311 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002312 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002313 Values[ 9] = Protocols;
2314 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002315 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002316 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002317 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002318 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002319 Values);
2320
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002321 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002322 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002323
2324 // Check for a forward reference.
2325 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2326 if (GV) {
2327 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2328 "Forward metaclass reference has incorrect type.");
2329 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2330 GV->setInitializer(Init);
2331 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002332 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002333 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002334 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002335 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002337 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002338 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002339
2340 return GV;
2341}
2342
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002343llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002344 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002345
Mike Stump18bb9282009-05-16 07:57:57 +00002346 // FIXME: Should we look these up somewhere other than the module. Its a bit
2347 // silly since we only generate these while processing an implementation, so
2348 // exactly one pointer would work if know when we entered/exitted an
2349 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002350
2351 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002352 // Previously, metaclass with internal linkage may have been defined.
2353 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002354 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2355 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002356 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2357 "Forward metaclass reference has incorrect type.");
2358 return GV;
2359 } else {
2360 // Generate as an external reference to keep a consistent
2361 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002362 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002363 llvm::GlobalValue::ExternalLinkage,
2364 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002365 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002366 }
2367}
2368
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002369llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2370 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2371
2372 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2373 true)) {
2374 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2375 "Forward class metadata reference has incorrect type.");
2376 return GV;
2377 } else {
2378 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2379 llvm::GlobalValue::ExternalLinkage,
2380 0,
2381 Name);
2382 }
2383}
2384
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002385/*
2386 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002387 uint32_t size;
2388 const char *weak_ivar_layout;
2389 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002390 };
2391*/
2392llvm::Constant *
2393CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002394 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002395 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002396
2397 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002398 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002399 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002400 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002401 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002402
2403 // Return null if no extension bits are used.
2404 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002405 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002406
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002407 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002408 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002409 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002410 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002411 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002412}
2413
2414/*
2415 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002416 char *ivar_name;
2417 char *ivar_type;
2418 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002419 };
2420
2421 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002422 int ivar_count;
2423 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002424 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002426llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002427 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002428 std::vector<llvm::Constant*> Ivars, Ivar(3);
2429
2430 // When emitting the root class GCC emits ivar entries for the
2431 // actual class structure. It is not clear if we need to follow this
2432 // behavior; for now lets try and get away with not doing it. If so,
2433 // the cleanest solution would be to make up an ObjCInterfaceDecl
2434 // for the class.
2435 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002436 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002437
2438 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002439 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002440
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002441 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002442 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002443
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002444 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2445 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002446 // Ignore unnamed bit-fields.
2447 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002448 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002449 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2450 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002451 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002452 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002453 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002454 }
2455
2456 // Return null for empty list.
2457 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002458 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002459
2460 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002461 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002462 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002463 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002464 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002465 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002466
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002467 llvm::GlobalVariable *GV;
2468 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002469 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002470 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002471 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002472 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002473 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002474 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002475 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002476 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002477}
2478
2479/*
2480 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002481 SEL method_name;
2482 char *method_types;
2483 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002484 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002485
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002486 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002487 struct objc_method_list *obsolete;
2488 int count;
2489 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490 };
2491*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002492
2493/// GetMethodConstant - Return a struct objc_method constant for the
2494/// given method if it has been defined. The result is null if the
2495/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002496llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002497 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002498 if (!Fn)
2499 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002500
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002501 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002502 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002503 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002504 ObjCTypes.SelectorPtrTy);
2505 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002506 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002507 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002508}
2509
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002510llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002511 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002512 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002513 // Return null for empty list.
2514 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002515 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002516
2517 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002518 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002519 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002520 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002521 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002522 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002523 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002524
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002525 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002526 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002527 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002528}
2529
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002530llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002531 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002532 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002533 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002534
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002535 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002536 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002537 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002539 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002540 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002541 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002542 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002543 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002544
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002545 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002546}
2547
Daniel Dunbar30c65362009-03-09 20:09:19 +00002548llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002549CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002550 llvm::Constant *Init,
2551 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002552 unsigned Align,
2553 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002554 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002555 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002556 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002557 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002558 if (Section)
2559 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002560 if (Align)
2561 GV->setAlignment(Align);
2562 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002563 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002564 return GV;
2565}
2566
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002567llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002568 // Abuse this interface function as a place to finalize.
2569 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002570 return NULL;
2571}
2572
Chris Lattnerd4808922009-03-22 21:03:39 +00002573llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002574 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002575}
2576
Chris Lattnerd4808922009-03-22 21:03:39 +00002577llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002578 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002579}
2580
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002581llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2582 return ObjCTypes.getCopyStructFn();
2583}
2584
Chris Lattnerd4808922009-03-22 21:03:39 +00002585llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002586 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002587}
2588
John McCallbd309292010-07-06 01:34:17 +00002589void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2590 return EmitTryOrSynchronizedStmt(CGF, S);
2591}
2592
2593void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2594 const ObjCAtSynchronizedStmt &S) {
2595 return EmitTryOrSynchronizedStmt(CGF, S);
2596}
2597
John McCall65bea082010-07-21 06:59:36 +00002598namespace {
John McCallcda666c2010-07-21 07:22:38 +00002599 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002600 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002601 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002602 llvm::Value *CallTryExitVar;
2603 llvm::Value *ExceptionData;
2604 ObjCTypesHelper &ObjCTypes;
2605 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002606 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002607 llvm::Value *CallTryExitVar,
2608 llvm::Value *ExceptionData,
2609 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002610 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002611 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2612
2613 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2614 // Check whether we need to call objc_exception_try_exit.
2615 // In optimized code, this branch will always be folded.
2616 llvm::BasicBlock *FinallyCallExit =
2617 CGF.createBasicBlock("finally.call_exit");
2618 llvm::BasicBlock *FinallyNoCallExit =
2619 CGF.createBasicBlock("finally.no_call_exit");
2620 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2621 FinallyCallExit, FinallyNoCallExit);
2622
2623 CGF.EmitBlock(FinallyCallExit);
2624 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2625 ->setDoesNotThrow();
2626
2627 CGF.EmitBlock(FinallyNoCallExit);
2628
2629 if (isa<ObjCAtTryStmt>(S)) {
2630 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002631 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2632 // Save the current cleanup destination in case there's
2633 // control flow inside the finally statement.
2634 llvm::Value *CurCleanupDest =
2635 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2636
John McCall65bea082010-07-21 06:59:36 +00002637 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2638
John McCallcebe0ca2010-08-11 00:16:14 +00002639 if (CGF.HaveInsertPoint()) {
2640 CGF.Builder.CreateStore(CurCleanupDest,
2641 CGF.getNormalCleanupDestSlot());
2642 } else {
2643 // Currently, the end of the cleanup must always exist.
2644 CGF.EnsureInsertPoint();
2645 }
2646 }
John McCall65bea082010-07-21 06:59:36 +00002647 } else {
2648 // Emit objc_sync_exit(expr); as finally's sole statement for
2649 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002650 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002651 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2652 ->setDoesNotThrow();
2653 }
2654 }
2655 };
John McCall42227ed2010-07-31 23:20:56 +00002656
2657 class FragileHazards {
2658 CodeGenFunction &CGF;
2659 llvm::SmallVector<llvm::Value*, 20> Locals;
2660 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2661
2662 llvm::InlineAsm *ReadHazard;
2663 llvm::InlineAsm *WriteHazard;
2664
2665 llvm::FunctionType *GetAsmFnType();
2666
2667 void collectLocals();
2668 void emitReadHazard(CGBuilderTy &Builder);
2669
2670 public:
2671 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002672
John McCall42227ed2010-07-31 23:20:56 +00002673 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002674 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002675 };
2676}
2677
2678/// Create the fragile-ABI read and write hazards based on the current
2679/// state of the function, which is presumed to be immediately prior
2680/// to a @try block. These hazards are used to maintain correct
2681/// semantics in the face of optimization and the fragile ABI's
2682/// cavalier use of setjmp/longjmp.
2683FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2684 collectLocals();
2685
2686 if (Locals.empty()) return;
2687
2688 // Collect all the blocks in the function.
2689 for (llvm::Function::iterator
2690 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2691 BlocksBeforeTry.insert(&*I);
2692
2693 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2694
2695 // Create a read hazard for the allocas. This inhibits dead-store
2696 // optimizations and forces the values to memory. This hazard is
2697 // inserted before any 'throwing' calls in the protected scope to
2698 // reflect the possibility that the variables might be read from the
2699 // catch block if the call throws.
2700 {
2701 std::string Constraint;
2702 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2703 if (I) Constraint += ',';
2704 Constraint += "*m";
2705 }
2706
2707 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2708 }
2709
2710 // Create a write hazard for the allocas. This inhibits folding
2711 // loads across the hazard. This hazard is inserted at the
2712 // beginning of the catch path to reflect the possibility that the
2713 // variables might have been written within the protected scope.
2714 {
2715 std::string Constraint;
2716 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2717 if (I) Constraint += ',';
2718 Constraint += "=*m";
2719 }
2720
2721 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2722 }
2723}
2724
2725/// Emit a write hazard at the current location.
2726void FragileHazards::emitWriteHazard() {
2727 if (Locals.empty()) return;
2728
2729 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2730 ->setDoesNotThrow();
2731}
2732
John McCall42227ed2010-07-31 23:20:56 +00002733void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2734 assert(!Locals.empty());
2735 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2736 ->setDoesNotThrow();
2737}
2738
2739/// Emit read hazards in all the protected blocks, i.e. all the blocks
2740/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002741void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002742 if (Locals.empty()) return;
2743
2744 CGBuilderTy Builder(CGF.getLLVMContext());
2745
2746 // Iterate through all blocks, skipping those prior to the try.
2747 for (llvm::Function::iterator
2748 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2749 llvm::BasicBlock &BB = *FI;
2750 if (BlocksBeforeTry.count(&BB)) continue;
2751
2752 // Walk through all the calls in the block.
2753 for (llvm::BasicBlock::iterator
2754 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2755 llvm::Instruction &I = *BI;
2756
2757 // Ignore instructions that aren't non-intrinsic calls.
2758 // These are the only calls that can possibly call longjmp.
2759 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2760 if (isa<llvm::IntrinsicInst>(I))
2761 continue;
2762
2763 // Ignore call sites marked nounwind. This may be questionable,
2764 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2765 llvm::CallSite CS(&I);
2766 if (CS.doesNotThrow()) continue;
2767
John McCall2dd7d442010-08-04 05:59:32 +00002768 // Insert a read hazard before the call. This will ensure that
2769 // any writes to the locals are performed before making the
2770 // call. If the call throws, then this is sufficient to
2771 // guarantee correctness as long as it doesn't also write to any
2772 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002773 Builder.SetInsertPoint(&BB, BI);
2774 emitReadHazard(Builder);
2775 }
2776 }
2777}
2778
2779static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2780 if (V) S.insert(V);
2781}
2782
2783void FragileHazards::collectLocals() {
2784 // Compute a set of allocas to ignore.
2785 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2786 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2787 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2788 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2789
2790 // Collect all the allocas currently in the function. This is
2791 // probably way too aggressive.
2792 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2793 for (llvm::BasicBlock::iterator
2794 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2795 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2796 Locals.push_back(&*I);
2797}
2798
2799llvm::FunctionType *FragileHazards::GetAsmFnType() {
2800 std::vector<const llvm::Type *> Tys(Locals.size());
2801 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2802 Tys[I] = Locals[I]->getType();
2803 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002804}
2805
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002806/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002807
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002808 Objective-C setjmp-longjmp (sjlj) Exception Handling
2809 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002810
John McCallbd309292010-07-06 01:34:17 +00002811 A catch buffer is a setjmp buffer plus:
2812 - a pointer to the exception that was caught
2813 - a pointer to the previous exception data buffer
2814 - two pointers of reserved storage
2815 Therefore catch buffers form a stack, with a pointer to the top
2816 of the stack kept in thread-local storage.
2817
2818 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2819 objc_exception_try_exit pops the given catch buffer, which is
2820 required to be the top of the EH stack.
2821 objc_exception_throw pops the top of the EH stack, writes the
2822 thrown exception into the appropriate field, and longjmps
2823 to the setjmp buffer. It crashes the process (with a printf
2824 and an abort()) if there are no catch buffers on the stack.
2825 objc_exception_extract just reads the exception pointer out of the
2826 catch buffer.
2827
2828 There's no reason an implementation couldn't use a light-weight
2829 setjmp here --- something like __builtin_setjmp, but API-compatible
2830 with the heavyweight setjmp. This will be more important if we ever
2831 want to implement correct ObjC/C++ exception interactions for the
2832 fragile ABI.
2833
2834 Note that for this use of setjmp/longjmp to be correct, we may need
2835 to mark some local variables volatile: if a non-volatile local
2836 variable is modified between the setjmp and the longjmp, it has
2837 indeterminate value. For the purposes of LLVM IR, it may be
2838 sufficient to make loads and stores within the @try (to variables
2839 declared outside the @try) volatile. This is necessary for
2840 optimized correctness, but is not currently being done; this is
2841 being tracked as rdar://problem/8160285
2842
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002843 The basic framework for a @try-catch-finally is as follows:
2844 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002845 objc_exception_data d;
2846 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002847 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002848
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002849 objc_exception_try_enter(&d);
2850 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002851 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002852 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002853 // exception path
2854 id _caught = objc_exception_extract(&d);
2855
2856 // enter new try scope for handlers
2857 if (!setjmp(d.jmp_buf)) {
2858 ... match exception and execute catch blocks ...
2859
2860 // fell off end, rethrow.
2861 _rethrow = _caught;
2862 ... jump-through-finally to finally_rethrow ...
2863 } else {
2864 // exception in catch block
2865 _rethrow = objc_exception_extract(&d);
2866 _call_try_exit = false;
2867 ... jump-through-finally to finally_rethrow ...
2868 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002869 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002870 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002871
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002872 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002873 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002874 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002875
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002876 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002877 ... dispatch to finally destination ...
2878
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002879 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002880 objc_exception_throw(_rethrow);
2881
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002882 finally_end:
2883 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002884
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002885 This framework differs slightly from the one gcc uses, in that gcc
2886 uses _rethrow to determine if objc_exception_try_exit should be called
2887 and if the object should be rethrown. This breaks in the face of
2888 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002889
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002891
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002892 - If there are no catch blocks, then we avoid emitting the second
2893 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002894
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002895 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2896 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002897
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002898 - FIXME: If there is no @finally block we can do a few more
2899 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002900
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002901 Rethrows and Jumps-Through-Finally
2902 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002903
John McCallbd309292010-07-06 01:34:17 +00002904 '@throw;' is supported by pushing the currently-caught exception
2905 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002906
John McCallbd309292010-07-06 01:34:17 +00002907 Branches through the @finally block are handled with an ordinary
2908 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2909 exceptions are not compatible with C++ exceptions, and this is
2910 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002911
John McCallbd309292010-07-06 01:34:17 +00002912 @synchronized(expr) { stmt; } is emitted as if it were:
2913 id synch_value = expr;
2914 objc_sync_enter(synch_value);
2915 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002916*/
2917
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002918void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2919 const Stmt &S) {
2920 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002921
2922 // A destination for the fall-through edges of the catch handlers to
2923 // jump to.
2924 CodeGenFunction::JumpDest FinallyEnd =
2925 CGF.getJumpDestInCurrentScope("finally.end");
2926
2927 // A destination for the rethrow edge of the catch handlers to jump
2928 // to.
2929 CodeGenFunction::JumpDest FinallyRethrow =
2930 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002932 // For @synchronized, call objc_sync_enter(sync.expr). The
2933 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002934 // @synchronized. We can't avoid a temp here because we need the
2935 // value to be preserved. If the backend ever does liveness
2936 // correctly after setjmp, this will be unnecessary.
2937 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002938 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002939 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002940 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2941 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002942 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2943 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002944
2945 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2946 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002947 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002948
John McCall2dd7d442010-08-04 05:59:32 +00002949 // Allocate memory for the setjmp buffer. This needs to be kept
2950 // live throughout the try and catch blocks.
2951 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2952 "exceptiondata.ptr");
2953
John McCall42227ed2010-07-31 23:20:56 +00002954 // Create the fragile hazards. Note that this will not capture any
2955 // of the allocas required for exception processing, but will
2956 // capture the current basic block (which extends all the way to the
2957 // setjmp call) as "before the @try".
2958 FragileHazards Hazards(CGF);
2959
John McCallbd309292010-07-06 01:34:17 +00002960 // Create a flag indicating whether the cleanup needs to call
2961 // objc_exception_try_exit. This is true except when
2962 // - no catches match and we're branching through the cleanup
2963 // just to rethrow the exception, or
2964 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002965 // The setjmp-safety rule here is that we should always store to this
2966 // variable in a place that dominates the branch through the cleanup
2967 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002968 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002969 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002970
John McCallbd309292010-07-06 01:34:17 +00002971 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002972 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002973 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002974 CallTryExitVar,
2975 ExceptionData,
2976 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002977
2978 // Enter a try block:
2979 // - Call objc_exception_try_enter to push ExceptionData on top of
2980 // the EH stack.
2981 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2982 ->setDoesNotThrow();
2983
2984 // - Call setjmp on the exception data buffer.
2985 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2986 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2987 llvm::Value *SetJmpBuffer =
2988 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2989 llvm::CallInst *SetJmpResult =
2990 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2991 SetJmpResult->setDoesNotThrow();
2992
2993 // If setjmp returned 0, enter the protected block; otherwise,
2994 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002995 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2996 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002997 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002998 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2999 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003000
John McCallbd309292010-07-06 01:34:17 +00003001 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003002 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003003 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003004 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003005 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003006
3007 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008
John McCallbd309292010-07-06 01:34:17 +00003009 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003010 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003011
John McCall42227ed2010-07-31 23:20:56 +00003012 // Don't optimize loads of the in-scope locals across this point.
3013 Hazards.emitWriteHazard();
3014
John McCallbd309292010-07-06 01:34:17 +00003015 // For a @synchronized (or a @try with no catches), just branch
3016 // through the cleanup to the rethrow block.
3017 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3018 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003019 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003020 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003021
3022 // Otherwise, we have to match against the caught exceptions.
3023 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003024 // Retrieve the exception object. We may emit multiple blocks but
3025 // nothing can cross this so the value is already in SSA form.
3026 llvm::CallInst *Caught =
3027 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3028 ExceptionData, "caught");
3029 Caught->setDoesNotThrow();
3030
John McCallbd309292010-07-06 01:34:17 +00003031 // Push the exception to rethrow onto the EH value stack for the
3032 // benefit of any @throws in the handlers.
3033 CGF.ObjCEHValueStack.push_back(Caught);
3034
Douglas Gregor96c79492010-04-23 22:50:49 +00003035 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003036
John McCall2dd7d442010-08-04 05:59:32 +00003037 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003038
John McCall2dd7d442010-08-04 05:59:32 +00003039 llvm::BasicBlock *CatchBlock = 0;
3040 llvm::BasicBlock *CatchHandler = 0;
3041 if (HasFinally) {
3042 // Enter a new exception try block (in case a @catch block
3043 // throws an exception).
3044 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3045 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003046
John McCall2dd7d442010-08-04 05:59:32 +00003047 llvm::CallInst *SetJmpResult =
3048 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3049 "setjmp.result");
3050 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003051
John McCall2dd7d442010-08-04 05:59:32 +00003052 llvm::Value *Threw =
3053 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3054
3055 CatchBlock = CGF.createBasicBlock("catch");
3056 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3057 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3058
3059 CGF.EmitBlock(CatchBlock);
3060 }
3061
3062 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003063
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003064 // Handle catch list. As a special case we check if everything is
3065 // matched and avoid generating code for falling off the end if
3066 // so.
3067 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003068 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3069 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003070
Douglas Gregor46a572b2010-04-26 16:46:50 +00003071 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003072 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003073
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003074 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003075 if (!CatchParam) {
3076 AllMatched = true;
3077 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003078 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003079
John McCallbd309292010-07-06 01:34:17 +00003080 // catch(id e) always matches under this ABI, since only
3081 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003082 // FIXME: For the time being we also match id<X>; this should
3083 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003084 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003085 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003087
John McCallbd309292010-07-06 01:34:17 +00003088 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003089 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003090 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3091
Anders Carlsson9396a892008-09-11 09:15:33 +00003092 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00003093 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003094 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003095
3096 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003097 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003098 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003099
Anders Carlsson9396a892008-09-11 09:15:33 +00003100 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003101
3102 // The scope of the catch variable ends right here.
3103 CatchVarCleanups.ForceCleanup();
3104
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003105 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003106 break;
3107 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003108
Steve Naroff7cae42b2009-07-10 23:34:53 +00003109 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003110 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003111
3112 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003113 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3114 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003115
3116 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003117 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003118
John McCallbd309292010-07-06 01:34:17 +00003119 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003120 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3121 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003122 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003123
John McCallbd309292010-07-06 01:34:17 +00003124 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3125 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003126
3127 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003128 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003129
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003130 // Emit the @catch block.
3131 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // Collect any cleanups for the catch variable. The scope lasts until
3134 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003135 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003136
Steve Naroff371b8fb2009-03-03 19:52:17 +00003137 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003138 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003139
John McCallbd309292010-07-06 01:34:17 +00003140 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003141 llvm::Value *Tmp =
3142 CGF.Builder.CreateBitCast(Caught,
3143 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003144 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003145 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003146
Anders Carlsson9396a892008-09-11 09:15:33 +00003147 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003148
3149 // We're done with the catch variable.
3150 CatchVarCleanups.ForceCleanup();
3151
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003152 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003153
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003154 CGF.EmitBlock(NextCatchBlock);
3155 }
3156
John McCallbd309292010-07-06 01:34:17 +00003157 CGF.ObjCEHValueStack.pop_back();
3158
John McCall2dd7d442010-08-04 05:59:32 +00003159 // If nothing wanted anything to do with the caught exception,
3160 // kill the extract call.
3161 if (Caught->use_empty())
3162 Caught->eraseFromParent();
3163
3164 if (!AllMatched)
3165 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3166
3167 if (HasFinally) {
3168 // Emit the exception handler for the @catch blocks.
3169 CGF.EmitBlock(CatchHandler);
3170
3171 // In theory we might now need a write hazard, but actually it's
3172 // unnecessary because there's no local-accessing code between
3173 // the try's write hazard and here.
3174 //Hazards.emitWriteHazard();
3175
3176 // Don't pop the catch handler; the throw already did.
3177 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003178 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003179 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003180 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003181
John McCall42227ed2010-07-31 23:20:56 +00003182 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003183 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003184
John McCallbd309292010-07-06 01:34:17 +00003185 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003186 CGF.Builder.restoreIP(TryFallthroughIP);
3187 if (CGF.HaveInsertPoint())
3188 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003189 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003190 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003191
John McCallbd309292010-07-06 01:34:17 +00003192 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003193 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003194 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003195 if (CGF.HaveInsertPoint()) {
John McCall2dd7d442010-08-04 05:59:32 +00003196 // Just look in the buffer for the exception to throw.
3197 llvm::CallInst *Caught =
3198 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3199 ExceptionData);
3200 Caught->setDoesNotThrow();
3201
3202 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallbd309292010-07-06 01:34:17 +00003203 ->setDoesNotThrow();
3204 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003205 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003206
John McCall42227ed2010-07-31 23:20:56 +00003207 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003208}
3209
3210void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003211 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003212 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003213
Anders Carlssone005aa12008-09-09 16:16:55 +00003214 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3215 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003216 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003217 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3218 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003219 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003220 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003221 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003222 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003223
John McCallbd309292010-07-06 01:34:17 +00003224 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3225 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003226 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003227
3228 // Clear the insertion point to indicate we are in unreachable code.
3229 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003230}
3231
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003232/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003233/// object: objc_read_weak (id *src)
3234///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003235llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003236 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003237 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003238 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3239 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3240 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003241 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003242 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003243 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003244 return read_weak;
3245}
3246
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003247/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3248/// objc_assign_weak (id src, id *dst)
3249///
3250void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003251 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003252 const llvm::Type * SrcTy = src->getType();
3253 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003254 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003255 assert(Size <= 8 && "does not support size > 8");
3256 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003257 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003258 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3259 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003260 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3261 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003262 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003263 src, dst, "weakassign");
3264 return;
3265}
3266
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003267/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3268/// objc_assign_global (id src, id *dst)
3269///
3270void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003271 llvm::Value *src, llvm::Value *dst,
3272 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003273 const llvm::Type * SrcTy = src->getType();
3274 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003275 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003276 assert(Size <= 8 && "does not support size > 8");
3277 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003278 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003279 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3280 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003281 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3282 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003283 if (!threadlocal)
3284 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3285 src, dst, "globalassign");
3286 else
3287 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3288 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003289 return;
3290}
3291
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003292/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003293/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003294///
3295void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003296 llvm::Value *src, llvm::Value *dst,
3297 llvm::Value *ivarOffset) {
3298 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003299 const llvm::Type * SrcTy = src->getType();
3300 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003301 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003302 assert(Size <= 8 && "does not support size > 8");
3303 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003304 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003305 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3306 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003307 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3308 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003309 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3310 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003311 return;
3312}
3313
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003314/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3315/// objc_assign_strongCast (id src, id *dst)
3316///
3317void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003318 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003319 const llvm::Type * SrcTy = src->getType();
3320 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003321 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003322 assert(Size <= 8 && "does not support size > 8");
3323 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003324 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003325 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3326 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003327 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3328 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003329 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003330 src, dst, "weakassign");
3331 return;
3332}
3333
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003334void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003335 llvm::Value *DestPtr,
3336 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003337 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003338 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3339 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003340 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003341 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003342 return;
3343}
3344
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003345/// EmitObjCValueForIvar - Code Gen for ivar reference.
3346///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003347LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3348 QualType ObjectTy,
3349 llvm::Value *BaseValue,
3350 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003351 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003352 const ObjCInterfaceDecl *ID =
3353 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003354 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3355 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003356}
3357
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003358llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003359 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003360 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003361 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003362 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003363 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3364 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003365}
3366
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003367/* *** Private Interface *** */
3368
3369/// EmitImageInfo - Emit the image info marker used to encode some module
3370/// level information.
3371///
3372/// See: <rdr://4810609&4810587&4810587>
3373/// struct IMAGE_INFO {
3374/// unsigned version;
3375/// unsigned flags;
3376/// };
3377enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003378 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003379 eImageInfo_GarbageCollected = (1 << 1),
3380 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003381 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3382
Daniel Dunbar5e639272010-04-25 20:39:01 +00003383 // A flag indicating that the module has no instances of a @synthesize of a
3384 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003385 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003386};
3387
Daniel Dunbar5e639272010-04-25 20:39:01 +00003388void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003389 unsigned version = 0; // Version is unused?
3390 unsigned flags = 0;
3391
3392 // FIXME: Fix and continue?
3393 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3394 flags |= eImageInfo_GarbageCollected;
3395 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3396 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003397
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003398 // We never allow @synthesize of a superclass property.
3399 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003400
Chris Lattner5e016ae2010-06-27 07:15:29 +00003401 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3402
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003403 // Emitted as int[2];
3404 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003405 llvm::ConstantInt::get(Int32Ty, version),
3406 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003407 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003408 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003409
3410 const char *Section;
3411 if (ObjCABI == 1)
3412 Section = "__OBJC, __image_info,regular";
3413 else
3414 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003415 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003416 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003417 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003418 Section,
3419 0,
3420 true);
3421 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003422}
3423
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003424
3425// struct objc_module {
3426// unsigned long version;
3427// unsigned long size;
3428// const char *name;
3429// Symtab symtab;
3430// };
3431
3432// FIXME: Get from somewhere
3433static const int ModuleVersion = 7;
3434
3435void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003436 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003437
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003438 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003439 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3440 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003441 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003442 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003443 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003444 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003445 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003446 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003447 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003448}
3449
3450llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003451 unsigned NumClasses = DefinedClasses.size();
3452 unsigned NumCategories = DefinedCategories.size();
3453
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003454 // Return null if no symbols were defined.
3455 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003456 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003457
3458 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003459 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003460 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003461 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3462 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003463
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003464 // The runtime expects exactly the list of defined classes followed
3465 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003466 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003467 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003468 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003469 ObjCTypes.Int8PtrTy);
3470 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003471 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003472 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003473 ObjCTypes.Int8PtrTy);
3474
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003475 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003476 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003477 NumClasses + NumCategories),
3478 Symbols);
3479
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003480 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003481
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003482 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003483 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3484 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003485 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003486 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003487}
3488
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003490 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003491 LazySymbols.insert(ID->getIdentifier());
3492
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003493 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003494
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003495 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003496 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003497 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003498 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003499 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003500 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3501 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003502 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003503 }
3504
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003505 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003506}
3507
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003508llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3509 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003510 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003511
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003512 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003513 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003514 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003515 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003516 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003517 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3518 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003519 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003520 }
3521
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003522 if (lvalue)
3523 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003524 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003525}
3526
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003527llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003528 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003529
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003530 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003531 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003532 llvm::ConstantArray::get(VMContext,
3533 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003534 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003535 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003536
Owen Anderson170229f2009-07-14 23:10:40 +00003537 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003538}
3539
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003540llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3541 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3542 I = MethodDefinitions.find(MD);
3543 if (I != MethodDefinitions.end())
3544 return I->second;
3545
3546 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3547 // MD isn't emitted yet because it comes from PCH.
3548 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3549 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3550 return MethodDefinitions[MD];
3551 }
3552
3553 return NULL;
3554}
3555
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003556/// GetIvarLayoutName - Returns a unique constant for the given
3557/// ivar layout bitmap.
3558llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003560 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003561}
3562
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003563void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003565 bool ForStrongLayout,
3566 bool &HasUnion) {
3567 const RecordDecl *RD = RT->getDecl();
3568 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003569 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003570 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003571 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003572 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003573
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003574 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3575 ForStrongLayout, HasUnion);
3576}
3577
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003578void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003579 const llvm::StructLayout *Layout,
3580 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003581 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 unsigned int BytePos, bool ForStrongLayout,
3583 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003584 bool IsUnion = (RD && RD->isUnion());
3585 uint64_t MaxUnionIvarSize = 0;
3586 uint64_t MaxSkippedUnionIvarSize = 0;
3587 FieldDecl *MaxField = 0;
3588 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003589 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003590 uint64_t MaxFieldOffset = 0;
3591 uint64_t MaxSkippedFieldOffset = 0;
3592 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003593
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003594 if (RecFields.empty())
3595 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003596 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3597 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3598
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003599 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003600 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003601 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003602 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003603 // Note that 'i' here is actually the field index inside RD of Field,
3604 // although this dependency is hidden.
3605 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3606 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003607 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003608 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003609
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003610 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003611 if (!Field->getIdentifier() || Field->isBitField()) {
3612 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003613 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003614 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003615 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003616
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003617 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003618 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003619 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003620 if (FQT->isUnionType())
3621 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003622
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003623 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003624 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003625 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003626 continue;
3627 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003628
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003629 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003630 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003631 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003632 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003633 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003634 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003635 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3636 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003637 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003638 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003639 FQT = CArray->getElementType();
3640 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003641
3642 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003643 "layout for array of unions not supported");
3644 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003645 int OldIndex = IvarsInfo.size() - 1;
3646 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003647
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003648 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003649 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003650 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003651
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003652 // Replicate layout information for each array element. Note that
3653 // one element is already done.
3654 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003655 for (int FirstIndex = IvarsInfo.size() - 1,
3656 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003657 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003658 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3659 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3660 IvarsInfo[i].ivar_size));
3661 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3662 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3663 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003664 }
3665 continue;
3666 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003667 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003668 // At this point, we are done with Record/Union and array there of.
3669 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003670 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003671
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003672 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003673 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3674 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003675 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003676 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003677 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003678 MaxUnionIvarSize = UnionIvarSize;
3679 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003680 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003681 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003682 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003683 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003684 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003685 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003686 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003687 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3688 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003689 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003690 // FIXME: Why the asymmetry? We divide by word size in bits on other
3691 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003692 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003693 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003694 MaxSkippedUnionIvarSize = UnionIvarSize;
3695 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003696 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003697 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003698 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003699 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003700 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003701 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003702 }
3703 }
3704 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003705
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003706 if (LastFieldBitfield) {
3707 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003708 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3709 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003710 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003711 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003712 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003713 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3714 + ((BitFieldSize % ByteSizeInBits) != 0);
3715 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003716 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003717
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003718 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003719 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003720 MaxUnionIvarSize));
3721 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003722 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003723 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003724}
3725
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003726/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3727/// the computations and returning the layout bitmap (for ivar or blocks) in
3728/// the given argument BitMap string container. Routine reads
3729/// two containers, IvarsInfo and SkipIvars which are assumed to be
3730/// filled already by the caller.
3731llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003732 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003733 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003734
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003735 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003736 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003737 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003738 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003739 if (IvarsInfo[0].ivar_bytepos == 0) {
3740 WordsToSkip = 0;
3741 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003742 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003743 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3744 WordsToScan = IvarsInfo[0].ivar_size;
3745 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003746 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003747 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003748 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003749 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003750 // consecutive 'scanned' object pointers.
3751 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003752 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003753 // Skip over 'gc'able object pointer which lay over each other.
3754 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3755 continue;
3756 // Must skip over 1 or more words. We save current skip/scan values
3757 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003758 SKIP_SCAN SkScan;
3759 SkScan.skip = WordsToSkip;
3760 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003761 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003762
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003763 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003764 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3765 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003766 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003767 WordsToSkip = 0;
3768 WordsToScan = IvarsInfo[i].ivar_size;
3769 }
3770 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003771 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003772 SKIP_SCAN SkScan;
3773 SkScan.skip = WordsToSkip;
3774 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003775 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003776 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003777
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003778 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003779 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003780 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003781 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003782 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003783 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003784 IvarsInfo[LastIndex].ivar_bytepos +
3785 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003786 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003787 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003788 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003789 SKIP_SCAN SkScan;
3790 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3791 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003792 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003793 }
3794 }
3795 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3796 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003797 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003798 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003799 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3800 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3801 // 0xM0 followed by 0x0N detected.
3802 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3803 for (int j = i+1; j < SkipScan; j++)
3804 SkipScanIvars[j] = SkipScanIvars[j+1];
3805 --SkipScan;
3806 }
3807 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003808
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003809 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003810 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003811 unsigned char byte;
3812 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3813 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3814 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3815 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003816
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003817 // first skip big.
3818 for (unsigned int ix = 0; ix < skip_big; ix++)
3819 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003820
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003821 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003822 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003823 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003824 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003825 byte |= 0xf;
3826 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003827 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003828 byte |= scan_small;
3829 scan_small = 0;
3830 }
3831 BitMap += byte;
3832 }
3833 // next scan big
3834 for (unsigned int ix = 0; ix < scan_big; ix++)
3835 BitMap += (unsigned char)(0x0f);
3836 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003837 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003838 byte = scan_small;
3839 BitMap += byte;
3840 }
3841 }
3842 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003843 unsigned char zero = 0;
3844 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003845
3846 llvm::GlobalVariable * Entry =
3847 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3848 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3849 "__TEXT,__cstring,cstring_literals",
3850 1, true);
3851 return getConstantGEP(VMContext, Entry, 0, 0);
3852}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003853
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003854/// BuildIvarLayout - Builds ivar layout bitmap for the class
3855/// implementation for the __strong or __weak case.
3856/// The layout map displays which words in ivar list must be skipped
3857/// and which must be scanned by GC (see below). String is built of bytes.
3858/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3859/// of words to skip and right nibble is count of words to scan. So, each
3860/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3861/// represented by a 0x00 byte which also ends the string.
3862/// 1. when ForStrongLayout is true, following ivars are scanned:
3863/// - id, Class
3864/// - object *
3865/// - __strong anything
3866///
3867/// 2. When ForStrongLayout is false, following ivars are scanned:
3868/// - __weak anything
3869///
3870llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3871 const ObjCImplementationDecl *OMD,
3872 bool ForStrongLayout) {
3873 bool hasUnion = false;
3874
3875 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3876 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3877 return llvm::Constant::getNullValue(PtrTy);
3878
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003879 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003880 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003881 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003882
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003883 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003884 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3885 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3886
3887 if (RecFields.empty())
3888 return llvm::Constant::getNullValue(PtrTy);
3889
3890 SkipIvars.clear();
3891 IvarsInfo.clear();
3892
3893 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3894 if (IvarsInfo.empty())
3895 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003896 // Sort on byte position in case we encounterred a union nested in
3897 // the ivar list.
3898 if (hasUnion && !IvarsInfo.empty())
3899 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3900 if (hasUnion && !SkipIvars.empty())
3901 std::sort(SkipIvars.begin(), SkipIvars.end());
3902
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003903 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003904 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003905
3906 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003907 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003908 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003909 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003910 const unsigned char *s = (unsigned char*)BitMap.c_str();
3911 for (unsigned i = 0; i < BitMap.size(); i++)
3912 if (!(s[i] & 0xf0))
3913 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3914 else
3915 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3916 printf("\n");
3917 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003918 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003919}
3920
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003921llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003922 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3923
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003924 // FIXME: Avoid std::string copying.
3925 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003926 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003927 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003928 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003929 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003930
Owen Anderson170229f2009-07-14 23:10:40 +00003931 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003932}
3933
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003934// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003935llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003936 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3937}
3938
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003939llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003940 std::string TypeStr;
3941 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3942
3943 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003944
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003945 if (!Entry)
3946 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003947 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003948 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003949 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003950
Owen Anderson170229f2009-07-14 23:10:40 +00003951 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003952}
3953
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003954llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003955 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003956 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3957 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003958
3959 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3960
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003961 if (!Entry)
3962 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003963 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003964 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003965 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003966
Owen Anderson170229f2009-07-14 23:10:40 +00003967 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003968}
3969
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003970// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003971llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003972 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003973
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003974 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003975 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003976 llvm::ConstantArray::get(VMContext,
3977 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003978 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003979 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003980
Owen Anderson170229f2009-07-14 23:10:40 +00003981 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003982}
3983
3984// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003985// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003986llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003987CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3988 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003989 std::string TypeStr;
3990 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003991 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3992}
3993
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003994void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003995 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003996 llvm::SmallVectorImpl<char> &Name) {
3997 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003998 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003999 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4000 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004001 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004002 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004003 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004004 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004005}
4006
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004007void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004008 EmitModuleInfo();
4009
Daniel Dunbarc475d422008-10-29 22:36:39 +00004010 // Emit the dummy bodies for any protocols which were referenced but
4011 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004012 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004013 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4014 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004015 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004016
Daniel Dunbarc475d422008-10-29 22:36:39 +00004017 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004018 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004019 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004020 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004021 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004022 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004023 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004024 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004025 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004026 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004027 }
4028
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004029 // Add assembler directives to add lazy undefined symbol references
4030 // for classes which are referenced but not defined. This is
4031 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004032 //
4033 // FIXME: It would be nice if we had an LLVM construct for this.
4034 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4035 llvm::SmallString<256> Asm;
4036 Asm += CGM.getModule().getModuleInlineAsm();
4037 if (!Asm.empty() && Asm.back() != '\n')
4038 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004039
Daniel Dunbard027a922009-09-07 00:20:42 +00004040 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004041 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4042 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004043 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4044 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004045 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004046 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004047 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004048 }
4049
4050 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4051 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4052 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4053 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004054
Daniel Dunbard027a922009-09-07 00:20:42 +00004055 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004056 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004057}
4058
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004059CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004060 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004061 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004062 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004063 ObjCABI = 2;
4064}
4065
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004066/* *** */
4067
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004068ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004069 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004070 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4071 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004072
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004073 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004074 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004075 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004076 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004077 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004078
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004079 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004080 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004081 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004082
Mike Stump18bb9282009-05-16 07:57:57 +00004083 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4084 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004085 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004086 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004087
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004088 // I'm not sure I like this. The implicit coordination is a bit
4089 // gross. We should solve this in a reasonable fashion because this
4090 // is a pretty common task (match some runtime data structure with
4091 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004092
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004093 // FIXME: This is leaked.
4094 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004096 // struct _objc_super {
4097 // id self;
4098 // Class cls;
4099 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004100 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004101 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004102 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004103 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004104 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004105 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004106 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004107 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004108 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004109
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004110 SuperCTy = Ctx.getTagDeclType(RD);
4111 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004112
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004113 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004114 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4115
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004116 // struct _prop_t {
4117 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004118 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004119 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004120 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004121 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004122 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004123
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004124 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004125 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004126 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004127 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004128 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004129 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004130 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004131 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004132 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004133 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004134 PropertyListTy);
4135 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004136 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004137
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004138 // struct _objc_method {
4139 // SEL _cmd;
4140 // char *method_type;
4141 // char *_imp;
4142 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004143 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004144 Int8PtrTy,
4145 Int8PtrTy,
4146 NULL);
4147 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004149 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004150 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004151 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004152 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004153}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004154
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004156 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004157 // struct _objc_method_description {
4158 // SEL name;
4159 // char *types;
4160 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004162 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004163 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004164 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004165 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004166 MethodDescriptionTy);
4167
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004168 // struct _objc_method_description_list {
4169 // int count;
4170 // struct _objc_method_description[1];
4171 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004172 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004173 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004174 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004175 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004176 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004177 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004178
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004179 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004180 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004181 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004182
Daniel Dunbarb036db82008-08-13 03:21:16 +00004183 // Protocol description structures
4184
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004185 // struct _objc_protocol_extension {
4186 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4187 // struct _objc_method_description_list *optional_instance_methods;
4188 // struct _objc_method_description_list *optional_class_methods;
4189 // struct _objc_property_list *instance_properties;
4190 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004191 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004192 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004193 MethodDescriptionListPtrTy,
4194 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004195 PropertyListPtrTy,
4196 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004197 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004198 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004199
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004200 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004201 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004202
Daniel Dunbarc475d422008-10-29 22:36:39 +00004203 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004204
Owen Andersonc36edfe2009-08-13 23:27:53 +00004205 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4206 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004207
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004209 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004210 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004211 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004212 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004213 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004214 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4215
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004216 // struct _objc_protocol {
4217 // struct _objc_protocol_extension *isa;
4218 // char *protocol_name;
4219 // struct _objc_protocol **_objc_protocol_list;
4220 // struct _objc_method_description_list *instance_methods;
4221 // struct _objc_method_description_list *class_methods;
4222 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004223 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004224 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004225 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004226 MethodDescriptionListPtrTy,
4227 MethodDescriptionListPtrTy,
4228 NULL);
4229 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4230
4231 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004232 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004233 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004234 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004235 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004236
4237 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004238 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004239 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004240
4241 // Class description structures
4242
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004243 // struct _objc_ivar {
4244 // char *ivar_name;
4245 // char *ivar_type;
4246 // int ivar_offset;
4247 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004248 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004249 Int8PtrTy,
4250 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004251 NULL);
4252 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4253
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004254 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004255 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004256 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004257 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004258
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004259 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004260 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004261 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004262 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004263
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004264 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004265 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004266 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004267 Int8PtrTy,
4268 PropertyListPtrTy,
4269 NULL);
4270 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004271 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004272
Owen Andersonc36edfe2009-08-13 23:27:53 +00004273 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004274
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004275 // struct _objc_class {
4276 // Class isa;
4277 // Class super_class;
4278 // char *name;
4279 // long version;
4280 // long info;
4281 // long instance_size;
4282 // struct _objc_ivar_list *ivars;
4283 // struct _objc_method_list *methods;
4284 // struct _objc_cache *cache;
4285 // struct _objc_protocol_list *protocols;
4286 // char *ivar_layout;
4287 // struct _objc_class_ext *ext;
4288 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004289 T = llvm::StructType::get(VMContext,
4290 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004291 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004292 Int8PtrTy,
4293 LongTy,
4294 LongTy,
4295 LongTy,
4296 IvarListPtrTy,
4297 MethodListPtrTy,
4298 CachePtrTy,
4299 ProtocolListPtrTy,
4300 Int8PtrTy,
4301 ClassExtensionPtrTy,
4302 NULL);
4303 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004304
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004305 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4306 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004307 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004308
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004309 // struct _objc_category {
4310 // char *category_name;
4311 // char *class_name;
4312 // struct _objc_method_list *instance_method;
4313 // struct _objc_method_list *class_method;
4314 // uint32_t size; // sizeof(struct _objc_category)
4315 // struct _objc_property_list *instance_properties;// category's @property
4316 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004317 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004318 Int8PtrTy,
4319 MethodListPtrTy,
4320 MethodListPtrTy,
4321 ProtocolListPtrTy,
4322 IntTy,
4323 PropertyListPtrTy,
4324 NULL);
4325 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4326
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004327 // Global metadata structures
4328
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004329 // struct _objc_symtab {
4330 // long sel_ref_cnt;
4331 // SEL *refs;
4332 // short cls_def_cnt;
4333 // short cat_def_cnt;
4334 // char *defs[cls_def_cnt + cat_def_cnt];
4335 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004336 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004337 SelectorPtrTy,
4338 ShortTy,
4339 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004340 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004341 NULL);
4342 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004343 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004344
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004345 // struct _objc_module {
4346 // long version;
4347 // long size; // sizeof(struct _objc_module)
4348 // char *name;
4349 // struct _objc_symtab* symtab;
4350 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004351 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004352 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004353 LongTy,
4354 Int8PtrTy,
4355 SymtabPtrTy,
4356 NULL);
4357 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004358
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004359
Mike Stump18bb9282009-05-16 07:57:57 +00004360 // FIXME: This is the size of the setjmp buffer and should be target
4361 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004362 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004363
Anders Carlsson9ff22482008-09-09 10:10:21 +00004364 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004365 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004366 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367
4368 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004369 llvm::StructType::get(VMContext,
4370 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4371 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004372 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004373 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004374 ExceptionDataTy);
4375
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004376}
4377
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004378ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004379 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004380 // struct _method_list_t {
4381 // uint32_t entsize; // sizeof(struct _objc_method)
4382 // uint32_t method_count;
4383 // struct _objc_method method_list[method_count];
4384 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004385 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004386 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004387 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004388 NULL);
4389 CGM.getModule().addTypeName("struct.__method_list_t",
4390 MethodListnfABITy);
4391 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004392 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004393
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004394 // struct _protocol_t {
4395 // id isa; // NULL
4396 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004397 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004398 // const struct method_list_t * const instance_methods;
4399 // const struct method_list_t * const class_methods;
4400 // const struct method_list_t *optionalInstanceMethods;
4401 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004402 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004403 // const uint32_t size; // sizeof(struct _protocol_t)
4404 // const uint32_t flags; // = 0
4405 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004406
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004407 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004408 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004409
Owen Anderson758428f2009-08-05 23:18:46 +00004410 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004411 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004412 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004413 ProtocolListTyHolder),
4414 MethodListnfABIPtrTy,
4415 MethodListnfABIPtrTy,
4416 MethodListnfABIPtrTy,
4417 MethodListnfABIPtrTy,
4418 PropertyListPtrTy,
4419 IntTy,
4420 IntTy,
4421 NULL);
4422 CGM.getModule().addTypeName("struct._protocol_t",
4423 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004424
4425 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004426 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004427
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004428 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004429 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004430 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004431 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004432 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004433 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004434 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004435 NULL);
4436 CGM.getModule().addTypeName("struct._objc_protocol_list",
4437 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004438 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439 ProtocolListnfABITy);
4440
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004441 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004442 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004443
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004444 // struct _ivar_t {
4445 // unsigned long int *offset; // pointer to ivar offset location
4446 // char *name;
4447 // char *type;
4448 // uint32_t alignment;
4449 // uint32_t size;
4450 // }
Mike Stump11289f42009-09-09 15:08:12 +00004451 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004452 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004453 Int8PtrTy,
4454 Int8PtrTy,
4455 IntTy,
4456 IntTy,
4457 NULL);
4458 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004459
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004460 // struct _ivar_list_t {
4461 // uint32 entsize; // sizeof(struct _ivar_t)
4462 // uint32 count;
4463 // struct _iver_t list[count];
4464 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004465 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004466 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004467 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004468 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004469 NULL);
4470 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004471
Owen Anderson9793f0e2009-07-29 22:16:19 +00004472 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004473
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004474 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004475 // uint32_t const flags;
4476 // uint32_t const instanceStart;
4477 // uint32_t const instanceSize;
4478 // uint32_t const reserved; // only when building for 64bit targets
4479 // const uint8_t * const ivarLayout;
4480 // const char *const name;
4481 // const struct _method_list_t * const baseMethods;
4482 // const struct _objc_protocol_list *const baseProtocols;
4483 // const struct _ivar_list_t *const ivars;
4484 // const uint8_t * const weakIvarLayout;
4485 // const struct _prop_list_t * const properties;
4486 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004487
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004488 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004489 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004490 IntTy,
4491 IntTy,
4492 Int8PtrTy,
4493 Int8PtrTy,
4494 MethodListnfABIPtrTy,
4495 ProtocolListnfABIPtrTy,
4496 IvarListnfABIPtrTy,
4497 Int8PtrTy,
4498 PropertyListPtrTy,
4499 NULL);
4500 CGM.getModule().addTypeName("struct._class_ro_t",
4501 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004502
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004503 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4504 std::vector<const llvm::Type*> Params;
4505 Params.push_back(ObjectPtrTy);
4506 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004507 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004508 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4509
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004510 // struct _class_t {
4511 // struct _class_t *isa;
4512 // struct _class_t * const superclass;
4513 // void *cache;
4514 // IMP *vtable;
4515 // struct class_ro_t *ro;
4516 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517
Owen Andersonc36edfe2009-08-13 23:27:53 +00004518 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004519 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004520 llvm::StructType::get(VMContext,
4521 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004522 llvm::PointerType::getUnqual(ClassTyHolder),
4523 CachePtrTy,
4524 llvm::PointerType::getUnqual(ImpnfABITy),
4525 llvm::PointerType::getUnqual(ClassRonfABITy),
4526 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004527 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4528
4529 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004530 ClassnfABITy);
4531
Fariborz Jahanian71394042009-01-23 23:53:38 +00004532 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004533 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004534
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004535 // struct _category_t {
4536 // const char * const name;
4537 // struct _class_t *const cls;
4538 // const struct _method_list_t * const instance_methods;
4539 // const struct _method_list_t * const class_methods;
4540 // const struct _protocol_list_t * const protocols;
4541 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004542 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004543 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004544 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004545 MethodListnfABIPtrTy,
4546 MethodListnfABIPtrTy,
4547 ProtocolListnfABIPtrTy,
4548 PropertyListPtrTy,
4549 NULL);
4550 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004551
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004552 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004553 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4554 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004555
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004556 // MessageRefTy - LLVM for:
4557 // struct _message_ref_t {
4558 // IMP messenger;
4559 // SEL name;
4560 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004561
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004562 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004563 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004564 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004565 SourceLocation(),
4566 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004567 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004568 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004569 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004570 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004571 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004573 MessageRefCTy = Ctx.getTagDeclType(RD);
4574 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4575 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004576
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004577 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004578 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004579
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004580 // SuperMessageRefTy - LLVM for:
4581 // struct _super_message_ref_t {
4582 // SUPER_IMP messenger;
4583 // SEL name;
4584 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004585 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004586 SelectorPtrTy,
4587 NULL);
4588 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004589
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004590 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004591 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4592
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004593
4594 // struct objc_typeinfo {
4595 // const void** vtable; // objc_ehtype_vtable + 2
4596 // const char* name; // c++ typeinfo string
4597 // Class cls;
4598 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004599 EHTypeTy = llvm::StructType::get(VMContext,
4600 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004601 Int8PtrTy,
4602 ClassnfABIPtrTy,
4603 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004604 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004605 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004606}
4607
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004608llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004609 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004610
Fariborz Jahanian71394042009-01-23 23:53:38 +00004611 return NULL;
4612}
4613
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004614void CGObjCNonFragileABIMac::AddModuleClassList(const
4615 std::vector<llvm::GlobalValue*>
4616 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004617 const char *SymbolName,
4618 const char *SectionName) {
4619 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004620
Daniel Dunbar19573e72009-05-15 21:48:48 +00004621 if (!NumClasses)
4622 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004623
Daniel Dunbar19573e72009-05-15 21:48:48 +00004624 std::vector<llvm::Constant*> Symbols(NumClasses);
4625 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004626 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004627 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004628 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004629 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004630 NumClasses),
4631 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004632
Daniel Dunbar19573e72009-05-15 21:48:48 +00004633 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004634 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004635 llvm::GlobalValue::InternalLinkage,
4636 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004637 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004638 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004639 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004640 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004641}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004642
Fariborz Jahanian71394042009-01-23 23:53:38 +00004643void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4644 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004645
Daniel Dunbar19573e72009-05-15 21:48:48 +00004646 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004647 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004648 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004649 "\01L_OBJC_LABEL_CLASS_$",
4650 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004651
Fariborz Jahanian67260552009-11-17 21:37:35 +00004652 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4653 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4654 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4655 continue;
4656 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004657 }
4658
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004659 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4660 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4661 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4662 continue;
4663 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4664 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004665
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004666 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004667 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4668 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004669
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004670 // Build list of all implemented category addresses in array
4671 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004672 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004673 "\01L_OBJC_LABEL_CATEGORY_$",
4674 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004675 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004676 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4677 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004678
Daniel Dunbar5e639272010-04-25 20:39:01 +00004679 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004680}
4681
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004682/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004683/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004685/// message dispatch call for all the rest.
4686///
4687bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004688 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4689 default:
4690 assert(0 && "Invalid dispatch method!");
4691 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004692 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004693 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004694 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004695 case CodeGenOptions::Mixed:
4696 break;
4697 }
4698
4699 // If so, see whether this selector is in the white-list of things which must
4700 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004701 if (NonLegacyDispatchMethods.empty()) {
4702 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4703 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4704 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4705 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4706 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4707 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4708 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4709 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4710 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4711 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004712
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004713 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4714 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4715 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4716 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4717 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4718 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4719 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4720 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004721 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004722 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004723 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4724 &CGM.getContext().Idents.get("objects"),
4725 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004726 };
4727 NonLegacyDispatchMethods.insert(
4728 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004729 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004730
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004731 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004732}
4733
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004734// Metadata flags
4735enum MetaDataDlags {
4736 CLS = 0x0,
4737 CLS_META = 0x1,
4738 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004739 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004740 CLS_EXCEPTION = 0x20
4741};
4742/// BuildClassRoTInitializer - generate meta-data for:
4743/// struct _class_ro_t {
4744/// uint32_t const flags;
4745/// uint32_t const instanceStart;
4746/// uint32_t const instanceSize;
4747/// uint32_t const reserved; // only when building for 64bit targets
4748/// const uint8_t * const ivarLayout;
4749/// const char *const name;
4750/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004751/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004752/// const struct _ivar_list_t *const ivars;
4753/// const uint8_t * const weakIvarLayout;
4754/// const struct _prop_list_t * const properties;
4755/// }
4756///
4757llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004758 unsigned flags,
4759 unsigned InstanceStart,
4760 unsigned InstanceSize,
4761 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004762 std::string ClassName = ID->getNameAsString();
4763 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004764 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4765 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4766 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004767 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004768 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4769 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004770 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004771 // const struct _method_list_t * const baseMethods;
4772 std::vector<llvm::Constant*> Methods;
4773 std::string MethodListName("\01l_OBJC_$_");
4774 if (flags & CLS_META) {
4775 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004776 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004777 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004778 // Class methods should always be defined.
4779 Methods.push_back(GetMethodConstant(*i));
4780 }
4781 } else {
4782 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004783 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004784 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004785 // Instance methods should always be defined.
4786 Methods.push_back(GetMethodConstant(*i));
4787 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004788 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004789 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004790 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004791
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004792 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4793 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004795 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4796 if (llvm::Constant *C = GetMethodConstant(MD))
4797 Methods.push_back(C);
4798 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4799 if (llvm::Constant *C = GetMethodConstant(MD))
4800 Methods.push_back(C);
4801 }
4802 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004803 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004804 Values[ 5] = EmitMethodList(MethodListName,
4805 "__DATA, __objc_const", Methods);
4806
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004807 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4808 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004809 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004810 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004811 OID->all_referenced_protocol_begin(),
4812 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004813
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004814 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004815 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004816 else
4817 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004818 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4819 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004820 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004821 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004822 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004823 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4824 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004825 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004826 Values);
4827 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004828 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4829 llvm::GlobalValue::InternalLinkage,
4830 Init,
4831 (flags & CLS_META) ?
4832 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4833 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004834 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004835 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004836 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004837 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004838
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004839}
4840
4841/// BuildClassMetaData - This routine defines that to-level meta-data
4842/// for the given ClassName for:
4843/// struct _class_t {
4844/// struct _class_t *isa;
4845/// struct _class_t * const superclass;
4846/// void *cache;
4847/// IMP *vtable;
4848/// struct class_ro_t *ro;
4849/// }
4850///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004851llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004852 std::string &ClassName,
4853 llvm::Constant *IsAGV,
4854 llvm::Constant *SuperClassGV,
4855 llvm::Constant *ClassRoGV,
4856 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004857 std::vector<llvm::Constant*> Values(5);
4858 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004859 Values[1] = SuperClassGV;
4860 if (!Values[1])
4861 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004862 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4863 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4864 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004865 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004866 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004867 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4868 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004869 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004870 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004871 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004872 if (HiddenVisibility)
4873 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004874 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004875}
4876
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004877bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004878CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004879 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004880}
4881
Daniel Dunbar961202372009-05-03 12:57:56 +00004882void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004883 uint32_t &InstanceStart,
4884 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004885 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004886 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004887
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004888 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004889 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004890
4891 // If there are no fields, the start is the same as the end.
4892 if (!RL.getFieldCount())
4893 InstanceStart = InstanceSize;
4894 else
4895 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004896}
4897
Fariborz Jahanian71394042009-01-23 23:53:38 +00004898void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4899 std::string ClassName = ID->getNameAsString();
4900 if (!ObjCEmptyCacheVar) {
4901 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004902 CGM.getModule(),
4903 ObjCTypes.CacheTy,
4904 false,
4905 llvm::GlobalValue::ExternalLinkage,
4906 0,
4907 "_objc_empty_cache");
4908
Fariborz Jahanian71394042009-01-23 23:53:38 +00004909 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004910 CGM.getModule(),
4911 ObjCTypes.ImpnfABITy,
4912 false,
4913 llvm::GlobalValue::ExternalLinkage,
4914 0,
4915 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004916 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004917 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004918 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004919 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004920 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004921 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004922 uint32_t InstanceSize = InstanceStart;
4923 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004924 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4925 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004926
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004927 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928
4929 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004930 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004931 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004932 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004933 if (ID->getNumIvarInitializers())
4934 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004935 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004936 // class is root
4937 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004938 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004939 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004940 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004941 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004942 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4943 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4944 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004945 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004946 if (Root->hasAttr<WeakImportAttr>())
4947 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004948 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004949 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004950 ObjCMetaClassName +
4951 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004952 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004953 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4954 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004955 }
4956 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4957 InstanceStart,
4958 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004959 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004960 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004961 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4962 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004963 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004964
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004965 // Metadata for the class
4966 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004967 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004968 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004969 if (ID->getNumIvarInitializers())
4970 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004971
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004972 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004973 flags |= CLS_EXCEPTION;
4974
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004975 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004976 flags |= CLS_ROOT;
4977 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004978 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004979 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004980 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004981 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004982 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004983 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4984 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004985 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004986 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004987 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004988 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004989 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004990 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004991
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004992 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004993 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004994 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4995 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004996 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004997
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004998 // Determine if this class is also "non-lazy".
4999 if (ImplementationIsNonLazy(ID))
5000 DefinedNonLazyClasses.push_back(ClassMD);
5001
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005002 // Force the definition of the EHType if necessary.
5003 if (flags & CLS_EXCEPTION)
5004 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005005}
5006
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005007/// GenerateProtocolRef - This routine is called to generate code for
5008/// a protocol reference expression; as in:
5009/// @code
5010/// @protocol(Proto1);
5011/// @endcode
5012/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5013/// which will hold address of the protocol meta-data.
5014///
5015llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016 const ObjCProtocolDecl *PD) {
5017
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005018 // This routine is called for @protocol only. So, we must build definition
5019 // of protocol's meta-data (not a reference to it!)
5020 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005021 llvm::Constant *Init =
5022 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5023 ObjCTypes.ExternalProtocolPtrTy);
5024
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005025 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005026 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005027
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005028 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5029 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005030 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005031 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005032 CGM.getModule(),
5033 Init->getType(), false,
5034 llvm::GlobalValue::WeakAnyLinkage,
5035 Init,
5036 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005037 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5038 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005039 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005040 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005041}
5042
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005043/// GenerateCategory - Build metadata for a category implementation.
5044/// struct _category_t {
5045/// const char * const name;
5046/// struct _class_t *const cls;
5047/// const struct _method_list_t * const instance_methods;
5048/// const struct _method_list_t * const class_methods;
5049/// const struct _protocol_list_t * const protocols;
5050/// const struct _prop_list_t * const properties;
5051/// }
5052///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005053void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005054 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005055 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005056 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5057 "_$_" + OCD->getNameAsString());
5058 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005059 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005060
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005061 std::vector<llvm::Constant*> Values(6);
5062 Values[0] = GetClassName(OCD->getIdentifier());
5063 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005064 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005065 if (Interface->hasAttr<WeakImportAttr>())
5066 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5067
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005068 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005069 std::vector<llvm::Constant*> Methods;
5070 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005071 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005072 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073
5074 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005075 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005076 // Instance methods should always be defined.
5077 Methods.push_back(GetMethodConstant(*i));
5078 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079
5080 Values[2] = EmitMethodList(MethodListName,
5081 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005082 Methods);
5083
5084 MethodListName = Prefix;
5085 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5086 OCD->getNameAsString();
5087 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005089 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005090 // Class methods should always be defined.
5091 Methods.push_back(GetMethodConstant(*i));
5092 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093
5094 Values[3] = EmitMethodList(MethodListName,
5095 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005096 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005097 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005098 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005099 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005100 llvm::SmallString<256> ExtName;
5101 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5102 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005103 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005104 + Interface->getName() + "_$_"
5105 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005106 Category->protocol_begin(),
5107 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005108 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5109 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005110 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005111 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5112 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005113 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005114
5115 llvm::Constant *Init =
5116 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005117 Values);
5118 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005119 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005120 false,
5121 llvm::GlobalValue::InternalLinkage,
5122 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005123 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005124 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005125 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005126 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005127 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005128 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005129
5130 // Determine if this category is also "non-lazy".
5131 if (ImplementationIsNonLazy(OCD))
5132 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005133}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005134
5135/// GetMethodConstant - Return a struct objc_method constant for the
5136/// given method if it has been defined. The result is null if the
5137/// method has not been defined. The return value has type MethodPtrTy.
5138llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005139 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005140 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005141 if (!Fn)
5142 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005143
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005144 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005145 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005146 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005147 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005148 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005149 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005150 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005151}
5152
5153/// EmitMethodList - Build meta-data for method declarations
5154/// struct _method_list_t {
5155/// uint32_t entsize; // sizeof(struct _objc_method)
5156/// uint32_t method_count;
5157/// struct _objc_method method_list[method_count];
5158/// }
5159///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005160llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5161 const char *Section,
5162 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005163 // Return null for empty list.
5164 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005165 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005166
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005167 std::vector<llvm::Constant*> Values(3);
5168 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005169 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005170 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005171 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005172 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005173 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005174 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005175 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005176 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005177
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005178 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005179 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005180 llvm::GlobalValue::InternalLinkage,
5181 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005182 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005183 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005184 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005185 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005186 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005187 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005188 ObjCTypes.MethodListnfABIPtrTy);
5189}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005190
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005191/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5192/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005193llvm::GlobalVariable *
5194CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5195 const ObjCIvarDecl *Ivar) {
5196 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005197 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005198 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005199 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005200 CGM.getModule().getGlobalVariable(Name);
5201 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005202 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005203 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005204 false,
5205 llvm::GlobalValue::ExternalLinkage,
5206 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005207 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005208 return IvarOffsetGV;
5209}
5210
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005211llvm::Constant *
5212CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5213 const ObjCIvarDecl *Ivar,
5214 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005215 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005216 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005217 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005218 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005219 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005220
Mike Stump18bb9282009-05-16 07:57:57 +00005221 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5222 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005223 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5224 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5225 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005226 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005227 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005228 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005229 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005230 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005231}
5232
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005233/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005234/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005235/// IvarListnfABIPtrTy.
5236/// struct _ivar_t {
5237/// unsigned long int *offset; // pointer to ivar offset location
5238/// char *name;
5239/// char *type;
5240/// uint32_t alignment;
5241/// uint32_t size;
5242/// }
5243/// struct _ivar_list_t {
5244/// uint32 entsize; // sizeof(struct _ivar_t)
5245/// uint32 count;
5246/// struct _iver_t list[count];
5247/// }
5248///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005249
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005250llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005251 const ObjCImplementationDecl *ID) {
5252
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005253 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005255 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5256 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005257
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005258 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005259
Daniel Dunbarae032262009-04-20 00:33:43 +00005260 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005261 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005262 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005263
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005264 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5265 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005266 // Ignore unnamed bit-fields.
5267 if (!IVD->getDeclName())
5268 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005269 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005270 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005271 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5272 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005273 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005274 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005275 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005276 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005277 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005278 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005279 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005280 // NOTE. Size of a bitfield does not match gcc's, because of the
5281 // way bitfields are treated special in each. But I am told that
5282 // 'size' for bitfield ivars is ignored by the runtime so it does
5283 // not matter. If it matters, there is enough info to get the
5284 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005285 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005286 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005287 }
5288 // Return null for empty list.
5289 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005290 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005291 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005292 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005293 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5294 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005295 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005296 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005297 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005298 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005299 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5300 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005301 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005302 llvm::GlobalValue::InternalLinkage,
5303 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005304 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005305 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005306 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005307 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005308
Chris Lattnerf56501c2009-07-17 23:57:13 +00005309 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005310 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005311}
5312
5313llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005314 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005315 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005316
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005317 if (!Entry) {
5318 // We use the initializer as a marker of whether this is a forward
5319 // reference or not. At module finalization we add the empty
5320 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005321 Entry =
5322 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5323 llvm::GlobalValue::ExternalLinkage,
5324 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005325 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005326 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005327 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005328
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005329 return Entry;
5330}
5331
5332/// GetOrEmitProtocol - Generate the protocol meta-data:
5333/// @code
5334/// struct _protocol_t {
5335/// id isa; // NULL
5336/// const char * const protocol_name;
5337/// const struct _protocol_list_t * protocol_list; // super protocols
5338/// const struct method_list_t * const instance_methods;
5339/// const struct method_list_t * const class_methods;
5340/// const struct method_list_t *optionalInstanceMethods;
5341/// const struct method_list_t *optionalClassMethods;
5342/// const struct _prop_list_t * properties;
5343/// const uint32_t size; // sizeof(struct _protocol_t)
5344/// const uint32_t flags; // = 0
5345/// }
5346/// @endcode
5347///
5348
5349llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005350 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005351 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005352
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005353 // Early exit if a defining object has already been generated.
5354 if (Entry && Entry->hasInitializer())
5355 return Entry;
5356
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005357 // Construct method lists.
5358 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5359 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005360 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005361 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005362 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005363 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005364 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5365 OptInstanceMethods.push_back(C);
5366 } else {
5367 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005368 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005369 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370
5371 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005372 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005373 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005374 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005375 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5376 OptClassMethods.push_back(C);
5377 } else {
5378 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005379 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005380 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005381
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005382 std::vector<llvm::Constant*> Values(10);
5383 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005384 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005385 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005386 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5387 PD->protocol_begin(),
5388 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005389
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005390 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005391 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005392 "__DATA, __objc_const",
5393 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005394 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005395 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005396 "__DATA, __objc_const",
5397 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005398 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005399 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 "__DATA, __objc_const",
5401 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005402 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005403 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005404 "__DATA, __objc_const",
5405 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005406 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005407 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005408 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005409 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005410 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005411 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005412 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005413 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005414
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005415 if (Entry) {
5416 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005417 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005418 Entry->setInitializer(Init);
5419 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005420 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005421 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5422 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5423 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005424 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005425 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005426 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005427 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005428 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005429 CGM.AddUsedGlobal(Entry);
5430
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005431 // Use this protocol meta-data to build protocol list table in section
5432 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005433 llvm::GlobalVariable *PTGV =
5434 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5435 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5436 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005437 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005438 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005439 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005440 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005441 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005442 return Entry;
5443}
5444
5445/// EmitProtocolList - Generate protocol list meta-data:
5446/// @code
5447/// struct _protocol_list_t {
5448/// long protocol_count; // Note, this is 32/64 bit
5449/// struct _protocol_t[protocol_count];
5450/// }
5451/// @endcode
5452///
5453llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005454CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5455 ObjCProtocolDecl::protocol_iterator begin,
5456 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005457 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005458
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005460 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005461 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005462
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005463 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005464 llvm::SmallString<256> TmpName;
5465 Name.toVector(TmpName);
5466 llvm::GlobalVariable *GV =
5467 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005468 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005469 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005470
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005471 for (; begin != end; ++begin)
5472 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5473
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005474 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005475 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005476 ObjCTypes.ProtocolnfABIPtrTy));
5477
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005478 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005479 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005480 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005481 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005482 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005483 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005484 ProtocolRefs.size()),
5485 ProtocolRefs);
5486
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005487 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005488 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489 llvm::GlobalValue::InternalLinkage,
5490 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005491 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005492 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005493 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005494 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005495 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005496 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005497 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005498}
5499
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005500/// GetMethodDescriptionConstant - This routine build following meta-data:
5501/// struct _objc_method {
5502/// SEL _cmd;
5503/// char *method_type;
5504/// char *_imp;
5505/// }
5506
5507llvm::Constant *
5508CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5509 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005510 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005511 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5512 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005513 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005514 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005515 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005516 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005517}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005518
5519/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5520/// This code gen. amounts to generating code for:
5521/// @code
5522/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5523/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005524///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005525LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005526 CodeGen::CodeGenFunction &CGF,
5527 QualType ObjectTy,
5528 llvm::Value *BaseValue,
5529 const ObjCIvarDecl *Ivar,
5530 unsigned CVRQualifiers) {
5531 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005532 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5533 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005534}
5535
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005536llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005537 CodeGen::CodeGenFunction &CGF,
5538 const ObjCInterfaceDecl *Interface,
5539 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005540 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005541}
5542
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005543CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005545 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005546 QualType ResultType,
5547 Selector Sel,
5548 llvm::Value *Receiver,
5549 QualType Arg0Ty,
5550 bool IsSuper,
5551 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005552 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5553 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005554 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005555 llvm::Value *Arg0 = Receiver;
5556 if (!IsSuper)
5557 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005558
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005559 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005560 // FIXME. This is too much work to get the ABI-specific result type needed to
5561 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005562 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005563 = Types.getFunctionInfo(ResultType, CallArgList(),
5564 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005565 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005566 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005567 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner396639d2010-08-18 16:09:06 +00005568 if (IsSuper) {
5569 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5570 Name += "objc_msgSendSuper2_stret_fixup";
5571 } else {
5572 Fn = ObjCTypes.getMessageSendStretFixupFn();
5573 Name += "objc_msgSend_stret_fixup";
5574 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005575 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5576 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5577 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005578 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005579 if (IsSuper) {
5580 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5581 Name += "objc_msgSendSuper2_fixup";
5582 } else {
5583 Fn = ObjCTypes.getMessageSendFixupFn();
5584 Name += "objc_msgSend_fixup";
5585 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005586 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005587 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005588 Name += '_';
5589 std::string SelName(Sel.getAsString());
5590 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005591 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005592 if (SelName[i] == ':')
5593 SelName[i] = '_';
5594 Name += SelName;
5595 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5596 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005597 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005598 std::vector<llvm::Constant*> Values(2);
5599 Values[0] = Fn;
5600 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005601 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005602 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005603 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005604 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005605 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005606 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005607 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005608 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005609 }
5610 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005611
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005612 CallArgList ActualArgs;
5613 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005614 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005615 ObjCTypes.MessageRefCPtrTy));
5616 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005617 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005618 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005619 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5620 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005621 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005622 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005623 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005624 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005625}
5626
5627/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005628CodeGen::RValue
5629CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005630 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005631 QualType ResultType,
5632 Selector Sel,
5633 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005634 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005635 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005636 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005637 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005638 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5639 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005640 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005641 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005642 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005643 Receiver, CGF.getContext().getObjCIdType(),
5644 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005645}
5646
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005647llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005648CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005649 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5650
Daniel Dunbara6468342009-03-02 05:18:14 +00005651 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005652 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005653 false, llvm::GlobalValue::ExternalLinkage,
5654 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005655 }
5656
5657 return GV;
5658}
5659
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005660llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5661 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005662 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005663
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005664 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005665 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005666 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005667 Entry =
5668 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005669 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005670 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005671 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005672 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005673 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005674 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005675 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005676 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005677 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005678
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005679 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005680}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005681
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005682llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005683CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005684 const ObjCInterfaceDecl *ID) {
5685 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005686
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005687 if (!Entry) {
5688 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5689 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005690 Entry =
5691 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005692 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005693 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005694 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005695 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005696 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005697 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005698 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005699 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005700 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005702 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005703}
5704
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005705/// EmitMetaClassRef - Return a Value * of the address of _class_t
5706/// meta-data
5707///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005708llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5709 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005710 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5711 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005712 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005713
Daniel Dunbar15894b72009-04-07 05:48:37 +00005714 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005715 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005716 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005717 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005718 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005719 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005720 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005721 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005722 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005723 ObjCTypes.ClassnfABIPtrTy));
5724
Daniel Dunbare60aa052009-04-15 19:03:14 +00005725 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005726 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005728 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005729}
5730
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005731/// GetClass - Return a reference to the class for the given interface
5732/// decl.
5733llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5734 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005735 if (ID->hasAttr<WeakImportAttr>()) {
5736 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5737 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5738 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5739 }
5740
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005741 return EmitClassRef(Builder, ID);
5742}
5743
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005744/// Generates a message send where the super is the receiver. This is
5745/// a message send to self with special delivery semantics indicating
5746/// which class's method should be called.
5747CodeGen::RValue
5748CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005749 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005750 QualType ResultType,
5751 Selector Sel,
5752 const ObjCInterfaceDecl *Class,
5753 bool isCategoryImpl,
5754 llvm::Value *Receiver,
5755 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005756 const CodeGen::CallArgList &CallArgs,
5757 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005758 // ...
5759 // Create and init a super structure; this is a (receiver, class)
5760 // pair we will pass to objc_msgSendSuper.
5761 llvm::Value *ObjCSuper =
5762 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005763
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005764 llvm::Value *ReceiverAsObject =
5765 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5766 CGF.Builder.CreateStore(ReceiverAsObject,
5767 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005768
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005769 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005770 llvm::Value *Target;
5771 if (IsClassMessage) {
5772 if (isCategoryImpl) {
5773 // Message sent to "super' in a class method defined in
5774 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005775 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005776 Target = CGF.Builder.CreateStructGEP(Target, 0);
5777 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005778 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005779 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005780 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005781 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005782
Mike Stump18bb9282009-05-16 07:57:57 +00005783 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5784 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005785 const llvm::Type *ClassTy =
5786 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5787 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5788 CGF.Builder.CreateStore(Target,
5789 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005790
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005791 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005792 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5793 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005794 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005795 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005796 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005797 ObjCSuper, ObjCTypes.SuperPtrCTy,
5798 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005799}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005800
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005801llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005802 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005803 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005804
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005805 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005806 llvm::Constant *Casted =
5807 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5808 ObjCTypes.SelectorPtrTy);
5809 Entry =
5810 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5811 llvm::GlobalValue::InternalLinkage,
5812 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005813 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005814 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005815 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005816
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005817 if (lval)
5818 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005819 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005820}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005821/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005822/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005823///
5824void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005825 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005826 llvm::Value *dst,
5827 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005828 const llvm::Type * SrcTy = src->getType();
5829 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005830 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005831 assert(Size <= 8 && "does not support size > 8");
5832 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5833 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005834 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5835 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005836 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5837 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005838 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5839 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005840 return;
5841}
5842
5843/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5844/// objc_assign_strongCast (id src, id *dst)
5845///
5846void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005847 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005848 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005849 const llvm::Type * SrcTy = src->getType();
5850 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005851 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005852 assert(Size <= 8 && "does not support size > 8");
5853 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005854 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005855 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5856 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005857 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5858 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005859 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005860 src, dst, "weakassign");
5861 return;
5862}
5863
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005864void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005865 CodeGen::CodeGenFunction &CGF,
5866 llvm::Value *DestPtr,
5867 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005868 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005869 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5870 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005871 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005872 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005873 return;
5874}
5875
Fariborz Jahanian06292952009-02-16 22:52:32 +00005876/// EmitObjCWeakRead - Code gen for loading value of a __weak
5877/// object: objc_read_weak (id *src)
5878///
5879llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005880 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005881 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005882 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005883 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5884 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005885 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005886 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005887 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005888 return read_weak;
5889}
5890
5891/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5892/// objc_assign_weak (id src, id *dst)
5893///
5894void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005895 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005896 const llvm::Type * SrcTy = src->getType();
5897 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005898 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005899 assert(Size <= 8 && "does not support size > 8");
5900 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5901 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005902 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5903 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005904 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5905 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005906 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005907 src, dst, "weakassign");
5908 return;
5909}
5910
5911/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5912/// objc_assign_global (id src, id *dst)
5913///
5914void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005915 llvm::Value *src, llvm::Value *dst,
5916 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005917 const llvm::Type * SrcTy = src->getType();
5918 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005919 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005920 assert(Size <= 8 && "does not support size > 8");
5921 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5922 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005923 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5924 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005925 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5926 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005927 if (!threadlocal)
5928 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5929 src, dst, "globalassign");
5930 else
5931 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5932 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005933 return;
5934}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005935
John McCallc20acd32010-07-21 00:41:47 +00005936namespace {
John McCallcda666c2010-07-21 07:22:38 +00005937 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00005938 llvm::Value *SyncExitFn;
5939 llvm::Value *SyncArg;
5940 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5941 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5942
5943 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5944 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5945 }
5946 };
5947}
5948
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005949void
John McCallbd309292010-07-06 01:34:17 +00005950CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5951 const ObjCAtSynchronizedStmt &S) {
5952 // Evaluate the lock operand. This should dominate the cleanup.
5953 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005954
John McCallbd309292010-07-06 01:34:17 +00005955 // Acquire the lock.
5956 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5957 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5958 ->setDoesNotThrow();
5959
5960 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00005961 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5962 ObjCTypes.getSyncExitFn(),
5963 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005964
John McCallbd309292010-07-06 01:34:17 +00005965 // Emit the body of the statement.
5966 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005967
John McCallbd309292010-07-06 01:34:17 +00005968 // Pop the lock-release cleanup.
5969 CGF.PopCleanupBlock();
5970}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005971
John McCallbd309292010-07-06 01:34:17 +00005972namespace {
5973 struct CatchHandler {
5974 const VarDecl *Variable;
5975 const Stmt *Body;
5976 llvm::BasicBlock *Block;
5977 llvm::Value *TypeInfo;
5978 };
John McCall5c08ab92010-07-13 22:12:14 +00005979
John McCallcda666c2010-07-21 07:22:38 +00005980 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00005981 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5982 MightThrow(MightThrow), Fn(Fn) {}
5983 bool MightThrow;
5984 llvm::Value *Fn;
5985
5986 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5987 if (!MightThrow) {
5988 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5989 return;
5990 }
5991
5992 CGF.EmitCallOrInvoke(Fn, 0, 0);
5993 }
5994 };
John McCallbd309292010-07-06 01:34:17 +00005995}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005996
John McCall2ca705e2010-07-24 00:37:23 +00005997llvm::Constant *
5998CGObjCNonFragileABIMac::GetEHType(QualType T) {
5999 // There's a particular fixed type info for 'id'.
6000 if (T->isObjCIdType() ||
6001 T->isObjCQualifiedIdType()) {
6002 llvm::Constant *IDEHType =
6003 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6004 if (!IDEHType)
6005 IDEHType =
6006 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6007 false,
6008 llvm::GlobalValue::ExternalLinkage,
6009 0, "OBJC_EHTYPE_id");
6010 return IDEHType;
6011 }
6012
6013 // All other types should be Objective-C interface pointer types.
6014 const ObjCObjectPointerType *PT =
6015 T->getAs<ObjCObjectPointerType>();
6016 assert(PT && "Invalid @catch type.");
6017 const ObjCInterfaceType *IT = PT->getInterfaceType();
6018 assert(IT && "Invalid @catch type.");
6019 return GetInterfaceEHType(IT->getDecl(), false);
6020}
6021
John McCallbd309292010-07-06 01:34:17 +00006022void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6023 const ObjCAtTryStmt &S) {
6024 // Jump destination for falling out of catch bodies.
6025 CodeGenFunction::JumpDest Cont;
6026 if (S.getNumCatchStmts())
6027 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006028
John McCallbd309292010-07-06 01:34:17 +00006029 CodeGenFunction::FinallyInfo FinallyInfo;
6030 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6031 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6032 ObjCTypes.getObjCBeginCatchFn(),
6033 ObjCTypes.getObjCEndCatchFn(),
6034 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006035
John McCallbd309292010-07-06 01:34:17 +00006036 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006037
John McCallbd309292010-07-06 01:34:17 +00006038 // Enter the catch, if there is one.
6039 if (S.getNumCatchStmts()) {
6040 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6041 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006042 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006043
John McCallbd309292010-07-06 01:34:17 +00006044 Handlers.push_back(CatchHandler());
6045 CatchHandler &Handler = Handlers.back();
6046 Handler.Variable = CatchDecl;
6047 Handler.Body = CatchStmt->getCatchBody();
6048 Handler.Block = CGF.createBasicBlock("catch");
6049
6050 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006051 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006052 Handler.TypeInfo = 0; // catch-all
6053 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006054 break;
6055 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006056
John McCall2ca705e2010-07-24 00:37:23 +00006057 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006058 }
John McCallbd309292010-07-06 01:34:17 +00006059
6060 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6061 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6062 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006063 }
John McCallbd309292010-07-06 01:34:17 +00006064
6065 // Emit the try body.
6066 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006067
John McCallbd309292010-07-06 01:34:17 +00006068 // Leave the try.
6069 if (S.getNumCatchStmts())
6070 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006071
John McCallbd309292010-07-06 01:34:17 +00006072 // Remember where we were.
6073 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006074
John McCallbd309292010-07-06 01:34:17 +00006075 // Emit the handlers.
6076 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6077 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006078
John McCallbd309292010-07-06 01:34:17 +00006079 CGF.EmitBlock(Handler.Block);
6080 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006081
John McCallbd309292010-07-06 01:34:17 +00006082 // Enter the catch.
6083 llvm::CallInst *Exn =
6084 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6085 "exn.adjusted");
6086 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006087
John McCallbd309292010-07-06 01:34:17 +00006088 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006089 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006090 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6091 EndCatchMightThrow,
6092 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006093
John McCallbd309292010-07-06 01:34:17 +00006094 // Bind the catch parameter if it exists.
6095 if (const VarDecl *CatchParam = Handler.Variable) {
6096 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6097 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006098
John McCallbd309292010-07-06 01:34:17 +00006099 CGF.EmitLocalBlockVarDecl(*CatchParam);
6100 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006101 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006102
John McCallbd309292010-07-06 01:34:17 +00006103 CGF.ObjCEHValueStack.push_back(Exn);
6104 CGF.EmitStmt(Handler.Body);
6105 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006106
John McCallbd309292010-07-06 01:34:17 +00006107 // Leave the earlier cleanup.
6108 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006109
John McCallbd309292010-07-06 01:34:17 +00006110 CGF.EmitBranchThroughCleanup(Cont);
6111 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006112
John McCallbd309292010-07-06 01:34:17 +00006113 // Go back to the try-statement fallthrough.
6114 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006115
John McCallbd309292010-07-06 01:34:17 +00006116 // Pop out of the normal cleanup on the finally.
6117 if (S.getFinallyStmt())
6118 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006119
John McCallad5d61e2010-07-23 21:56:41 +00006120 if (Cont.isValid())
6121 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006122}
6123
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006124/// EmitThrowStmt - Generate code for a throw statement.
6125void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6126 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006127 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006128 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006129 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006130 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006131 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006132 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006133 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006134 "Unexpected rethrow outside @catch block.");
6135 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006136 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006137 }
6138
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006139 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006140 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6141 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6142 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006143 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00006144 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006145 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00006146 } else {
6147 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6148 ->setDoesNotReturn();
6149 CGF.Builder.CreateUnreachable();
6150 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006151
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006152 // Clear the insertion point to indicate we are in unreachable code.
6153 CGF.Builder.ClearInsertionPoint();
6154}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006155
John McCall2ca705e2010-07-24 00:37:23 +00006156llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006157CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006158 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006159 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006160
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006161 // If we don't need a definition, return the entry if found or check
6162 // if we use an external reference.
6163 if (!ForDefinition) {
6164 if (Entry)
6165 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006166
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006167 // If this type (or a super class) has the __objc_exception__
6168 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006169 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006170 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006171 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006172 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006173 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006174 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006175 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006176 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006177
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006178 // Otherwise we need to either make a new entry or fill in the
6179 // initializer.
6180 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006181 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006182 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006183 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006184 CGM.getModule().getGlobalVariable(VTableName);
6185 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006186 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6187 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006188 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006189 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006190
Chris Lattner5e016ae2010-06-27 07:15:29 +00006191 llvm::Value *VTableIdx =
6192 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006193
6194 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006195 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006196 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006197 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006198 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006199 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006200
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006201 if (Entry) {
6202 Entry->setInitializer(Init);
6203 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006204 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006205 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006206 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006207 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006208 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006209 }
6210
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006211 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006212 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006213 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6214 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006215
6216 if (ForDefinition) {
6217 Entry->setSection("__DATA,__objc_const");
6218 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6219 } else {
6220 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6221 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006222
6223 return Entry;
6224}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006225
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006226/* *** */
6227
Daniel Dunbarb036db82008-08-13 03:21:16 +00006228CodeGen::CGObjCRuntime *
6229CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006230 return new CGObjCMac(CGM);
6231}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006232
6233CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006234CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006235 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006236}