blob: e7f81a2be8946599e29203dd1151845108d3546b [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
John McCall8ccfcb52009-09-24 19:53:00 +0000110 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
111 Quals.addCVRQualifiers(CVRQualifiers);
112
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000113 if (!Ivar->isBitField())
114 return LValue::MakeAddr(V, Quals);
Daniel Dunbar961202372009-05-03 12:57:56 +0000115
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000116 // We need to compute the bit offset for the bit-field, the offset is to the
117 // byte. Note, there is a subtle invariant here: we can only call this routine
118 // on non-synthesized ivars but we may be called for synthesized ivars.
119 // However, a synthesized ivar can never be a bit-field, so this is safe.
120 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
121 uint64_t BitFieldSize =
122 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000123
Daniel Dunbardc406b82010-04-05 21:36:35 +0000124 // Allocate a new CGBitFieldInfo object to describe this access.
125 //
126 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
127 // layout object. However, this is blocked on other cleanups to the
128 // Objective-C code, so for now we just live with allocating a bunch of these
129 // objects.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000130
Daniel Dunbarb935b932010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000133 AI.FieldIndex = 0;
134 AI.FieldByteOffset = 0;
135 AI.FieldBitStart = BitOffset;
136 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
137 AI.AccessAlignment = 0;
138 AI.TargetBitOffset = 0;
139 AI.TargetBitWidth = BitFieldSize;
140
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000145 // FIXME: We need to set a very conservative alignment on this, or make sure
146 // that the runtime is doing the right thing.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000147 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000148}
149
150///
151
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000152namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000153
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000154typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000155
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156// FIXME: We should find a nicer way to make the labels for metadata, string
157// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000158
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000159class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000160protected:
161 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000162
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000163private:
164 llvm::Constant *getMessageSendFn() const {
165 // id objc_msgSend (id, SEL, ...)
166 std::vector<const llvm::Type*> Params;
167 Params.push_back(ObjectPtrTy);
168 Params.push_back(SelectorPtrTy);
169 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000170 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
171 Params, true),
172 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000173 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000174
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000175 llvm::Constant *getMessageSendStretFn() const {
176 // id objc_msgSend_stret (id, SEL, ...)
177 std::vector<const llvm::Type*> Params;
178 Params.push_back(ObjectPtrTy);
179 Params.push_back(SelectorPtrTy);
180 return
Owen Anderson41a75022009-08-13 21:57:51 +0000181 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182 Params, true),
183 "objc_msgSend_stret");
184
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000185 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000186
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000187 llvm::Constant *getMessageSendFpretFn() const {
188 // FIXME: This should be long double on x86_64?
189 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
190 std::vector<const llvm::Type*> Params;
191 Params.push_back(ObjectPtrTy);
192 Params.push_back(SelectorPtrTy);
193 return
Owen Anderson41a75022009-08-13 21:57:51 +0000194 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
195 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000196 Params,
197 true),
198 "objc_msgSend_fpret");
199
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000201
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000202 llvm::Constant *getMessageSendSuperFn() const {
203 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
204 const char *SuperName = "objc_msgSendSuper";
205 std::vector<const llvm::Type*> Params;
206 Params.push_back(SuperPtrTy);
207 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000209 Params, true),
210 SuperName);
211 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000212
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000213 llvm::Constant *getMessageSendSuperFn2() const {
214 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
215 const char *SuperName = "objc_msgSendSuper2";
216 std::vector<const llvm::Type*> Params;
217 Params.push_back(SuperPtrTy);
218 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000219 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000220 Params, true),
221 SuperName);
222 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000223
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000224 llvm::Constant *getMessageSendSuperStretFn() const {
225 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
226 // SEL op, ...)
227 std::vector<const llvm::Type*> Params;
228 Params.push_back(Int8PtrTy);
229 Params.push_back(SuperPtrTy);
230 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000231 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000232 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000233 Params, true),
234 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000235 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000236
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000237 llvm::Constant *getMessageSendSuperStretFn2() const {
238 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
239 // SEL op, ...)
240 std::vector<const llvm::Type*> Params;
241 Params.push_back(Int8PtrTy);
242 Params.push_back(SuperPtrTy);
243 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000244 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000245 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000246 Params, true),
247 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000248 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000249
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000250 llvm::Constant *getMessageSendSuperFpretFn() const {
251 // There is no objc_msgSendSuper_fpret? How can that work?
252 return getMessageSendSuperFn();
253 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000254
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000255 llvm::Constant *getMessageSendSuperFpretFn2() const {
256 // There is no objc_msgSendSuper_fpret? How can that work?
257 return getMessageSendSuperFn2();
258 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000259
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000260protected:
261 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000262
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000263public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000264 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000265 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000266
Daniel Dunbar5d715592008-08-12 05:28:47 +0000267 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
268 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000269
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000270 /// PtrObjectPtrTy - LLVM type for id *
271 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000273 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000274 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000275 /// ProtocolPtrTy - LLVM type for external protocol handles
276 /// (typeof(Protocol))
277 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000278
Daniel Dunbarc722b852008-08-30 03:02:31 +0000279 // SuperCTy - clang type for struct objc_super.
280 QualType SuperCTy;
281 // SuperPtrCTy - clang type for struct objc_super *.
282 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000283
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000284 /// SuperTy - LLVM type for struct objc_super.
285 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000286 /// SuperPtrTy - LLVM type for struct objc_super *.
287 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000288
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000289 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
290 /// in GCC parlance).
291 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000292
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000293 /// PropertyListTy - LLVM type for struct objc_property_list
294 /// (_prop_list_t in GCC parlance).
295 const llvm::StructType *PropertyListTy;
296 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
297 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000298
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000299 // MethodTy - LLVM type for struct objc_method.
300 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000301
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000302 /// CacheTy - LLVM type for struct objc_cache.
303 const llvm::Type *CacheTy;
304 /// CachePtrTy - LLVM type for struct objc_cache *.
305 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000306
Chris Lattnerce8754e2009-04-22 02:44:54 +0000307 llvm::Constant *getGetPropertyFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000311 llvm::SmallVector<CanQualType,4> Params;
312 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
313 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000314 Params.push_back(IdType);
315 Params.push_back(SelType);
316 Params.push_back(Ctx.LongTy);
317 Params.push_back(Ctx.BoolTy);
318 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000319 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000320 FunctionType::ExtInfo()),
321 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
323 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000324
Chris Lattnerce8754e2009-04-22 02:44:54 +0000325 llvm::Constant *getSetPropertyFn() {
326 CodeGen::CodeGenTypes &Types = CGM.getTypes();
327 ASTContext &Ctx = CGM.getContext();
328 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000329 llvm::SmallVector<CanQualType,6> Params;
330 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
331 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000332 Params.push_back(IdType);
333 Params.push_back(SelType);
334 Params.push_back(Ctx.LongTy);
335 Params.push_back(IdType);
336 Params.push_back(Ctx.BoolTy);
337 Params.push_back(Ctx.BoolTy);
338 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000339 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000340 FunctionType::ExtInfo()),
341 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
343 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000344
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000345
346 llvm::Constant *getCopyStructFn() {
347 CodeGen::CodeGenTypes &Types = CGM.getTypes();
348 ASTContext &Ctx = CGM.getContext();
349 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
350 llvm::SmallVector<CanQualType,5> Params;
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.VoidPtrTy);
353 Params.push_back(Ctx.LongTy);
354 Params.push_back(Ctx.BoolTy);
355 Params.push_back(Ctx.BoolTy);
356 const llvm::FunctionType *FTy =
357 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
358 FunctionType::ExtInfo()),
359 false);
360 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
361 }
362
Chris Lattnerce8754e2009-04-22 02:44:54 +0000363 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
365 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000366 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000367 llvm::SmallVector<CanQualType,1> Params;
368 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000369 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000370 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000371 FunctionType::ExtInfo()),
372 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
374 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000375
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000376 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000377 llvm::Constant *getGcReadWeakFn() {
378 // id objc_read_weak (id *)
379 std::vector<const llvm::Type*> Args;
380 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000382 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000384 }
385
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000386 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000387 llvm::Constant *getGcAssignWeakFn() {
388 // id objc_assign_weak (id, id *)
389 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
390 Args.push_back(ObjectPtrTy->getPointerTo());
391 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000392 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
394 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000395
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000396 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000397 llvm::Constant *getGcAssignGlobalFn() {
398 // id objc_assign_global(id, id *)
399 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
400 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000401 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000402 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000403 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
404 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000405
Fariborz Jahanian217af242010-07-20 20:30:03 +0000406 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
407 llvm::Constant *getGcAssignThreadLocalFn() {
408 // id objc_assign_threadlocal(id src, id * dest)
409 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
410 Args.push_back(ObjectPtrTy->getPointerTo());
411 llvm::FunctionType *FTy =
412 llvm::FunctionType::get(ObjectPtrTy, Args, false);
413 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
414 }
415
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000416 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000418 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000419 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
420 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000421 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000422 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000423 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
425 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000426
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000427 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
428 llvm::Constant *GcMemmoveCollectableFn() {
429 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
430 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
431 Args.push_back(Int8PtrTy);
432 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
435 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000436
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000437 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000438 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000439 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000440 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
441 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000442 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000443 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000444 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
445 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000446
447 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000448 llvm::Constant *getExceptionThrowFn() {
449 // void objc_exception_throw(id)
450 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
451 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000452 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000453 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
454 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000455
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000456 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
457 llvm::Constant *getExceptionRethrowFn() {
458 // void objc_exception_rethrow(void)
459 std::vector<const llvm::Type*> Args;
460 llvm::FunctionType *FTy =
461 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
462 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
463 }
464
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000465 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000466 llvm::Constant *getSyncEnterFn() {
467 // void objc_sync_enter (id)
468 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
469 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000470 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000471 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
472 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000473
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000474 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000475 llvm::Constant *getSyncExitFn() {
476 // void objc_sync_exit (id)
477 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
478 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000479 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000480 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
481 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000482
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
485 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000486
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
489 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000490
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendStretFn(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000494
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn2(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
497 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000498
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendFpretFn(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
501 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000502
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
505 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000506
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000507 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
508 ~ObjCCommonTypesHelper(){}
509};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000510
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000511/// ObjCTypesHelper - Helper class that encapsulates lazy
512/// construction of varies types used during ObjC generation.
513class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000514public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000515 /// SymtabTy - LLVM type for struct objc_symtab.
516 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000517 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
518 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000519 /// ModuleTy - LLVM type for struct objc_module.
520 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000521
Daniel Dunbarb036db82008-08-13 03:21:16 +0000522 /// ProtocolTy - LLVM type for struct objc_protocol.
523 const llvm::StructType *ProtocolTy;
524 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
525 const llvm::Type *ProtocolPtrTy;
526 /// ProtocolExtensionTy - LLVM type for struct
527 /// objc_protocol_extension.
528 const llvm::StructType *ProtocolExtensionTy;
529 /// ProtocolExtensionTy - LLVM type for struct
530 /// objc_protocol_extension *.
531 const llvm::Type *ProtocolExtensionPtrTy;
532 /// MethodDescriptionTy - LLVM type for struct
533 /// objc_method_description.
534 const llvm::StructType *MethodDescriptionTy;
535 /// MethodDescriptionListTy - LLVM type for struct
536 /// objc_method_description_list.
537 const llvm::StructType *MethodDescriptionListTy;
538 /// MethodDescriptionListPtrTy - LLVM type for struct
539 /// objc_method_description_list *.
540 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000541 /// ProtocolListTy - LLVM type for struct objc_property_list.
542 const llvm::Type *ProtocolListTy;
543 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
544 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000545 /// CategoryTy - LLVM type for struct objc_category.
546 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000547 /// ClassTy - LLVM type for struct objc_class.
548 const llvm::StructType *ClassTy;
549 /// ClassPtrTy - LLVM type for struct objc_class *.
550 const llvm::Type *ClassPtrTy;
551 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
552 const llvm::StructType *ClassExtensionTy;
553 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
554 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000555 // IvarTy - LLVM type for struct objc_ivar.
556 const llvm::StructType *IvarTy;
557 /// IvarListTy - LLVM type for struct objc_ivar_list.
558 const llvm::Type *IvarListTy;
559 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
560 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000561 /// MethodListTy - LLVM type for struct objc_method_list.
562 const llvm::Type *MethodListTy;
563 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
564 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Anders Carlsson9ff22482008-09-09 10:10:21 +0000566 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
567 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Anders Carlsson9ff22482008-09-09 10:10:21 +0000569 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionTryEnterFn() {
571 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000572 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000573 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000574 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575 Params, false),
576 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000577 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000578
579 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000580 llvm::Constant *getExceptionTryExitFn() {
581 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000582 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000583 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000584 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585 Params, false),
586 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000587 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000588
589 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000590 llvm::Constant *getExceptionExtractFn() {
591 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000592 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000594 Params, false),
595 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000596
Chris Lattnerc6406db2009-04-22 02:26:14 +0000597 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Anders Carlsson9ff22482008-09-09 10:10:21 +0000599 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000600 llvm::Constant *getExceptionMatchFn() {
601 std::vector<const llvm::Type*> Params;
602 Params.push_back(ClassPtrTy);
603 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000605 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000606 Params, false),
607 "objc_exception_match");
608
Chris Lattnerc6406db2009-04-22 02:26:14 +0000609 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000610
Anders Carlsson9ff22482008-09-09 10:10:21 +0000611 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000612 llvm::Constant *getSetJmpFn() {
613 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000614 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000615 return
Owen Anderson41a75022009-08-13 21:57:51 +0000616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000617 Params, false),
618 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Chris Lattnerc6406db2009-04-22 02:26:14 +0000620 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000622public:
623 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000624 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000625};
626
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000627/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000628/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000629class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000630public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000631
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000632 // MethodListnfABITy - LLVM for struct _method_list_t
633 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000634
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000635 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
636 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000638 // ProtocolnfABITy = LLVM for struct _protocol_t
639 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000641 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
642 const llvm::Type *ProtocolnfABIPtrTy;
643
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000644 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
645 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000647 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
648 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000650 // ClassnfABITy - LLVM for struct _class_t
651 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian71394042009-01-23 23:53:38 +0000653 // ClassnfABIPtrTy - LLVM for struct _class_t*
654 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000656 // IvarnfABITy - LLVM for struct _ivar_t
657 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000659 // IvarListnfABITy - LLVM for struct _ivar_list_t
660 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000662 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
663 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000665 // ClassRonfABITy - LLVM for struct _class_ro_t
666 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000668 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
669 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000671 // CategorynfABITy - LLVM for struct _category_t
672 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000674 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000675
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000676 // MessageRefTy - LLVM for:
677 // struct _message_ref_t {
678 // IMP messenger;
679 // SEL name;
680 // };
681 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000682 // MessageRefCTy - clang type for struct _message_ref_t
683 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000685 // MessageRefPtrTy - LLVM for struct _message_ref_t*
686 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000687 // MessageRefCPtrTy - clang type for struct _message_ref_t*
688 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000689
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000690 // MessengerTy - Type of the messenger (shown as IMP above)
691 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000692
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000693 // SuperMessageRefTy - LLVM for:
694 // struct _super_message_ref_t {
695 // SUPER_IMP messenger;
696 // SEL name;
697 // };
698 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000699
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000700 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
701 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000702
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendFixupFn() {
704 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
705 std::vector<const llvm::Type*> Params;
706 Params.push_back(ObjectPtrTy);
707 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSend_fixup");
711 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000713 llvm::Constant *getMessageSendFpretFixupFn() {
714 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
715 std::vector<const llvm::Type*> Params;
716 Params.push_back(ObjectPtrTy);
717 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000719 Params, true),
720 "objc_msgSend_fpret_fixup");
721 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000722
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000723 llvm::Constant *getMessageSendStretFixupFn() {
724 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
725 std::vector<const llvm::Type*> Params;
726 Params.push_back(ObjectPtrTy);
727 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000728 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000729 Params, true),
730 "objc_msgSend_stret_fixup");
731 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000732
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000733 llvm::Constant *getMessageSendIdFixupFn() {
734 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
735 std::vector<const llvm::Type*> Params;
736 Params.push_back(ObjectPtrTy);
737 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000738 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000739 Params, true),
740 "objc_msgSendId_fixup");
741 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000743 llvm::Constant *getMessageSendIdStretFixupFn() {
744 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
745 std::vector<const llvm::Type*> Params;
746 Params.push_back(ObjectPtrTy);
747 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000748 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000749 Params, true),
750 "objc_msgSendId_stret_fixup");
751 }
752 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000753 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000754 // struct _super_message_ref_t*, ...)
755 std::vector<const llvm::Type*> Params;
756 Params.push_back(SuperPtrTy);
757 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000758 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000759 Params, true),
760 "objc_msgSendSuper2_fixup");
761 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000762
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000763 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000764 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000765 // struct _super_message_ref_t*, ...)
766 std::vector<const llvm::Type*> Params;
767 Params.push_back(SuperPtrTy);
768 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000770 Params, true),
771 "objc_msgSendSuper2_stret_fixup");
772 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000773
774
775
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000776 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
777 /// exception personality function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000778 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +0000780 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerdcceee72009-04-06 16:53:45 +0000781 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000782 "__objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +0000783 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000784 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000785
Chris Lattnera7c00b42009-04-22 02:15:23 +0000786 llvm::Constant *getUnwindResumeOrRethrowFn() {
787 std::vector<const llvm::Type*> Params;
788 Params.push_back(Int8PtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000789 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000790 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791 Params, false),
Daniel Dunbar3241d402010-02-10 18:49:11 +0000792 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
793 "_Unwind_Resume_or_Rethrow"));
Chris Lattnera7c00b42009-04-22 02:15:23 +0000794 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000795
Chris Lattnera7c00b42009-04-22 02:15:23 +0000796 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000797 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000798 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000799 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000800
Chris Lattnera7c00b42009-04-22 02:15:23 +0000801 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000802
Chris Lattnera7c00b42009-04-22 02:15:23 +0000803 llvm::Constant *getObjCBeginCatchFn() {
804 std::vector<const llvm::Type*> Params;
805 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000806 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000807 Params, false),
808 "objc_begin_catch");
809 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000810
811 const llvm::StructType *EHTypeTy;
812 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000813
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000814 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
815 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000816};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000817
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000818class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000819public:
820 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000821 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000822 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000823 unsigned ivar_bytepos;
824 unsigned ivar_size;
825 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000826 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000827
828 // Allow sorting based on byte pos.
829 bool operator<(const GC_IVAR &b) const {
830 return ivar_bytepos < b.ivar_bytepos;
831 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000832 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000833
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000834 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000835 public:
836 unsigned skip;
837 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000838 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000839 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000840 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000841
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000842protected:
843 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000844 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000845 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000846 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000847
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000848 // gc ivar layout bitmap calculation helper caches.
849 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
850 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000852 /// LazySymbols - Symbols to generate a lazy reference for. See
853 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000854 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000856 /// DefinedSymbols - External symbols which are defined by this
857 /// module. The symbols in this list and LazySymbols are used to add
858 /// special linker symbols which ensure that Objective-C modules are
859 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000860 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000861
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000862 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000863 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000864
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000865 /// MethodVarNames - uniqued method variable names.
866 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000867
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000868 /// DefinedCategoryNames - list of category names in form Class_Category.
869 llvm::SetVector<std::string> DefinedCategoryNames;
870
Daniel Dunbarb036db82008-08-13 03:21:16 +0000871 /// MethodVarTypes - uniqued method type signatures. We have to use
872 /// a StringMap here because have no other unique reference.
873 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000874
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000875 /// MethodDefinitions - map of methods which have been defined in
876 /// this translation unit.
877 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000878
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000879 /// PropertyNames - uniqued method variable names.
880 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000882 /// ClassReferences - uniqued class references.
883 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000884
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000885 /// SelectorReferences - uniqued selector references.
886 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000887
Daniel Dunbarb036db82008-08-13 03:21:16 +0000888 /// Protocols - Protocols for which an objc_protocol structure has
889 /// been emitted. Forward declarations are handled by creating an
890 /// empty structure whose initializer is filled in when/if defined.
891 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000892
Daniel Dunbarc475d422008-10-29 22:36:39 +0000893 /// DefinedProtocols - Protocols which have actually been
894 /// defined. We should not need this, see FIXME in GenerateProtocol.
895 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000897 /// DefinedClasses - List of defined classes.
898 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000899
900 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
901 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000903 /// DefinedCategories - List of defined categories.
904 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000905
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000906 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
907 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000909 /// GetNameForMethod - Return a name for the given method.
910 /// \param[out] NameOut - The return value.
911 void GetNameForMethod(const ObjCMethodDecl *OMD,
912 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000913 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000914
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000915 /// GetMethodVarName - Return a unique constant for the given
916 /// selector's name. The return value has type char *.
917 llvm::Constant *GetMethodVarName(Selector Sel);
918 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
919 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000920
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000921 /// GetMethodVarType - Return a unique constant for the given
922 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000924 // FIXME: This is a horrible name.
925 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000926 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000927
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000928 /// GetPropertyName - Return a unique constant for the given
929 /// name. The return value has type char *.
930 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000931
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000932 // FIXME: This can be dropped once string functions are unified.
933 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
934 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000935
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000936 /// GetClassName - Return a unique constant for the given selector's
937 /// name. The return value has type char *.
938 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000939
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000940 /// BuildIvarLayout - Builds ivar layout bitmap for the class
941 /// implementation for the __strong or __weak case.
942 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000943 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
944 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000945
946 llvm::Constant *BuildIvarLayoutBitmap(bool hasUnion,
947 std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000948
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000949 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000950 unsigned int BytePos, bool ForStrongLayout,
951 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000952 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000953 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000954 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000955 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000956 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000957 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000958
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000959 /// GetIvarLayoutName - Returns a unique constant for the given
960 /// ivar layout bitmap.
961 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
962 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000963
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000964 /// EmitPropertyList - Emit the given property list. The return
965 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000966 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000967 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000968 const ObjCContainerDecl *OCD,
969 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000970
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000971 /// PushProtocolProperties - Push protocol's property on the input stack.
972 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
973 std::vector<llvm::Constant*> &Properties,
974 const Decl *Container,
975 const ObjCProtocolDecl *PROTO,
976 const ObjCCommonTypesHelper &ObjCTypes);
977
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000978 /// GetProtocolRef - Return a reference to the internal protocol
979 /// description, creating an empty one if it has not been
980 /// defined. The return value has type ProtocolPtrTy.
981 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000982
Daniel Dunbar30c65362009-03-09 20:09:19 +0000983 /// CreateMetadataVar - Create a global variable with internal
984 /// linkage for use by the Objective-C runtime.
985 ///
986 /// This is a convenience wrapper which not only creates the
987 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000988 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000989 ///
990 /// \param Name - The variable name.
991 /// \param Init - The variable initializer; this is also used to
992 /// define the type of the variable.
993 /// \param Section - The section the variable should go into, or 0.
994 /// \param Align - The alignment for the variable, or 0.
995 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000996 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000997 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000998 llvm::Constant *Init,
999 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001000 unsigned Align,
1001 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +00001002
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001003 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001004 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001005 QualType ResultType,
1006 llvm::Value *Sel,
1007 llvm::Value *Arg0,
1008 QualType Arg0Ty,
1009 bool IsSuper,
1010 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001011 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001012 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001013
Daniel Dunbar5e639272010-04-25 20:39:01 +00001014 /// EmitImageInfo - Emit the image info marker used to encode some module
1015 /// level information.
1016 void EmitImageInfo();
1017
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001018public:
Owen Andersonae86c192009-07-13 04:10:07 +00001019 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +00001020 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001021
David Chisnall481e3a82010-01-23 02:40:42 +00001022 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001023
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001024 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1025 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001026
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001027 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001028
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001029 /// GetOrEmitProtocol - Get the protocol object for the given
1030 /// declaration, emitting it if necessary. The return value has type
1031 /// ProtocolPtrTy.
1032 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001033
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001034 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1035 /// object for the given declaration, emitting it if needed. These
1036 /// forward references will be filled in with empty bodies if no
1037 /// definition is seen. The return value has type ProtocolPtrTy.
1038 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001039 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1040 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1041
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001042};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001043
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001044class CGObjCMac : public CGObjCCommonMac {
1045private:
1046 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001047
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001048 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001049 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001050 void EmitModuleInfo();
1051
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001052 /// EmitModuleSymols - Emit module symbols, the list of defined
1053 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001054 llvm::Constant *EmitModuleSymbols();
1055
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001056 /// FinishModule - Write out global data structures at the end of
1057 /// processing a translation unit.
1058 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001059
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001060 /// EmitClassExtension - Generate the class extension structure used
1061 /// to store the weak ivar layout and properties. The return value
1062 /// has type ClassExtensionPtrTy.
1063 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1064
1065 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1066 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001067 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001068 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001069
1070 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1071 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001072
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001073 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001074 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001075 QualType ResultType,
1076 Selector Sel,
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001077 llvm::Value *Arg0,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001078 QualType Arg0Ty,
1079 bool IsSuper,
1080 const CallArgList &CallArgs);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001081
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001082 /// EmitIvarList - Emit the ivar list for the given
1083 /// implementation. If ForClass is true the list of class ivars
1084 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1085 /// interface ivars will be emitted. The return value has type
1086 /// IvarListPtrTy.
1087 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001088 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001090 /// EmitMetaClass - Emit a forward reference to the class structure
1091 /// for the metaclass of the given interface. The return value has
1092 /// type ClassPtrTy.
1093 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1094
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001095 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001096 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001097 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1098 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001099 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001100
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001101 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001102
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001103 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001104
1105 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001106 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001107 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001108 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001109 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001110
1111 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001112 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001113 /// - TypeName: The name for the type containing the methods.
1114 /// - IsProtocol: True iff these methods are for a protocol.
1115 /// - ClassMethds: True iff these are class methods.
1116 /// - Required: When true, only "required" methods are
1117 /// listed. Similarly, when false only "optional" methods are
1118 /// listed. For classes this should always be true.
1119 /// - begin, end: The method list to output.
1120 ///
1121 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001122 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001123 const char *Section,
1124 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001125
Daniel Dunbarc475d422008-10-29 22:36:39 +00001126 /// GetOrEmitProtocol - Get the protocol object for the given
1127 /// declaration, emitting it if necessary. The return value has type
1128 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001129 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001130
1131 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1132 /// object for the given declaration, emitting it if needed. These
1133 /// forward references will be filled in with empty bodies if no
1134 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001135 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001136
Daniel Dunbarb036db82008-08-13 03:21:16 +00001137 /// EmitProtocolExtension - Generate the protocol extension
1138 /// structure used to store optional instance and class methods, and
1139 /// protocol properties. The return value has type
1140 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001141 llvm::Constant *
1142 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1143 const ConstantVector &OptInstanceMethods,
1144 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001145
1146 /// EmitProtocolList - Generate the list of referenced
1147 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001148 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001149 ObjCProtocolDecl::protocol_iterator begin,
1150 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001151
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001152 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1153 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001154 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1155 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156
1157public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001158 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001159
Fariborz Jahanian71394042009-01-23 23:53:38 +00001160 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001162 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001163 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001164 QualType ResultType,
1165 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001166 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001167 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001168 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001169 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001170
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001171 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001172 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001173 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001174 QualType ResultType,
1175 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001176 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001177 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001178 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001179 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001180 const CallArgList &CallArgs,
1181 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182
Daniel Dunbarcb463852008-11-01 01:53:16 +00001183 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001184 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001185
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001186 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1187 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001188
1189 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1190 /// untyped one.
1191 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1192 const ObjCMethodDecl *Method);
1193
John McCall2ca705e2010-07-24 00:37:23 +00001194 virtual llvm::Constant *GetEHType(QualType T);
1195
Daniel Dunbar92992502008-08-15 22:20:32 +00001196 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001197
Daniel Dunbar92992502008-08-15 22:20:32 +00001198 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001199
Daniel Dunbarcb463852008-11-01 01:53:16 +00001200 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001201 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202
Chris Lattnerd4808922009-03-22 21:03:39 +00001203 virtual llvm::Constant *GetPropertyGetFunction();
1204 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001205 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001206 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001207
John McCallbd309292010-07-06 01:34:17 +00001208 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1209 const ObjCAtTryStmt &S);
1210 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1211 const ObjCAtSynchronizedStmt &S);
1212 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001213 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1214 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001215 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001216 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001217 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001219 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001220 llvm::Value *src, llvm::Value *dest,
1221 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001222 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001223 llvm::Value *src, llvm::Value *dest,
1224 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001225 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1226 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001227 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1228 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001229 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001230
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001231 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1232 QualType ObjectTy,
1233 llvm::Value *BaseValue,
1234 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001235 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001236 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001237 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001238 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001239};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001240
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001241class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001242private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001243 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001244 llvm::GlobalVariable* ObjCEmptyCacheVar;
1245 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001246
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001247 /// SuperClassReferences - uniqued super class references.
1248 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001249
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001250 /// MetaClassReferences - uniqued meta class references.
1251 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001252
1253 /// EHTypeReferences - uniqued class ehtype references.
1254 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001255
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001256 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001257 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001258 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001259
Fariborz Jahanian67260552009-11-17 21:37:35 +00001260 /// DefinedMetaClasses - List of defined meta-classes.
1261 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1262
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001263 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001264 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001265 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001266
Fariborz Jahanian71394042009-01-23 23:53:38 +00001267 /// FinishNonFragileABIModule - Write out global data structures at the end of
1268 /// processing a translation unit.
1269 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001270
Daniel Dunbar19573e72009-05-15 21:48:48 +00001271 /// AddModuleClassList - Add the given list of class pointers to the
1272 /// module with the provided symbol and section names.
1273 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1274 const char *SymbolName,
1275 const char *SectionName);
1276
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001277 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1278 unsigned InstanceStart,
1279 unsigned InstanceSize,
1280 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001281 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001282 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001283 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001284 llvm::Constant *ClassRoGV,
1285 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001287 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001288
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001289 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001290
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001291 /// EmitMethodList - Emit the method list for the given
1292 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001293 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001294 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001295 const ConstantVector &Methods);
1296 /// EmitIvarList - Emit the ivar list for the given
1297 /// implementation. If ForClass is true the list of class ivars
1298 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1299 /// interface ivars will be emitted. The return value has type
1300 /// IvarListnfABIPtrTy.
1301 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001303 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001304 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001305 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001307 /// GetOrEmitProtocol - Get the protocol object for the given
1308 /// declaration, emitting it if necessary. The return value has type
1309 /// ProtocolPtrTy.
1310 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001311
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001312 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1313 /// object for the given declaration, emitting it if needed. These
1314 /// forward references will be filled in with empty bodies if no
1315 /// definition is seen. The return value has type ProtocolPtrTy.
1316 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001317
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001318 /// EmitProtocolList - Generate the list of referenced
1319 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001320 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001321 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001322 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001324 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001325 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001326 QualType ResultType,
1327 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001328 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001329 QualType Arg0Ty,
1330 bool IsSuper,
1331 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001332
1333 /// GetClassGlobal - Return the global variable for the Objective-C
1334 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001335 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001336
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001337 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001338 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001339 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001340 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001341
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001342 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1343 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001344 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1345 const ObjCInterfaceDecl *ID);
1346
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001347 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1348 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001349 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001350 const ObjCInterfaceDecl *ID);
1351
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001352 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1353 /// the given ivar.
1354 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001355 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356 const ObjCInterfaceDecl *ID,
1357 const ObjCIvarDecl *Ivar);
1358
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001359 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1360 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001361 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1362 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001363
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001364 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001365 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001366 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001367 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001368
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001369 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001370 return "OBJC_METACLASS_$_";
1371 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001372
Daniel Dunbar15894b72009-04-07 05:48:37 +00001373 const char *getClassSymbolPrefix() const {
1374 return "OBJC_CLASS_$_";
1375 }
1376
Daniel Dunbar961202372009-05-03 12:57:56 +00001377 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001378 uint32_t &InstanceStart,
1379 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001380
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001381 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001382 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001383 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1384 return CGM.getContext().Selectors.getSelector(0, &II);
1385 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001386
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001387 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001388 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1389 return CGM.getContext().Selectors.getSelector(1, &II);
1390 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001391
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001392 /// ImplementationIsNonLazy - Check whether the given category or
1393 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001394 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001395
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001396public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001397 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001398 // FIXME. All stubs for now!
1399 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001400
Fariborz Jahanian71394042009-01-23 23:53:38 +00001401 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001402 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001403 QualType ResultType,
1404 Selector Sel,
1405 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001406 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001407 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001408 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001409
1410 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001411 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001412 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001413 QualType ResultType,
1414 Selector Sel,
1415 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001416 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001417 llvm::Value *Receiver,
1418 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001419 const CallArgList &CallArgs,
1420 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001421
Fariborz Jahanian71394042009-01-23 23:53:38 +00001422 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001423 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001424
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001425 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1426 bool lvalue = false)
1427 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001428
1429 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1430 /// untyped one.
1431 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1432 const ObjCMethodDecl *Method)
1433 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001434
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001435 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001436
Fariborz Jahanian71394042009-01-23 23:53:38 +00001437 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001438 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001439 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001440
John McCall2ca705e2010-07-24 00:37:23 +00001441 virtual llvm::Constant *GetEHType(QualType T);
1442
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001443 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001444 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001445 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001446 virtual llvm::Constant *GetPropertySetFunction() {
1447 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001448 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001449
1450 virtual llvm::Constant *GetCopyStructFunction() {
1451 return ObjCTypes.getCopyStructFn();
1452 }
1453
Chris Lattnerd4808922009-03-22 21:03:39 +00001454 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001455 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001456 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001457
John McCallbd309292010-07-06 01:34:17 +00001458 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1459 const ObjCAtTryStmt &S);
1460 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1461 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001462 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001463 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001464 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001465 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001466 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001467 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001468 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001469 llvm::Value *src, llvm::Value *dest,
1470 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001471 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001472 llvm::Value *src, llvm::Value *dest,
1473 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001474 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001475 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001476 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1477 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001478 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001479 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1480 QualType ObjectTy,
1481 llvm::Value *BaseValue,
1482 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001483 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001484 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001485 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001486 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001487};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001488
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001489} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001490
1491/* *** Helper Functions *** */
1492
1493/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001494static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001495 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001496 unsigned idx0,
1497 unsigned idx1) {
1498 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001499 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1500 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001501 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001502 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001503}
1504
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001505/// hasObjCExceptionAttribute - Return true if this class or any super
1506/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001507static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001508 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001509 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001510 return true;
1511 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001512 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001513 return false;
1514}
1515
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001516/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001517
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001518CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001519 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001520 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001521 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001522}
1523
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001524/// GetClass - Return a reference to the class for the given interface
1525/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001526llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001527 const ObjCInterfaceDecl *ID) {
1528 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001529}
1530
1531/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001532llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1533 bool lval) {
1534 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001535}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001536llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001537 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001538 return EmitSelector(Builder, Method->getSelector());
1539}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001540
John McCall2ca705e2010-07-24 00:37:23 +00001541llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1542 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1543 return 0;
1544}
1545
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001546/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001547/*
1548 struct __builtin_CFString {
1549 const int *isa; // point to __CFConstantStringClassReference
1550 int flags;
1551 const char *str;
1552 long length;
1553 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001554*/
1555
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001556/// or Generate a constant NSString object.
1557/*
1558 struct __builtin_NSString {
1559 const int *isa; // point to __NSConstantStringClassReference
1560 const char *str;
1561 unsigned int length;
1562 };
1563*/
1564
Fariborz Jahanian71394042009-01-23 23:53:38 +00001565llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001566 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001567 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1568 CGM.GetAddrOfConstantCFString(SL) :
1569 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001570}
1571
1572/// Generates a message send where the super is the receiver. This is
1573/// a message send to self with special delivery semantics indicating
1574/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001575CodeGen::RValue
1576CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001577 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001578 QualType ResultType,
1579 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001580 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001581 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001582 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001583 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001584 const CodeGen::CallArgList &CallArgs,
1585 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001586 // Create and init a super structure; this is a (receiver, class)
1587 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001588 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001589 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001590 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001591 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001592 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001593 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001594
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001595 // If this is a class message the metaclass is passed as the target.
1596 llvm::Value *Target;
1597 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001598 if (isCategoryImpl) {
1599 // Message sent to 'super' in a class method defined in a category
1600 // implementation requires an odd treatment.
1601 // If we are in a class method, we must retrieve the
1602 // _metaclass_ for the current class, pointed at by
1603 // the class's "isa" pointer. The following assumes that
1604 // isa" is the first ivar in a class (which it must be).
1605 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1606 Target = CGF.Builder.CreateStructGEP(Target, 0);
1607 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001608 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001609 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1610 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1611 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1612 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001613 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001614 }
1615 else if (isCategoryImpl)
1616 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1617 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001618 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1619 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1620 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001621 }
Mike Stump18bb9282009-05-16 07:57:57 +00001622 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1623 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001624 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001625 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001626 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001627 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001628 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001629 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001630 EmitSelector(CGF.Builder, Sel),
1631 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001632 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001633}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001634
1635/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001636CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001637 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001638 QualType ResultType,
1639 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001640 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001641 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001642 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001643 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001644 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001645 EmitSelector(CGF.Builder, Sel),
1646 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001647 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001648}
1649
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001650CodeGen::RValue
1651CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001652 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001653 QualType ResultType,
1654 llvm::Value *Sel,
1655 llvm::Value *Arg0,
1656 QualType Arg0Ty,
1657 bool IsSuper,
1658 const CallArgList &CallArgs,
1659 const ObjCMethodDecl *Method,
1660 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001661 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001662 if (!IsSuper)
1663 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001664 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001665 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001666 CGF.getContext().getObjCSelType()));
1667 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001668
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001669 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001670 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001671 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001672 const llvm::FunctionType *FTy =
1673 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001674
Anders Carlsson280e61f12010-06-21 20:59:55 +00001675 if (Method)
1676 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1677 CGM.getContext().getCanonicalType(ResultType) &&
1678 "Result type mismatch!");
1679
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001680 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001681 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001682 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001683 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001684 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1685 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1686 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001687 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001688 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001689 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001690 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001691 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001692 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001693}
1694
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001695static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1696 if (FQT.isObjCGCStrong())
1697 return Qualifiers::Strong;
1698
1699 if (FQT.isObjCGCWeak())
1700 return Qualifiers::Weak;
1701
1702 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1703 return Qualifiers::Strong;
1704
1705 if (const PointerType *PT = FQT->getAs<PointerType>())
1706 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1707
1708 return Qualifiers::GCNone;
1709}
1710
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001711llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001712 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1713 llvm::Constant *NullPtr =
1714 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
1715 if (DeclRefs.empty())
1716 return NullPtr;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001717 SkipIvars.clear();
1718 IvarsInfo.clear();
1719 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1720 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1721
1722 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1723 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1724 const ValueDecl *VD = BDRE->getDecl();
1725 CharUnits Offset = CGF.BlockDecls[VD];
1726 uint64_t FieldOffset = Offset.getQuantity();
1727 QualType Ty = VD->getType();
1728 assert(!Ty->isArrayType() &&
1729 "Array block variable should have been caught");
1730 if (Ty->isRecordType() || Ty->isUnionType())
1731 assert(false && "Aggregate block variable layout NYI");
1732 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1733 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1734 // __block variables are passed by their descriptior address. So, size
1735 // must reflect this.
1736 if (BDRE->isByRef())
1737 FieldSize = WordSizeInBits;
1738 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1739 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1740 FieldSize / WordSizeInBits));
1741 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1742 SkipIvars.push_back(GC_IVAR(FieldOffset,
1743 FieldSize / ByteSizeInBits));
1744 }
1745
1746 if (IvarsInfo.empty())
1747 return NullPtr;
1748
1749 std::string BitMap;
1750 llvm::Constant *C = BuildIvarLayoutBitmap(false, BitMap);
1751 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1752 printf("\n block variable layout for block: ");
1753 const unsigned char *s = (unsigned char*)BitMap.c_str();
1754 for (unsigned i = 0; i < BitMap.size(); i++)
1755 if (!(s[i] & 0xf0))
1756 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1757 else
1758 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1759 printf("\n");
1760 }
1761
1762 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001763}
1764
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001765llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001766 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001767 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001768 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001769 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1770
Owen Andersonade90fd2009-07-29 18:54:39 +00001771 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001772 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001773}
1774
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001775void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001776 // FIXME: We shouldn't need this, the protocol decl should contain enough
1777 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001778 DefinedProtocols.insert(PD->getIdentifier());
1779
1780 // If we have generated a forward reference to this protocol, emit
1781 // it now. Otherwise do nothing, the protocol objects are lazily
1782 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001783 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001784 GetOrEmitProtocol(PD);
1785}
1786
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001787llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001788 if (DefinedProtocols.count(PD->getIdentifier()))
1789 return GetOrEmitProtocol(PD);
1790 return GetOrEmitProtocolRef(PD);
1791}
1792
Daniel Dunbarb036db82008-08-13 03:21:16 +00001793/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001794// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1795struct _objc_protocol {
1796struct _objc_protocol_extension *isa;
1797char *protocol_name;
1798struct _objc_protocol_list *protocol_list;
1799struct _objc__method_prototype_list *instance_methods;
1800struct _objc__method_prototype_list *class_methods
1801};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001802
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001803See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001804*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001805llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1806 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1807
1808 // Early exit if a defining object has already been generated.
1809 if (Entry && Entry->hasInitializer())
1810 return Entry;
1811
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001812 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001813 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001814 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1815
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001816 // Construct method lists.
1817 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1818 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001819 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001820 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001821 ObjCMethodDecl *MD = *i;
1822 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1823 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1824 OptInstanceMethods.push_back(C);
1825 } else {
1826 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001827 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001828 }
1829
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001831 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001832 ObjCMethodDecl *MD = *i;
1833 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1834 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1835 OptClassMethods.push_back(C);
1836 } else {
1837 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001838 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001839 }
1840
Daniel Dunbarb036db82008-08-13 03:21:16 +00001841 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001842 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001844 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001845 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001846 PD->protocol_begin(),
1847 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001848 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001849 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001850 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1851 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001852 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001853 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001854 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1855 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001856 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001858
Daniel Dunbarb036db82008-08-13 03:21:16 +00001859 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001860 // Already created, fix the linkage and update the initializer.
1861 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862 Entry->setInitializer(Init);
1863 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001864 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001865 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001866 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001867 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001868 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001870 // FIXME: Is this necessary? Why only for protocol?
1871 Entry->setAlignment(4);
1872 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001873 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001874
1875 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001876}
1877
Daniel Dunbarc475d422008-10-29 22:36:39 +00001878llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001879 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1880
1881 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001882 // We use the initializer as a marker of whether this is a forward
1883 // reference or not. At module finalization we add the empty
1884 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001885 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001886 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001887 llvm::GlobalValue::ExternalLinkage,
1888 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001889 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001890 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001891 // FIXME: Is this necessary? Why only for protocol?
1892 Entry->setAlignment(4);
1893 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001894
Daniel Dunbarb036db82008-08-13 03:21:16 +00001895 return Entry;
1896}
1897
1898/*
1899 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001900 uint32_t size;
1901 struct objc_method_description_list *optional_instance_methods;
1902 struct objc_method_description_list *optional_class_methods;
1903 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001904 };
1905*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001906llvm::Constant *
1907CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1908 const ConstantVector &OptInstanceMethods,
1909 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001910 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001911 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001912 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001913 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001914 Values[1] =
1915 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001916 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001917 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1918 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001919 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001920 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001921 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1922 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001923 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001924 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001925
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001926 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001927 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001928 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001929 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001930
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001932 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001933
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001934 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001935 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001936 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001937 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001938}
1939
1940/*
1941 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001942 struct objc_protocol_list *next;
1943 long count;
1944 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001945 };
1946*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001947llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001948CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001949 ObjCProtocolDecl::protocol_iterator begin,
1950 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001951 std::vector<llvm::Constant*> ProtocolRefs;
1952
Daniel Dunbardec75f82008-08-21 21:57:41 +00001953 for (; begin != end; ++begin)
1954 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001955
1956 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001957 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001958 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001959
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001960 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001961 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001962
1963 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001964 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001965 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001966 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967 ProtocolRefs.size() - 1);
1968 Values[2] =
1969 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1970 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001971 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001972
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001973 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001974 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001975 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001976 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001977 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001978}
1979
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001980void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1981 std::vector<llvm::Constant*> &Properties,
1982 const Decl *Container,
1983 const ObjCProtocolDecl *PROTO,
1984 const ObjCCommonTypesHelper &ObjCTypes) {
1985 std::vector<llvm::Constant*> Prop(2);
1986 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1987 E = PROTO->protocol_end(); P != E; ++P)
1988 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1989 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1990 E = PROTO->prop_end(); I != E; ++I) {
1991 const ObjCPropertyDecl *PD = *I;
1992 if (!PropertySet.insert(PD->getIdentifier()))
1993 continue;
1994 Prop[0] = GetPropertyName(PD->getIdentifier());
1995 Prop[1] = GetPropertyTypeString(PD, Container);
1996 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1997 }
1998}
1999
Daniel Dunbarb036db82008-08-13 03:21:16 +00002000/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002001 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002002 const char * const name;
2003 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002004 };
2005
2006 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002007 uint32_t entsize; // sizeof (struct _objc_property)
2008 uint32_t prop_count;
2009 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002010 };
2011*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002012llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002013 const Decl *Container,
2014 const ObjCContainerDecl *OCD,
2015 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002016 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002017 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002018 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2019 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002020 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002021 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002022 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00002023 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002024 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002025 Prop));
2026 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002027 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002028 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
2029 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002030 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2031 ObjCTypes);
2032 }
2033 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2034 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2035 E = CD->protocol_end(); P != E; ++P)
2036 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2037 ObjCTypes);
2038 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002039
2040 // Return null for empty list.
2041 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002042 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002043
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002044 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002045 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002046 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002047 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2048 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002049 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002050 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002051 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002052 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002053
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002054 llvm::GlobalVariable *GV =
2055 CreateMetadataVar(Name, Init,
2056 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002057 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002058 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002059 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002060 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002061}
2062
2063/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002064 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002065 int count;
2066 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002067 };
2068*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002069llvm::Constant *
2070CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2071 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002072 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002073 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2074 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002075 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002076 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002077 Desc);
2078}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002079
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002080llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002081 const char *Section,
2082 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002083 // Return null for empty list.
2084 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002085 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002086
2087 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002088 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002089 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002090 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002091 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002092 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002093
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002094 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002095 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002096 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002097}
2098
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002099/*
2100 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002101 char *category_name;
2102 char *class_name;
2103 struct _objc_method_list *instance_methods;
2104 struct _objc_method_list *class_methods;
2105 struct _objc_protocol_list *protocols;
2106 uint32_t size; // <rdar://4585769>
2107 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002108 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002109*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002110void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002111 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002112
Mike Stump18bb9282009-05-16 07:57:57 +00002113 // FIXME: This is poor design, the OCD should have a pointer to the category
2114 // decl. Additionally, note that Category can be null for the @implementation
2115 // w/o an @interface case. Sema should just create one for us as it does for
2116 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002117 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002118 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002119 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002120
2121 llvm::SmallString<256> ExtName;
2122 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2123 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002124
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002126 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002127 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002128 // Instance methods should always be defined.
2129 InstanceMethods.push_back(GetMethodConstant(*i));
2130 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002132 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002133 // Class methods should always be defined.
2134 ClassMethods.push_back(GetMethodConstant(*i));
2135 }
2136
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002137 std::vector<llvm::Constant*> Values(7);
2138 Values[0] = GetClassName(OCD->getIdentifier());
2139 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002140 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002141 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002142 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002143 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002144 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002145 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002146 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002147 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002148 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002149 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002150 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002151 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002152 Category->protocol_begin(),
2153 Category->protocol_end());
2154 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002155 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002156 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002157 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002158
2159 // If there is no category @interface then there can be no properties.
2160 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002161 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002162 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002163 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002164 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002165 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002166
Owen Anderson0e0189d2009-07-27 22:29:56 +00002167 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002168 Values);
2169
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002170 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002171 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002172 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002173 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002174 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002175 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002176}
2177
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002178// FIXME: Get from somewhere?
2179enum ClassFlags {
2180 eClassFlags_Factory = 0x00001,
2181 eClassFlags_Meta = 0x00002,
2182 // <rdr://5142207>
2183 eClassFlags_HasCXXStructors = 0x02000,
2184 eClassFlags_Hidden = 0x20000,
2185 eClassFlags_ABI2_Hidden = 0x00010,
2186 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2187};
2188
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002189/*
2190 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002191 Class isa;
2192 Class super_class;
2193 const char *name;
2194 long version;
2195 long info;
2196 long instance_size;
2197 struct _objc_ivar_list *ivars;
2198 struct _objc_method_list *methods;
2199 struct _objc_cache *cache;
2200 struct _objc_protocol_list *protocols;
2201 // Objective-C 1.0 extensions (<rdr://4585769>)
2202 const char *ivar_layout;
2203 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002204 };
2205
2206 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002207*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002209 DefinedSymbols.insert(ID->getIdentifier());
2210
Chris Lattner86d7d912008-11-24 03:54:41 +00002211 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002212 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002213 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002214 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002215 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002216 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002217 Interface->protocol_begin(),
2218 Interface->protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002219 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002220 if (ID->getNumIvarInitializers())
2221 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002222 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002223 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002224
2225 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002226 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002227 Flags |= eClassFlags_Hidden;
2228
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002229 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002230 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002231 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002232 // Instance methods should always be defined.
2233 InstanceMethods.push_back(GetMethodConstant(*i));
2234 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002235 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002236 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002237 // Class methods should always be defined.
2238 ClassMethods.push_back(GetMethodConstant(*i));
2239 }
2240
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002241 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002242 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002243 ObjCPropertyImplDecl *PID = *i;
2244
2245 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2246 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2247
2248 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2249 if (llvm::Constant *C = GetMethodConstant(MD))
2250 InstanceMethods.push_back(C);
2251 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2252 if (llvm::Constant *C = GetMethodConstant(MD))
2253 InstanceMethods.push_back(C);
2254 }
2255 }
2256
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002257 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002258 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002259 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002260 // Record a reference to the super class.
2261 LazySymbols.insert(Super->getIdentifier());
2262
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002263 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002264 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002265 ObjCTypes.ClassPtrTy);
2266 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002267 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002268 }
2269 Values[ 2] = GetClassName(ID->getIdentifier());
2270 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002271 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2272 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2273 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002274 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002275 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002276 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002277 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002278 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002279 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002280 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002281 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002282 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002284 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002285 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002286 std::string Name("\01L_OBJC_CLASS_");
2287 Name += ClassName;
2288 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2289 // Check for a forward reference.
2290 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2291 if (GV) {
2292 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2293 "Forward metaclass reference has incorrect type.");
2294 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2295 GV->setInitializer(Init);
2296 GV->setSection(Section);
2297 GV->setAlignment(4);
2298 CGM.AddUsedGlobal(GV);
2299 }
2300 else
2301 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002302 DefinedClasses.push_back(GV);
2303}
2304
2305llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2306 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002307 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002309 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002310
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002311 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002312 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002313
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314 std::vector<llvm::Constant*> Values(12);
2315 // The isa for the metaclass is the root of the hierarchy.
2316 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2317 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2318 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002319 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002320 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002321 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002322 // The super class for the metaclass is emitted as the name of the
2323 // super class. The runtime fixes this up to point to the
2324 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002326 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002327 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002328 ObjCTypes.ClassPtrTy);
2329 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002330 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002331 }
2332 Values[ 2] = GetClassName(ID->getIdentifier());
2333 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002334 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2335 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2336 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002337 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002338 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002339 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002340 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002341 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002342 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002343 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002344 Values[ 9] = Protocols;
2345 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002346 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002347 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002348 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002349 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002350 Values);
2351
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002352 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002353 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002354
2355 // Check for a forward reference.
2356 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2357 if (GV) {
2358 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2359 "Forward metaclass reference has incorrect type.");
2360 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2361 GV->setInitializer(Init);
2362 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002363 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002364 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002365 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002366 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002367 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002368 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002369 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002370
2371 return GV;
2372}
2373
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002374llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002375 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002376
Mike Stump18bb9282009-05-16 07:57:57 +00002377 // FIXME: Should we look these up somewhere other than the module. Its a bit
2378 // silly since we only generate these while processing an implementation, so
2379 // exactly one pointer would work if know when we entered/exitted an
2380 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002381
2382 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002383 // Previously, metaclass with internal linkage may have been defined.
2384 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002385 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2386 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002387 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2388 "Forward metaclass reference has incorrect type.");
2389 return GV;
2390 } else {
2391 // Generate as an external reference to keep a consistent
2392 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002393 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002394 llvm::GlobalValue::ExternalLinkage,
2395 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002396 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002397 }
2398}
2399
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002400llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2401 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2402
2403 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2404 true)) {
2405 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2406 "Forward class metadata reference has incorrect type.");
2407 return GV;
2408 } else {
2409 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2410 llvm::GlobalValue::ExternalLinkage,
2411 0,
2412 Name);
2413 }
2414}
2415
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002416/*
2417 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002418 uint32_t size;
2419 const char *weak_ivar_layout;
2420 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421 };
2422*/
2423llvm::Constant *
2424CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002426 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002427
2428 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002429 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002430 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002431 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002432 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002433
2434 // Return null if no extension bits are used.
2435 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002436 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002437
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002438 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002439 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002440 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002441 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002442 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443}
2444
2445/*
2446 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002447 char *ivar_name;
2448 char *ivar_type;
2449 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002450 };
2451
2452 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002453 int ivar_count;
2454 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002456*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002457llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002458 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002459 std::vector<llvm::Constant*> Ivars, Ivar(3);
2460
2461 // When emitting the root class GCC emits ivar entries for the
2462 // actual class structure. It is not clear if we need to follow this
2463 // behavior; for now lets try and get away with not doing it. If so,
2464 // the cleanest solution would be to make up an ObjCInterfaceDecl
2465 // for the class.
2466 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002467 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002468
2469 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002470 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002471
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002472 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002473 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002474
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002475 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2476 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002477 // Ignore unnamed bit-fields.
2478 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002479 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002480 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2481 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002482 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002483 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002484 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485 }
2486
2487 // Return null for empty list.
2488 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002489 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490
2491 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002492 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002493 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002494 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002495 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002496 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002497
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002498 llvm::GlobalVariable *GV;
2499 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002500 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002501 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002502 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002503 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002504 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002505 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002506 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002507 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002508}
2509
2510/*
2511 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002512 SEL method_name;
2513 char *method_types;
2514 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002515 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002516
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002517 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002518 struct objc_method_list *obsolete;
2519 int count;
2520 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002521 };
2522*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002523
2524/// GetMethodConstant - Return a struct objc_method constant for the
2525/// given method if it has been defined. The result is null if the
2526/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002527llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002528 // FIXME: Use DenseMap::lookup
2529 llvm::Function *Fn = MethodDefinitions[MD];
2530 if (!Fn)
2531 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002532
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002533 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002534 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002535 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002536 ObjCTypes.SelectorPtrTy);
2537 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002538 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002539 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002540}
2541
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002542llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002543 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002544 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002545 // Return null for empty list.
2546 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002547 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002548
2549 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002550 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002551 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002552 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002553 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002554 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002555 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002556
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002557 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002558 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002559 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002560}
2561
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002562llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002563 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002564 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002565 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002566
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002567 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002568 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002569 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002570 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002571 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002572 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002573 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002574 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002575 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002576
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002577 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002578}
2579
Daniel Dunbar30c65362009-03-09 20:09:19 +00002580llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002581CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002582 llvm::Constant *Init,
2583 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002584 unsigned Align,
2585 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002586 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002587 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002588 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002589 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002590 if (Section)
2591 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002592 if (Align)
2593 GV->setAlignment(Align);
2594 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002595 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002596 return GV;
2597}
2598
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002599llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002600 // Abuse this interface function as a place to finalize.
2601 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002602 return NULL;
2603}
2604
Chris Lattnerd4808922009-03-22 21:03:39 +00002605llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002606 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002607}
2608
Chris Lattnerd4808922009-03-22 21:03:39 +00002609llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002610 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002611}
2612
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002613llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2614 return ObjCTypes.getCopyStructFn();
2615}
2616
Chris Lattnerd4808922009-03-22 21:03:39 +00002617llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002618 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002619}
2620
John McCallbd309292010-07-06 01:34:17 +00002621void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2622 return EmitTryOrSynchronizedStmt(CGF, S);
2623}
2624
2625void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2626 const ObjCAtSynchronizedStmt &S) {
2627 return EmitTryOrSynchronizedStmt(CGF, S);
2628}
2629
John McCall65bea082010-07-21 06:59:36 +00002630namespace {
John McCallcda666c2010-07-21 07:22:38 +00002631 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002632 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002633 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002634 llvm::Value *CallTryExitVar;
2635 llvm::Value *ExceptionData;
2636 ObjCTypesHelper &ObjCTypes;
2637 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002638 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002639 llvm::Value *CallTryExitVar,
2640 llvm::Value *ExceptionData,
2641 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002642 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002643 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2644
2645 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2646 // Check whether we need to call objc_exception_try_exit.
2647 // In optimized code, this branch will always be folded.
2648 llvm::BasicBlock *FinallyCallExit =
2649 CGF.createBasicBlock("finally.call_exit");
2650 llvm::BasicBlock *FinallyNoCallExit =
2651 CGF.createBasicBlock("finally.no_call_exit");
2652 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2653 FinallyCallExit, FinallyNoCallExit);
2654
2655 CGF.EmitBlock(FinallyCallExit);
2656 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2657 ->setDoesNotThrow();
2658
2659 CGF.EmitBlock(FinallyNoCallExit);
2660
2661 if (isa<ObjCAtTryStmt>(S)) {
2662 if (const ObjCAtFinallyStmt* FinallyStmt =
2663 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2664 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2665
2666 // Currently, the end of the cleanup must always exist.
2667 CGF.EnsureInsertPoint();
2668 } else {
2669 // Emit objc_sync_exit(expr); as finally's sole statement for
2670 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002671 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002672 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2673 ->setDoesNotThrow();
2674 }
2675 }
2676 };
John McCall42227ed2010-07-31 23:20:56 +00002677
2678 class FragileHazards {
2679 CodeGenFunction &CGF;
2680 llvm::SmallVector<llvm::Value*, 20> Locals;
2681 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2682
2683 llvm::InlineAsm *ReadHazard;
2684 llvm::InlineAsm *WriteHazard;
2685
2686 llvm::FunctionType *GetAsmFnType();
2687
2688 void collectLocals();
2689 void emitReadHazard(CGBuilderTy &Builder);
2690
2691 public:
2692 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002693
John McCall42227ed2010-07-31 23:20:56 +00002694 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002695 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002696 };
2697}
2698
2699/// Create the fragile-ABI read and write hazards based on the current
2700/// state of the function, which is presumed to be immediately prior
2701/// to a @try block. These hazards are used to maintain correct
2702/// semantics in the face of optimization and the fragile ABI's
2703/// cavalier use of setjmp/longjmp.
2704FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2705 collectLocals();
2706
2707 if (Locals.empty()) return;
2708
2709 // Collect all the blocks in the function.
2710 for (llvm::Function::iterator
2711 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2712 BlocksBeforeTry.insert(&*I);
2713
2714 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2715
2716 // Create a read hazard for the allocas. This inhibits dead-store
2717 // optimizations and forces the values to memory. This hazard is
2718 // inserted before any 'throwing' calls in the protected scope to
2719 // reflect the possibility that the variables might be read from the
2720 // catch block if the call throws.
2721 {
2722 std::string Constraint;
2723 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2724 if (I) Constraint += ',';
2725 Constraint += "*m";
2726 }
2727
2728 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2729 }
2730
2731 // Create a write hazard for the allocas. This inhibits folding
2732 // loads across the hazard. This hazard is inserted at the
2733 // beginning of the catch path to reflect the possibility that the
2734 // variables might have been written within the protected scope.
2735 {
2736 std::string Constraint;
2737 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2738 if (I) Constraint += ',';
2739 Constraint += "=*m";
2740 }
2741
2742 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2743 }
2744}
2745
2746/// Emit a write hazard at the current location.
2747void FragileHazards::emitWriteHazard() {
2748 if (Locals.empty()) return;
2749
2750 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2751 ->setDoesNotThrow();
2752}
2753
John McCall42227ed2010-07-31 23:20:56 +00002754void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2755 assert(!Locals.empty());
2756 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2757 ->setDoesNotThrow();
2758}
2759
2760/// Emit read hazards in all the protected blocks, i.e. all the blocks
2761/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002762void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002763 if (Locals.empty()) return;
2764
2765 CGBuilderTy Builder(CGF.getLLVMContext());
2766
2767 // Iterate through all blocks, skipping those prior to the try.
2768 for (llvm::Function::iterator
2769 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2770 llvm::BasicBlock &BB = *FI;
2771 if (BlocksBeforeTry.count(&BB)) continue;
2772
2773 // Walk through all the calls in the block.
2774 for (llvm::BasicBlock::iterator
2775 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2776 llvm::Instruction &I = *BI;
2777
2778 // Ignore instructions that aren't non-intrinsic calls.
2779 // These are the only calls that can possibly call longjmp.
2780 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2781 if (isa<llvm::IntrinsicInst>(I))
2782 continue;
2783
2784 // Ignore call sites marked nounwind. This may be questionable,
2785 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2786 llvm::CallSite CS(&I);
2787 if (CS.doesNotThrow()) continue;
2788
John McCall2dd7d442010-08-04 05:59:32 +00002789 // Insert a read hazard before the call. This will ensure that
2790 // any writes to the locals are performed before making the
2791 // call. If the call throws, then this is sufficient to
2792 // guarantee correctness as long as it doesn't also write to any
2793 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002794 Builder.SetInsertPoint(&BB, BI);
2795 emitReadHazard(Builder);
2796 }
2797 }
2798}
2799
2800static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2801 if (V) S.insert(V);
2802}
2803
2804void FragileHazards::collectLocals() {
2805 // Compute a set of allocas to ignore.
2806 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2807 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2808 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2809 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2810
2811 // Collect all the allocas currently in the function. This is
2812 // probably way too aggressive.
2813 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2814 for (llvm::BasicBlock::iterator
2815 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2816 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2817 Locals.push_back(&*I);
2818}
2819
2820llvm::FunctionType *FragileHazards::GetAsmFnType() {
2821 std::vector<const llvm::Type *> Tys(Locals.size());
2822 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2823 Tys[I] = Locals[I]->getType();
2824 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002825}
2826
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002827/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002829 Objective-C setjmp-longjmp (sjlj) Exception Handling
2830 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002831
John McCallbd309292010-07-06 01:34:17 +00002832 A catch buffer is a setjmp buffer plus:
2833 - a pointer to the exception that was caught
2834 - a pointer to the previous exception data buffer
2835 - two pointers of reserved storage
2836 Therefore catch buffers form a stack, with a pointer to the top
2837 of the stack kept in thread-local storage.
2838
2839 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2840 objc_exception_try_exit pops the given catch buffer, which is
2841 required to be the top of the EH stack.
2842 objc_exception_throw pops the top of the EH stack, writes the
2843 thrown exception into the appropriate field, and longjmps
2844 to the setjmp buffer. It crashes the process (with a printf
2845 and an abort()) if there are no catch buffers on the stack.
2846 objc_exception_extract just reads the exception pointer out of the
2847 catch buffer.
2848
2849 There's no reason an implementation couldn't use a light-weight
2850 setjmp here --- something like __builtin_setjmp, but API-compatible
2851 with the heavyweight setjmp. This will be more important if we ever
2852 want to implement correct ObjC/C++ exception interactions for the
2853 fragile ABI.
2854
2855 Note that for this use of setjmp/longjmp to be correct, we may need
2856 to mark some local variables volatile: if a non-volatile local
2857 variable is modified between the setjmp and the longjmp, it has
2858 indeterminate value. For the purposes of LLVM IR, it may be
2859 sufficient to make loads and stores within the @try (to variables
2860 declared outside the @try) volatile. This is necessary for
2861 optimized correctness, but is not currently being done; this is
2862 being tracked as rdar://problem/8160285
2863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002864 The basic framework for a @try-catch-finally is as follows:
2865 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002866 objc_exception_data d;
2867 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002868 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002870 objc_exception_try_enter(&d);
2871 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002872 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002873 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002874 // exception path
2875 id _caught = objc_exception_extract(&d);
2876
2877 // enter new try scope for handlers
2878 if (!setjmp(d.jmp_buf)) {
2879 ... match exception and execute catch blocks ...
2880
2881 // fell off end, rethrow.
2882 _rethrow = _caught;
2883 ... jump-through-finally to finally_rethrow ...
2884 } else {
2885 // exception in catch block
2886 _rethrow = objc_exception_extract(&d);
2887 _call_try_exit = false;
2888 ... jump-through-finally to finally_rethrow ...
2889 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002890 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002891 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002892
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002893 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002894 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002895 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002896
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002897 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002898 ... dispatch to finally destination ...
2899
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002900 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002901 objc_exception_throw(_rethrow);
2902
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002903 finally_end:
2904 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002905
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002906 This framework differs slightly from the one gcc uses, in that gcc
2907 uses _rethrow to determine if objc_exception_try_exit should be called
2908 and if the object should be rethrown. This breaks in the face of
2909 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002910
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002911 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002912
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002913 - If there are no catch blocks, then we avoid emitting the second
2914 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002915
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002916 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2917 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002918
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002919 - FIXME: If there is no @finally block we can do a few more
2920 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002921
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002922 Rethrows and Jumps-Through-Finally
2923 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002924
John McCallbd309292010-07-06 01:34:17 +00002925 '@throw;' is supported by pushing the currently-caught exception
2926 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002927
John McCallbd309292010-07-06 01:34:17 +00002928 Branches through the @finally block are handled with an ordinary
2929 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2930 exceptions are not compatible with C++ exceptions, and this is
2931 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002932
John McCallbd309292010-07-06 01:34:17 +00002933 @synchronized(expr) { stmt; } is emitted as if it were:
2934 id synch_value = expr;
2935 objc_sync_enter(synch_value);
2936 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002937*/
2938
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002939void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2940 const Stmt &S) {
2941 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002942
2943 // A destination for the fall-through edges of the catch handlers to
2944 // jump to.
2945 CodeGenFunction::JumpDest FinallyEnd =
2946 CGF.getJumpDestInCurrentScope("finally.end");
2947
2948 // A destination for the rethrow edge of the catch handlers to jump
2949 // to.
2950 CodeGenFunction::JumpDest FinallyRethrow =
2951 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002952
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002953 // For @synchronized, call objc_sync_enter(sync.expr). The
2954 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002955 // @synchronized. We can't avoid a temp here because we need the
2956 // value to be preserved. If the backend ever does liveness
2957 // correctly after setjmp, this will be unnecessary.
2958 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002959 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002960 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002961 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2962 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002963 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2964 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002965
2966 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2967 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002968 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002969
John McCall2dd7d442010-08-04 05:59:32 +00002970 // Allocate memory for the setjmp buffer. This needs to be kept
2971 // live throughout the try and catch blocks.
2972 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2973 "exceptiondata.ptr");
2974
John McCall42227ed2010-07-31 23:20:56 +00002975 // Create the fragile hazards. Note that this will not capture any
2976 // of the allocas required for exception processing, but will
2977 // capture the current basic block (which extends all the way to the
2978 // setjmp call) as "before the @try".
2979 FragileHazards Hazards(CGF);
2980
John McCallbd309292010-07-06 01:34:17 +00002981 // Create a flag indicating whether the cleanup needs to call
2982 // objc_exception_try_exit. This is true except when
2983 // - no catches match and we're branching through the cleanup
2984 // just to rethrow the exception, or
2985 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002986 // The setjmp-safety rule here is that we should always store to this
2987 // variable in a place that dominates the branch through the cleanup
2988 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002989 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002990 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002991
John McCallbd309292010-07-06 01:34:17 +00002992 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002993 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002994 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002995 CallTryExitVar,
2996 ExceptionData,
2997 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002998
2999 // Enter a try block:
3000 // - Call objc_exception_try_enter to push ExceptionData on top of
3001 // the EH stack.
3002 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3003 ->setDoesNotThrow();
3004
3005 // - Call setjmp on the exception data buffer.
3006 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3007 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3008 llvm::Value *SetJmpBuffer =
3009 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3010 llvm::CallInst *SetJmpResult =
3011 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3012 SetJmpResult->setDoesNotThrow();
3013
3014 // If setjmp returned 0, enter the protected block; otherwise,
3015 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003016 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3017 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003018 llvm::Value *DidCatch =
3019 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
3020 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003021
John McCallbd309292010-07-06 01:34:17 +00003022 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003023 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003024 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003025 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003026 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003027
3028 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003029
John McCallbd309292010-07-06 01:34:17 +00003030 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003031 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003032
John McCall42227ed2010-07-31 23:20:56 +00003033 // Don't optimize loads of the in-scope locals across this point.
3034 Hazards.emitWriteHazard();
3035
John McCallbd309292010-07-06 01:34:17 +00003036 // For a @synchronized (or a @try with no catches), just branch
3037 // through the cleanup to the rethrow block.
3038 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3039 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003040 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003041 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003042
3043 // Otherwise, we have to match against the caught exceptions.
3044 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003045 // Retrieve the exception object. We may emit multiple blocks but
3046 // nothing can cross this so the value is already in SSA form.
3047 llvm::CallInst *Caught =
3048 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3049 ExceptionData, "caught");
3050 Caught->setDoesNotThrow();
3051
John McCallbd309292010-07-06 01:34:17 +00003052 // Push the exception to rethrow onto the EH value stack for the
3053 // benefit of any @throws in the handlers.
3054 CGF.ObjCEHValueStack.push_back(Caught);
3055
Douglas Gregor96c79492010-04-23 22:50:49 +00003056 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003057
John McCall2dd7d442010-08-04 05:59:32 +00003058 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003059
John McCall2dd7d442010-08-04 05:59:32 +00003060 llvm::BasicBlock *CatchBlock = 0;
3061 llvm::BasicBlock *CatchHandler = 0;
3062 if (HasFinally) {
3063 // Enter a new exception try block (in case a @catch block
3064 // throws an exception).
3065 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3066 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003067
John McCall2dd7d442010-08-04 05:59:32 +00003068 llvm::CallInst *SetJmpResult =
3069 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3070 "setjmp.result");
3071 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003072
John McCall2dd7d442010-08-04 05:59:32 +00003073 llvm::Value *Threw =
3074 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3075
3076 CatchBlock = CGF.createBasicBlock("catch");
3077 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3078 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3079
3080 CGF.EmitBlock(CatchBlock);
3081 }
3082
3083 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003084
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003085 // Handle catch list. As a special case we check if everything is
3086 // matched and avoid generating code for falling off the end if
3087 // so.
3088 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003089 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3090 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003091
Douglas Gregor46a572b2010-04-26 16:46:50 +00003092 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003093 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003094
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003095 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003096 if (!CatchParam) {
3097 AllMatched = true;
3098 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003099 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
John McCallbd309292010-07-06 01:34:17 +00003101 // catch(id e) always matches under this ABI, since only
3102 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003103 // FIXME: For the time being we also match id<X>; this should
3104 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003105 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003106 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003107 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003108
John McCallbd309292010-07-06 01:34:17 +00003109 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003110 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003111 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3112
Anders Carlsson9396a892008-09-11 09:15:33 +00003113 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00003114 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003115 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003116
3117 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003118 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003119 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003120
Anders Carlsson9396a892008-09-11 09:15:33 +00003121 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003122
3123 // The scope of the catch variable ends right here.
3124 CatchVarCleanups.ForceCleanup();
3125
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003126 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003127 break;
3128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003129
Steve Naroff7cae42b2009-07-10 23:34:53 +00003130 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003131 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003134 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3135 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003136
3137 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003138 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003139
John McCallbd309292010-07-06 01:34:17 +00003140 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003141 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3142 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003143 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003144
John McCallbd309292010-07-06 01:34:17 +00003145 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3146 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003147
3148 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003149 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003150
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003151 // Emit the @catch block.
3152 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003153
3154 // Collect any cleanups for the catch variable. The scope lasts until
3155 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003156 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003157
Steve Naroff371b8fb2009-03-03 19:52:17 +00003158 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003159 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003160
John McCallbd309292010-07-06 01:34:17 +00003161 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003162 llvm::Value *Tmp =
3163 CGF.Builder.CreateBitCast(Caught,
3164 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003165 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003166 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
Anders Carlsson9396a892008-09-11 09:15:33 +00003168 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003169
3170 // We're done with the catch variable.
3171 CatchVarCleanups.ForceCleanup();
3172
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003173 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003175 CGF.EmitBlock(NextCatchBlock);
3176 }
3177
John McCallbd309292010-07-06 01:34:17 +00003178 CGF.ObjCEHValueStack.pop_back();
3179
John McCall2dd7d442010-08-04 05:59:32 +00003180 // If nothing wanted anything to do with the caught exception,
3181 // kill the extract call.
3182 if (Caught->use_empty())
3183 Caught->eraseFromParent();
3184
3185 if (!AllMatched)
3186 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3187
3188 if (HasFinally) {
3189 // Emit the exception handler for the @catch blocks.
3190 CGF.EmitBlock(CatchHandler);
3191
3192 // In theory we might now need a write hazard, but actually it's
3193 // unnecessary because there's no local-accessing code between
3194 // the try's write hazard and here.
3195 //Hazards.emitWriteHazard();
3196
3197 // Don't pop the catch handler; the throw already did.
3198 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003199 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003200 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003201 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003202
John McCall42227ed2010-07-31 23:20:56 +00003203 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003204 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003205
John McCallbd309292010-07-06 01:34:17 +00003206 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003207 CGF.Builder.restoreIP(TryFallthroughIP);
3208 if (CGF.HaveInsertPoint())
3209 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003210 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003211 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003212
John McCallbd309292010-07-06 01:34:17 +00003213 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003214 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003215 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003216 if (CGF.HaveInsertPoint()) {
John McCall2dd7d442010-08-04 05:59:32 +00003217 // Just look in the buffer for the exception to throw.
3218 llvm::CallInst *Caught =
3219 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3220 ExceptionData);
3221 Caught->setDoesNotThrow();
3222
3223 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallbd309292010-07-06 01:34:17 +00003224 ->setDoesNotThrow();
3225 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003226 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003227
John McCall42227ed2010-07-31 23:20:56 +00003228 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003229}
3230
3231void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003232 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003233 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003234
Anders Carlssone005aa12008-09-09 16:16:55 +00003235 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3236 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003237 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003238 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3239 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003240 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003241 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003242 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003243 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003244
John McCallbd309292010-07-06 01:34:17 +00003245 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3246 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003247 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003248
3249 // Clear the insertion point to indicate we are in unreachable code.
3250 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003251}
3252
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003253/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003254/// object: objc_read_weak (id *src)
3255///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003256llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003257 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003258 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003259 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3260 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3261 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003262 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003263 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003264 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003265 return read_weak;
3266}
3267
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003268/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3269/// objc_assign_weak (id src, id *dst)
3270///
3271void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003272 llvm::Value *src, llvm::Value *dst) {
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);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003283 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003284 src, dst, "weakassign");
3285 return;
3286}
3287
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003288/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3289/// objc_assign_global (id src, id *dst)
3290///
3291void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003292 llvm::Value *src, llvm::Value *dst,
3293 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003294 const llvm::Type * SrcTy = src->getType();
3295 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003296 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003297 assert(Size <= 8 && "does not support size > 8");
3298 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003299 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003300 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3301 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003302 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3303 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003304 if (!threadlocal)
3305 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3306 src, dst, "globalassign");
3307 else
3308 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3309 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003310 return;
3311}
3312
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003313/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003314/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003315///
3316void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003317 llvm::Value *src, llvm::Value *dst,
3318 llvm::Value *ivarOffset) {
3319 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003320 const llvm::Type * SrcTy = src->getType();
3321 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003322 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003323 assert(Size <= 8 && "does not support size > 8");
3324 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003325 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003326 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3327 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003328 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3329 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003330 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3331 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003332 return;
3333}
3334
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003335/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3336/// objc_assign_strongCast (id src, id *dst)
3337///
3338void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003339 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003340 const llvm::Type * SrcTy = src->getType();
3341 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003342 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003343 assert(Size <= 8 && "does not support size > 8");
3344 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003345 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003346 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3347 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003348 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3349 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003350 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003351 src, dst, "weakassign");
3352 return;
3353}
3354
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003355void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003356 llvm::Value *DestPtr,
3357 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003358 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003359 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3360 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003361 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003362 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003363 return;
3364}
3365
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003366/// EmitObjCValueForIvar - Code Gen for ivar reference.
3367///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003368LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3369 QualType ObjectTy,
3370 llvm::Value *BaseValue,
3371 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003372 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003373 const ObjCInterfaceDecl *ID =
3374 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003375 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3376 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003377}
3378
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003379llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003380 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003381 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003382 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003383 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003384 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3385 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003386}
3387
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003388/* *** Private Interface *** */
3389
3390/// EmitImageInfo - Emit the image info marker used to encode some module
3391/// level information.
3392///
3393/// See: <rdr://4810609&4810587&4810587>
3394/// struct IMAGE_INFO {
3395/// unsigned version;
3396/// unsigned flags;
3397/// };
3398enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003399 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003400 eImageInfo_GarbageCollected = (1 << 1),
3401 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003402 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3403
Daniel Dunbar5e639272010-04-25 20:39:01 +00003404 // A flag indicating that the module has no instances of a @synthesize of a
3405 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003406 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003407};
3408
Daniel Dunbar5e639272010-04-25 20:39:01 +00003409void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003410 unsigned version = 0; // Version is unused?
3411 unsigned flags = 0;
3412
3413 // FIXME: Fix and continue?
3414 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3415 flags |= eImageInfo_GarbageCollected;
3416 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3417 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003418
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003419 // We never allow @synthesize of a superclass property.
3420 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003421
Chris Lattner5e016ae2010-06-27 07:15:29 +00003422 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3423
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003424 // Emitted as int[2];
3425 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003426 llvm::ConstantInt::get(Int32Ty, version),
3427 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003428 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003429 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003430
3431 const char *Section;
3432 if (ObjCABI == 1)
3433 Section = "__OBJC, __image_info,regular";
3434 else
3435 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003436 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003437 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003438 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003439 Section,
3440 0,
3441 true);
3442 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003443}
3444
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003445
3446// struct objc_module {
3447// unsigned long version;
3448// unsigned long size;
3449// const char *name;
3450// Symtab symtab;
3451// };
3452
3453// FIXME: Get from somewhere
3454static const int ModuleVersion = 7;
3455
3456void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003457 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003458
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003459 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003460 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3461 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003462 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003463 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003464 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003465 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003466 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003467 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003468 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003469}
3470
3471llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003472 unsigned NumClasses = DefinedClasses.size();
3473 unsigned NumCategories = DefinedCategories.size();
3474
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003475 // Return null if no symbols were defined.
3476 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003477 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003478
3479 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003480 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003481 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003482 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3483 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003484
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003485 // The runtime expects exactly the list of defined classes followed
3486 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003487 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003488 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003489 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003490 ObjCTypes.Int8PtrTy);
3491 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003492 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003493 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003494 ObjCTypes.Int8PtrTy);
3495
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003496 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003497 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003498 NumClasses + NumCategories),
3499 Symbols);
3500
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003501 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003502
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003503 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003504 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3505 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003506 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003507 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003508}
3509
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003510llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003511 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003512 LazySymbols.insert(ID->getIdentifier());
3513
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003514 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003515
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003516 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003517 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003518 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003519 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003520 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003521 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3522 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003523 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003524 }
3525
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003526 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003527}
3528
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003529llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3530 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003531 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003532
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003533 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003534 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003535 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003536 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003537 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003538 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3539 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003540 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003541 }
3542
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003543 if (lvalue)
3544 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003545 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003546}
3547
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003548llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003549 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003550
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003551 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003552 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003553 llvm::ConstantArray::get(VMContext,
3554 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003555 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003556 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003557
Owen Anderson170229f2009-07-14 23:10:40 +00003558 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003559}
3560
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003561/// GetIvarLayoutName - Returns a unique constant for the given
3562/// ivar layout bitmap.
3563llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003565 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003566}
3567
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003568void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003569 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003570 bool ForStrongLayout,
3571 bool &HasUnion) {
3572 const RecordDecl *RD = RT->getDecl();
3573 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003574 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003575 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003576 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003577 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003578
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003579 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3580 ForStrongLayout, HasUnion);
3581}
3582
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003583void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003584 const llvm::StructLayout *Layout,
3585 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003586 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003587 unsigned int BytePos, bool ForStrongLayout,
3588 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003589 bool IsUnion = (RD && RD->isUnion());
3590 uint64_t MaxUnionIvarSize = 0;
3591 uint64_t MaxSkippedUnionIvarSize = 0;
3592 FieldDecl *MaxField = 0;
3593 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003594 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003595 uint64_t MaxFieldOffset = 0;
3596 uint64_t MaxSkippedFieldOffset = 0;
3597 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003598
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003599 if (RecFields.empty())
3600 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003601 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3602 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3603
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003604 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003605 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003606 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003607 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003608 // Note that 'i' here is actually the field index inside RD of Field,
3609 // although this dependency is hidden.
3610 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3611 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003612 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003613 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003614
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003615 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003616 if (!Field->getIdentifier() || Field->isBitField()) {
3617 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003618 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003619 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003620 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003621
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003622 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003623 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003624 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003625 if (FQT->isUnionType())
3626 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003627
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003628 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003629 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003630 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003631 continue;
3632 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003633
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003634 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003635 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003636 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003637 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003638 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003639 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003640 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3641 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003642 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003643 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003644 FQT = CArray->getElementType();
3645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003646
3647 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003648 "layout for array of unions not supported");
3649 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003650 int OldIndex = IvarsInfo.size() - 1;
3651 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003652
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003653 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003654 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003655 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003656
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003657 // Replicate layout information for each array element. Note that
3658 // one element is already done.
3659 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003660 for (int FirstIndex = IvarsInfo.size() - 1,
3661 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003662 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003663 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3664 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3665 IvarsInfo[i].ivar_size));
3666 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3667 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3668 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003669 }
3670 continue;
3671 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003672 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003673 // At this point, we are done with Record/Union and array there of.
3674 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003675 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003676
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003677 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003678 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3679 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003680 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003681 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003682 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003683 MaxUnionIvarSize = UnionIvarSize;
3684 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003685 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003686 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003687 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003688 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003689 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003690 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003691 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003692 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3693 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003694 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003695 // FIXME: Why the asymmetry? We divide by word size in bits on other
3696 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003697 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003698 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003699 MaxSkippedUnionIvarSize = UnionIvarSize;
3700 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003701 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003702 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003703 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003704 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003705 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003706 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003707 }
3708 }
3709 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003710
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003711 if (LastFieldBitfield) {
3712 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003713 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3714 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003715 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003716 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003717 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003718 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3719 + ((BitFieldSize % ByteSizeInBits) != 0);
3720 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003721 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003722
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003723 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003724 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003725 MaxUnionIvarSize));
3726 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003727 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003728 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003729}
3730
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003731llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(bool hasUnion,
3732 std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003733 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003734 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003735
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003736 // Sort on byte position in case we encounterred a union nested in
3737 // the ivar list.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003738 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003739 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003740 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003741 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003742
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003743 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003744 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003745 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003746 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003747 if (IvarsInfo[0].ivar_bytepos == 0) {
3748 WordsToSkip = 0;
3749 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003750 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003751 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3752 WordsToScan = IvarsInfo[0].ivar_size;
3753 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003754 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003755 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003756 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003757 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003758 // consecutive 'scanned' object pointers.
3759 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003760 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003761 // Skip over 'gc'able object pointer which lay over each other.
3762 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3763 continue;
3764 // Must skip over 1 or more words. We save current skip/scan values
3765 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003766 SKIP_SCAN SkScan;
3767 SkScan.skip = WordsToSkip;
3768 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003769 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003770
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003771 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003772 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3773 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003774 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003775 WordsToSkip = 0;
3776 WordsToScan = IvarsInfo[i].ivar_size;
3777 }
3778 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003779 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003780 SKIP_SCAN SkScan;
3781 SkScan.skip = WordsToSkip;
3782 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003783 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003784 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003785
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003786 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003787 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003788 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003789 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003790 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003791 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003792 IvarsInfo[LastIndex].ivar_bytepos +
3793 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003794 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003795 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003796 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003797 SKIP_SCAN SkScan;
3798 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3799 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003800 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003801 }
3802 }
3803 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3804 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003805 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003806 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003807 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3808 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3809 // 0xM0 followed by 0x0N detected.
3810 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3811 for (int j = i+1; j < SkipScan; j++)
3812 SkipScanIvars[j] = SkipScanIvars[j+1];
3813 --SkipScan;
3814 }
3815 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003816
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003817 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003818 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003819 unsigned char byte;
3820 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3821 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3822 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3823 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003824
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003825 // first skip big.
3826 for (unsigned int ix = 0; ix < skip_big; ix++)
3827 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003828
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003829 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003830 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003831 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003832 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003833 byte |= 0xf;
3834 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003835 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003836 byte |= scan_small;
3837 scan_small = 0;
3838 }
3839 BitMap += byte;
3840 }
3841 // next scan big
3842 for (unsigned int ix = 0; ix < scan_big; ix++)
3843 BitMap += (unsigned char)(0x0f);
3844 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003845 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003846 byte = scan_small;
3847 BitMap += byte;
3848 }
3849 }
3850 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003851 unsigned char zero = 0;
3852 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003853
3854 llvm::GlobalVariable * Entry =
3855 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3856 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3857 "__TEXT,__cstring,cstring_literals",
3858 1, true);
3859 return getConstantGEP(VMContext, Entry, 0, 0);
3860}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003861
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003862/// BuildIvarLayout - Builds ivar layout bitmap for the class
3863/// implementation for the __strong or __weak case.
3864/// The layout map displays which words in ivar list must be skipped
3865/// and which must be scanned by GC (see below). String is built of bytes.
3866/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3867/// of words to skip and right nibble is count of words to scan. So, each
3868/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3869/// represented by a 0x00 byte which also ends the string.
3870/// 1. when ForStrongLayout is true, following ivars are scanned:
3871/// - id, Class
3872/// - object *
3873/// - __strong anything
3874///
3875/// 2. When ForStrongLayout is false, following ivars are scanned:
3876/// - __weak anything
3877///
3878llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3879 const ObjCImplementationDecl *OMD,
3880 bool ForStrongLayout) {
3881 bool hasUnion = false;
3882
3883 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3884 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3885 return llvm::Constant::getNullValue(PtrTy);
3886
3887 llvm::SmallVector<FieldDecl*, 32> RecFields;
3888 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3889 CGM.getContext().CollectObjCIvars(OI, RecFields);
3890
3891 // Add this implementations synthesized ivars.
3892 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3893 CGM.getContext().CollectNonClassIvars(OI, Ivars);
3894 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3895 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3896
3897 if (RecFields.empty())
3898 return llvm::Constant::getNullValue(PtrTy);
3899
3900 SkipIvars.clear();
3901 IvarsInfo.clear();
3902
3903 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3904 if (IvarsInfo.empty())
3905 return llvm::Constant::getNullValue(PtrTy);
3906 std::string BitMap;
3907 llvm::Constant *C = BuildIvarLayoutBitmap(hasUnion, BitMap);
3908
3909 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003910 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003911 ForStrongLayout ? "strong" : "weak",
3912 OMD->getClassInterface()->getNameAsCString());
3913 const unsigned char *s = (unsigned char*)BitMap.c_str();
3914 for (unsigned i = 0; i < BitMap.size(); i++)
3915 if (!(s[i] & 0xf0))
3916 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3917 else
3918 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3919 printf("\n");
3920 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003921 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003922}
3923
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003924llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003925 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3926
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003927 // FIXME: Avoid std::string copying.
3928 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003929 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003930 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003931 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003932 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003933
Owen Anderson170229f2009-07-14 23:10:40 +00003934 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003935}
3936
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003937// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003938llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003939 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3940}
3941
3942// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003943llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003944 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3945}
3946
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003947llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003948 std::string TypeStr;
3949 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3950
3951 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003952
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003953 if (!Entry)
3954 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003955 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003956 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003957 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003958
Owen Anderson170229f2009-07-14 23:10:40 +00003959 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003960}
3961
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003962llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003963 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003964 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3965 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003966
3967 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3968
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003969 if (!Entry)
3970 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003971 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003972 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003973 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003974
Owen Anderson170229f2009-07-14 23:10:40 +00003975 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003976}
3977
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003978// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003979llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003980 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003981
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003982 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003983 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003984 llvm::ConstantArray::get(VMContext,
3985 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003986 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003987 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003988
Owen Anderson170229f2009-07-14 23:10:40 +00003989 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003990}
3991
3992// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003993// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003994llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003995CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3996 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003997 std::string TypeStr;
3998 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003999 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4000}
4001
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004002void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004003 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004004 llvm::SmallVectorImpl<char> &Name) {
4005 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004006 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004007 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4008 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004009 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004010 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004011 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004012 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004013}
4014
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004015void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004016 EmitModuleInfo();
4017
Daniel Dunbarc475d422008-10-29 22:36:39 +00004018 // Emit the dummy bodies for any protocols which were referenced but
4019 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004020 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004021 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4022 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004023 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004024
Daniel Dunbarc475d422008-10-29 22:36:39 +00004025 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004026 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004027 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004028 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004029 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004030 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004031 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004032 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004033 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004034 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004035 }
4036
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004037 // Add assembler directives to add lazy undefined symbol references
4038 // for classes which are referenced but not defined. This is
4039 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004040 //
4041 // FIXME: It would be nice if we had an LLVM construct for this.
4042 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4043 llvm::SmallString<256> Asm;
4044 Asm += CGM.getModule().getModuleInlineAsm();
4045 if (!Asm.empty() && Asm.back() != '\n')
4046 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004047
Daniel Dunbard027a922009-09-07 00:20:42 +00004048 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004049 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4050 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004051 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4052 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004053 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004054 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004055 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004056 }
4057
4058 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4059 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4060 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4061 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004062
Daniel Dunbard027a922009-09-07 00:20:42 +00004063 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004064 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004065}
4066
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004067CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004068 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004069 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004070 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004071 ObjCABI = 2;
4072}
4073
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004074/* *** */
4075
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004076ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004077 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004078 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4079 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004080
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004081 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004082 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004083 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004084 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004085 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004086
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004087 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004088 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004089 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004090
Mike Stump18bb9282009-05-16 07:57:57 +00004091 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4092 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004093 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004094 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004096 // I'm not sure I like this. The implicit coordination is a bit
4097 // gross. We should solve this in a reasonable fashion because this
4098 // is a pretty common task (match some runtime data structure with
4099 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004100
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004101 // FIXME: This is leaked.
4102 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004103
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004104 // struct _objc_super {
4105 // id self;
4106 // Class cls;
4107 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004108 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004109 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004110 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004111 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004112 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004113 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004114 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004115 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004116 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004117
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004118 SuperCTy = Ctx.getTagDeclType(RD);
4119 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004120
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004121 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4123
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004124 // struct _prop_t {
4125 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004126 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004127 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004128 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004129 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004130 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004131
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004132 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004133 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004134 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004135 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004136 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004137 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004138 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004139 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004140 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004141 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004142 PropertyListTy);
4143 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004144 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004145
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004146 // struct _objc_method {
4147 // SEL _cmd;
4148 // char *method_type;
4149 // char *_imp;
4150 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004151 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004152 Int8PtrTy,
4153 Int8PtrTy,
4154 NULL);
4155 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004157 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004158 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004159 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004160 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004161}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004162
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004163ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004164 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004165 // struct _objc_method_description {
4166 // SEL name;
4167 // char *types;
4168 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004169 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004170 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004171 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004172 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004173 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004174 MethodDescriptionTy);
4175
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004176 // struct _objc_method_description_list {
4177 // int count;
4178 // struct _objc_method_description[1];
4179 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004180 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004181 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004182 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004183 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004185 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004186
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004187 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004188 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004189 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004190
Daniel Dunbarb036db82008-08-13 03:21:16 +00004191 // Protocol description structures
4192
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004193 // struct _objc_protocol_extension {
4194 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4195 // struct _objc_method_description_list *optional_instance_methods;
4196 // struct _objc_method_description_list *optional_class_methods;
4197 // struct _objc_property_list *instance_properties;
4198 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004199 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004200 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004201 MethodDescriptionListPtrTy,
4202 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004203 PropertyListPtrTy,
4204 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004205 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004206 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004207
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004208 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004209 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004210
Daniel Dunbarc475d422008-10-29 22:36:39 +00004211 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004212
Owen Andersonc36edfe2009-08-13 23:27:53 +00004213 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4214 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004215
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004216 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004217 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004218 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004219 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004220 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004221 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004222 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4223
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004224 // struct _objc_protocol {
4225 // struct _objc_protocol_extension *isa;
4226 // char *protocol_name;
4227 // struct _objc_protocol **_objc_protocol_list;
4228 // struct _objc_method_description_list *instance_methods;
4229 // struct _objc_method_description_list *class_methods;
4230 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004231 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004232 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004233 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004234 MethodDescriptionListPtrTy,
4235 MethodDescriptionListPtrTy,
4236 NULL);
4237 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4238
4239 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004240 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004241 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004242 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004243 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004244
4245 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004246 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004247 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004248
4249 // Class description structures
4250
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004251 // struct _objc_ivar {
4252 // char *ivar_name;
4253 // char *ivar_type;
4254 // int ivar_offset;
4255 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004256 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004257 Int8PtrTy,
4258 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004259 NULL);
4260 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4261
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004262 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004263 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004264 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004265 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004266
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004267 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004268 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004269 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004270 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004271
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004272 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004273 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004274 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004275 Int8PtrTy,
4276 PropertyListPtrTy,
4277 NULL);
4278 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004279 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004280
Owen Andersonc36edfe2009-08-13 23:27:53 +00004281 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004282
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004283 // struct _objc_class {
4284 // Class isa;
4285 // Class super_class;
4286 // char *name;
4287 // long version;
4288 // long info;
4289 // long instance_size;
4290 // struct _objc_ivar_list *ivars;
4291 // struct _objc_method_list *methods;
4292 // struct _objc_cache *cache;
4293 // struct _objc_protocol_list *protocols;
4294 // char *ivar_layout;
4295 // struct _objc_class_ext *ext;
4296 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004297 T = llvm::StructType::get(VMContext,
4298 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004299 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004300 Int8PtrTy,
4301 LongTy,
4302 LongTy,
4303 LongTy,
4304 IvarListPtrTy,
4305 MethodListPtrTy,
4306 CachePtrTy,
4307 ProtocolListPtrTy,
4308 Int8PtrTy,
4309 ClassExtensionPtrTy,
4310 NULL);
4311 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004312
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004313 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4314 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004315 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004316
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004317 // struct _objc_category {
4318 // char *category_name;
4319 // char *class_name;
4320 // struct _objc_method_list *instance_method;
4321 // struct _objc_method_list *class_method;
4322 // uint32_t size; // sizeof(struct _objc_category)
4323 // struct _objc_property_list *instance_properties;// category's @property
4324 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004325 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004326 Int8PtrTy,
4327 MethodListPtrTy,
4328 MethodListPtrTy,
4329 ProtocolListPtrTy,
4330 IntTy,
4331 PropertyListPtrTy,
4332 NULL);
4333 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4334
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004335 // Global metadata structures
4336
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004337 // struct _objc_symtab {
4338 // long sel_ref_cnt;
4339 // SEL *refs;
4340 // short cls_def_cnt;
4341 // short cat_def_cnt;
4342 // char *defs[cls_def_cnt + cat_def_cnt];
4343 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004344 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004345 SelectorPtrTy,
4346 ShortTy,
4347 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004348 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004349 NULL);
4350 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004351 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004352
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004353 // struct _objc_module {
4354 // long version;
4355 // long size; // sizeof(struct _objc_module)
4356 // char *name;
4357 // struct _objc_symtab* symtab;
4358 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004359 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004360 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004361 LongTy,
4362 Int8PtrTy,
4363 SymtabPtrTy,
4364 NULL);
4365 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004366
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367
Mike Stump18bb9282009-05-16 07:57:57 +00004368 // FIXME: This is the size of the setjmp buffer and should be target
4369 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004370 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004371
Anders Carlsson9ff22482008-09-09 10:10:21 +00004372 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004373 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004374 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004375
4376 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004377 llvm::StructType::get(VMContext,
4378 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4379 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004380 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004381 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004382 ExceptionDataTy);
4383
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004384}
4385
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004386ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004387 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004388 // struct _method_list_t {
4389 // uint32_t entsize; // sizeof(struct _objc_method)
4390 // uint32_t method_count;
4391 // struct _objc_method method_list[method_count];
4392 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004393 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004394 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004395 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004396 NULL);
4397 CGM.getModule().addTypeName("struct.__method_list_t",
4398 MethodListnfABITy);
4399 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004400 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004401
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004402 // struct _protocol_t {
4403 // id isa; // NULL
4404 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004405 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004406 // const struct method_list_t * const instance_methods;
4407 // const struct method_list_t * const class_methods;
4408 // const struct method_list_t *optionalInstanceMethods;
4409 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004410 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004411 // const uint32_t size; // sizeof(struct _protocol_t)
4412 // const uint32_t flags; // = 0
4413 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004414
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004415 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004416 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004417
Owen Anderson758428f2009-08-05 23:18:46 +00004418 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004419 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004420 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004421 ProtocolListTyHolder),
4422 MethodListnfABIPtrTy,
4423 MethodListnfABIPtrTy,
4424 MethodListnfABIPtrTy,
4425 MethodListnfABIPtrTy,
4426 PropertyListPtrTy,
4427 IntTy,
4428 IntTy,
4429 NULL);
4430 CGM.getModule().addTypeName("struct._protocol_t",
4431 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004432
4433 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004434 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004435
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004436 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004437 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004438 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004439 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004440 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004441 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004442 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004443 NULL);
4444 CGM.getModule().addTypeName("struct._objc_protocol_list",
4445 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004446 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004447 ProtocolListnfABITy);
4448
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004449 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004450 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004451
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004452 // struct _ivar_t {
4453 // unsigned long int *offset; // pointer to ivar offset location
4454 // char *name;
4455 // char *type;
4456 // uint32_t alignment;
4457 // uint32_t size;
4458 // }
Mike Stump11289f42009-09-09 15:08:12 +00004459 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004460 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004461 Int8PtrTy,
4462 Int8PtrTy,
4463 IntTy,
4464 IntTy,
4465 NULL);
4466 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004467
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004468 // struct _ivar_list_t {
4469 // uint32 entsize; // sizeof(struct _ivar_t)
4470 // uint32 count;
4471 // struct _iver_t list[count];
4472 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004473 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004474 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004475 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004476 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004477 NULL);
4478 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004479
Owen Anderson9793f0e2009-07-29 22:16:19 +00004480 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004481
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004482 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004483 // uint32_t const flags;
4484 // uint32_t const instanceStart;
4485 // uint32_t const instanceSize;
4486 // uint32_t const reserved; // only when building for 64bit targets
4487 // const uint8_t * const ivarLayout;
4488 // const char *const name;
4489 // const struct _method_list_t * const baseMethods;
4490 // const struct _objc_protocol_list *const baseProtocols;
4491 // const struct _ivar_list_t *const ivars;
4492 // const uint8_t * const weakIvarLayout;
4493 // const struct _prop_list_t * const properties;
4494 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004495
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004496 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004497 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004498 IntTy,
4499 IntTy,
4500 Int8PtrTy,
4501 Int8PtrTy,
4502 MethodListnfABIPtrTy,
4503 ProtocolListnfABIPtrTy,
4504 IvarListnfABIPtrTy,
4505 Int8PtrTy,
4506 PropertyListPtrTy,
4507 NULL);
4508 CGM.getModule().addTypeName("struct._class_ro_t",
4509 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004510
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004511 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4512 std::vector<const llvm::Type*> Params;
4513 Params.push_back(ObjectPtrTy);
4514 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004515 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004516 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4517
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004518 // struct _class_t {
4519 // struct _class_t *isa;
4520 // struct _class_t * const superclass;
4521 // void *cache;
4522 // IMP *vtable;
4523 // struct class_ro_t *ro;
4524 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525
Owen Andersonc36edfe2009-08-13 23:27:53 +00004526 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004527 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004528 llvm::StructType::get(VMContext,
4529 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004530 llvm::PointerType::getUnqual(ClassTyHolder),
4531 CachePtrTy,
4532 llvm::PointerType::getUnqual(ImpnfABITy),
4533 llvm::PointerType::getUnqual(ClassRonfABITy),
4534 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004535 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4536
4537 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004538 ClassnfABITy);
4539
Fariborz Jahanian71394042009-01-23 23:53:38 +00004540 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004541 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004542
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004543 // struct _category_t {
4544 // const char * const name;
4545 // struct _class_t *const cls;
4546 // const struct _method_list_t * const instance_methods;
4547 // const struct _method_list_t * const class_methods;
4548 // const struct _protocol_list_t * const protocols;
4549 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004550 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004551 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004552 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004553 MethodListnfABIPtrTy,
4554 MethodListnfABIPtrTy,
4555 ProtocolListnfABIPtrTy,
4556 PropertyListPtrTy,
4557 NULL);
4558 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004559
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004560 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004561 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4562 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004563
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004564 // MessageRefTy - LLVM for:
4565 // struct _message_ref_t {
4566 // IMP messenger;
4567 // SEL name;
4568 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004569
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004570 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004571 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004572 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004573 SourceLocation(),
4574 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004575 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004576 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004577 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004578 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004579 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004580
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004581 MessageRefCTy = Ctx.getTagDeclType(RD);
4582 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4583 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004584
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004585 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004586 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004587
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004588 // SuperMessageRefTy - LLVM for:
4589 // struct _super_message_ref_t {
4590 // SUPER_IMP messenger;
4591 // SEL name;
4592 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004593 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004594 SelectorPtrTy,
4595 NULL);
4596 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004598 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004599 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4600
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004601
4602 // struct objc_typeinfo {
4603 // const void** vtable; // objc_ehtype_vtable + 2
4604 // const char* name; // c++ typeinfo string
4605 // Class cls;
4606 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004607 EHTypeTy = llvm::StructType::get(VMContext,
4608 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004609 Int8PtrTy,
4610 ClassnfABIPtrTy,
4611 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004612 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004613 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004614}
4615
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004616llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004617 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Fariborz Jahanian71394042009-01-23 23:53:38 +00004619 return NULL;
4620}
4621
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004622void CGObjCNonFragileABIMac::AddModuleClassList(const
4623 std::vector<llvm::GlobalValue*>
4624 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 const char *SymbolName,
4626 const char *SectionName) {
4627 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004628
Daniel Dunbar19573e72009-05-15 21:48:48 +00004629 if (!NumClasses)
4630 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004631
Daniel Dunbar19573e72009-05-15 21:48:48 +00004632 std::vector<llvm::Constant*> Symbols(NumClasses);
4633 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004634 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004635 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004636 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004637 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004638 NumClasses),
4639 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004640
Daniel Dunbar19573e72009-05-15 21:48:48 +00004641 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004642 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004643 llvm::GlobalValue::InternalLinkage,
4644 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004645 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004646 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004647 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004648 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004649}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650
Fariborz Jahanian71394042009-01-23 23:53:38 +00004651void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4652 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Daniel Dunbar19573e72009-05-15 21:48:48 +00004654 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004655 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004656 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004657 "\01L_OBJC_LABEL_CLASS_$",
4658 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004659
Fariborz Jahanian67260552009-11-17 21:37:35 +00004660 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4661 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4662 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4663 continue;
4664 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004665 }
4666
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004667 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4668 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4669 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4670 continue;
4671 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4672 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004673
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004674 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004675 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4676 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004677
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004678 // Build list of all implemented category addresses in array
4679 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004680 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004681 "\01L_OBJC_LABEL_CATEGORY_$",
4682 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004684 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4685 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004686
Daniel Dunbar5e639272010-04-25 20:39:01 +00004687 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004688}
4689
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004690/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004691/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004692/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004693/// message dispatch call for all the rest.
4694///
4695bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004696 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4697 default:
4698 assert(0 && "Invalid dispatch method!");
4699 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004700 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004701 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004702 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004703 case CodeGenOptions::Mixed:
4704 break;
4705 }
4706
4707 // If so, see whether this selector is in the white-list of things which must
4708 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004709 if (NonLegacyDispatchMethods.empty()) {
4710 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4711 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4712 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4713 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4714 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4715 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4716 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4717 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4718 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4719 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004720
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004721 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4722 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4723 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4724 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4725 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4726 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4727 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4728 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004729 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004730 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004731 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4732 &CGM.getContext().Idents.get("objects"),
4733 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004734 };
4735 NonLegacyDispatchMethods.insert(
4736 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004737 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004738
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004739 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004740}
4741
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004742// Metadata flags
4743enum MetaDataDlags {
4744 CLS = 0x0,
4745 CLS_META = 0x1,
4746 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004747 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004748 CLS_EXCEPTION = 0x20
4749};
4750/// BuildClassRoTInitializer - generate meta-data for:
4751/// struct _class_ro_t {
4752/// uint32_t const flags;
4753/// uint32_t const instanceStart;
4754/// uint32_t const instanceSize;
4755/// uint32_t const reserved; // only when building for 64bit targets
4756/// const uint8_t * const ivarLayout;
4757/// const char *const name;
4758/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004759/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004760/// const struct _ivar_list_t *const ivars;
4761/// const uint8_t * const weakIvarLayout;
4762/// const struct _prop_list_t * const properties;
4763/// }
4764///
4765llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004766 unsigned flags,
4767 unsigned InstanceStart,
4768 unsigned InstanceSize,
4769 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004770 std::string ClassName = ID->getNameAsString();
4771 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004772 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4773 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4774 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004775 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004776 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4777 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004778 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004779 // const struct _method_list_t * const baseMethods;
4780 std::vector<llvm::Constant*> Methods;
4781 std::string MethodListName("\01l_OBJC_$_");
4782 if (flags & CLS_META) {
4783 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004784 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004785 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004786 // Class methods should always be defined.
4787 Methods.push_back(GetMethodConstant(*i));
4788 }
4789 } else {
4790 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004791 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004792 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004793 // Instance methods should always be defined.
4794 Methods.push_back(GetMethodConstant(*i));
4795 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004796 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004797 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004798 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004800 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4801 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004803 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4804 if (llvm::Constant *C = GetMethodConstant(MD))
4805 Methods.push_back(C);
4806 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4807 if (llvm::Constant *C = GetMethodConstant(MD))
4808 Methods.push_back(C);
4809 }
4810 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004811 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004812 Values[ 5] = EmitMethodList(MethodListName,
4813 "__DATA, __objc_const", Methods);
4814
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004815 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4816 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004817 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004818 + OID->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004819 OID->protocol_begin(),
4820 OID->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004821
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004822 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004823 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004824 else
4825 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004826 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4827 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004828 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004829 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004830 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004831 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4832 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004833 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004834 Values);
4835 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004836 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4837 llvm::GlobalValue::InternalLinkage,
4838 Init,
4839 (flags & CLS_META) ?
4840 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4841 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004842 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004843 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004844 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004845 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004846
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004847}
4848
4849/// BuildClassMetaData - This routine defines that to-level meta-data
4850/// for the given ClassName for:
4851/// struct _class_t {
4852/// struct _class_t *isa;
4853/// struct _class_t * const superclass;
4854/// void *cache;
4855/// IMP *vtable;
4856/// struct class_ro_t *ro;
4857/// }
4858///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004859llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004860 std::string &ClassName,
4861 llvm::Constant *IsAGV,
4862 llvm::Constant *SuperClassGV,
4863 llvm::Constant *ClassRoGV,
4864 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004865 std::vector<llvm::Constant*> Values(5);
4866 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004867 Values[1] = SuperClassGV;
4868 if (!Values[1])
4869 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004870 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4871 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4872 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004873 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004874 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004875 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4876 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004877 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004878 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004879 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004880 if (HiddenVisibility)
4881 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004882 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004883}
4884
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004885bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004886CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004887 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004888}
4889
Daniel Dunbar961202372009-05-03 12:57:56 +00004890void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004891 uint32_t &InstanceStart,
4892 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004893 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004894 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004895
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004896 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004897 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004898
4899 // If there are no fields, the start is the same as the end.
4900 if (!RL.getFieldCount())
4901 InstanceStart = InstanceSize;
4902 else
4903 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004904}
4905
Fariborz Jahanian71394042009-01-23 23:53:38 +00004906void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4907 std::string ClassName = ID->getNameAsString();
4908 if (!ObjCEmptyCacheVar) {
4909 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004910 CGM.getModule(),
4911 ObjCTypes.CacheTy,
4912 false,
4913 llvm::GlobalValue::ExternalLinkage,
4914 0,
4915 "_objc_empty_cache");
4916
Fariborz Jahanian71394042009-01-23 23:53:38 +00004917 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004918 CGM.getModule(),
4919 ObjCTypes.ImpnfABITy,
4920 false,
4921 llvm::GlobalValue::ExternalLinkage,
4922 0,
4923 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004924 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004925 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004926 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004927 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004929 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004930 uint32_t InstanceSize = InstanceStart;
4931 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004932 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4933 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004934
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004935 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004936
4937 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004938 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004939 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004940 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004941 if (ID->getNumIvarInitializers())
4942 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004943 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004944 // class is root
4945 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004946 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004947 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004948 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004949 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004950 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4951 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4952 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004953 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004954 if (Root->hasAttr<WeakImportAttr>())
4955 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004956 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004957 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004958 ObjCMetaClassName +
4959 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004960 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004961 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4962 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004963 }
4964 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4965 InstanceStart,
4966 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004967 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004968 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004969 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4970 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004971 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004972
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004973 // Metadata for the class
4974 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004975 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004976 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004977 if (ID->getNumIvarInitializers())
4978 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004979
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004980 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004981 flags |= CLS_EXCEPTION;
4982
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004983 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004984 flags |= CLS_ROOT;
4985 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004986 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004987 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004988 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004989 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004990 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004991 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4992 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004993 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004994 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004995 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004996 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004997 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004998 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004999
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005000 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005001 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005002 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5003 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005004 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005005
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005006 // Determine if this class is also "non-lazy".
5007 if (ImplementationIsNonLazy(ID))
5008 DefinedNonLazyClasses.push_back(ClassMD);
5009
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005010 // Force the definition of the EHType if necessary.
5011 if (flags & CLS_EXCEPTION)
5012 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00005013}
5014
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005015/// GenerateProtocolRef - This routine is called to generate code for
5016/// a protocol reference expression; as in:
5017/// @code
5018/// @protocol(Proto1);
5019/// @endcode
5020/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5021/// which will hold address of the protocol meta-data.
5022///
5023llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005024 const ObjCProtocolDecl *PD) {
5025
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005026 // This routine is called for @protocol only. So, we must build definition
5027 // of protocol's meta-data (not a reference to it!)
5028 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005029 llvm::Constant *Init =
5030 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5031 ObjCTypes.ExternalProtocolPtrTy);
5032
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005033 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
5034 ProtocolName += PD->getNameAsCString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005035
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005036 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5037 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005038 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005039 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005040 CGM.getModule(),
5041 Init->getType(), false,
5042 llvm::GlobalValue::WeakAnyLinkage,
5043 Init,
5044 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005045 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5046 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005047 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005048 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005049}
5050
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005051/// GenerateCategory - Build metadata for a category implementation.
5052/// struct _category_t {
5053/// const char * const name;
5054/// struct _class_t *const cls;
5055/// const struct _method_list_t * const instance_methods;
5056/// const struct _method_list_t * const class_methods;
5057/// const struct _protocol_list_t * const protocols;
5058/// const struct _prop_list_t * const properties;
5059/// }
5060///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005061void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005062 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005063 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5065 "_$_" + OCD->getNameAsString());
5066 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005067 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005068
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005069 std::vector<llvm::Constant*> Values(6);
5070 Values[0] = GetClassName(OCD->getIdentifier());
5071 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005072 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005073 if (Interface->hasAttr<WeakImportAttr>())
5074 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5075
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005076 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005077 std::vector<llvm::Constant*> Methods;
5078 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005080 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005081
5082 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005083 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005084 // Instance methods should always be defined.
5085 Methods.push_back(GetMethodConstant(*i));
5086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087
5088 Values[2] = EmitMethodList(MethodListName,
5089 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005090 Methods);
5091
5092 MethodListName = Prefix;
5093 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5094 OCD->getNameAsString();
5095 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005096 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005097 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005098 // Class methods should always be defined.
5099 Methods.push_back(GetMethodConstant(*i));
5100 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005101
5102 Values[3] = EmitMethodList(MethodListName,
5103 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005104 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005105 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005106 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005107 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005108 llvm::SmallString<256> ExtName;
5109 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5110 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005111 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005112 + Interface->getName() + "_$_"
5113 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005114 Category->protocol_begin(),
5115 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005116 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5117 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005118 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005119 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5120 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005121 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005122
5123 llvm::Constant *Init =
5124 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005125 Values);
5126 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005127 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005128 false,
5129 llvm::GlobalValue::InternalLinkage,
5130 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005131 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005132 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005133 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005134 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005135 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005136 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005137
5138 // Determine if this category is also "non-lazy".
5139 if (ImplementationIsNonLazy(OCD))
5140 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005141}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005142
5143/// GetMethodConstant - Return a struct objc_method constant for the
5144/// given method if it has been defined. The result is null if the
5145/// method has not been defined. The return value has type MethodPtrTy.
5146llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005147 const ObjCMethodDecl *MD) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005148 // FIXME: Use DenseMap::lookup
5149 llvm::Function *Fn = MethodDefinitions[MD];
5150 if (!Fn)
5151 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005152
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005153 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005155 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005156 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005157 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005158 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005159 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005160}
5161
5162/// EmitMethodList - Build meta-data for method declarations
5163/// struct _method_list_t {
5164/// uint32_t entsize; // sizeof(struct _objc_method)
5165/// uint32_t method_count;
5166/// struct _objc_method method_list[method_count];
5167/// }
5168///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005169llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5170 const char *Section,
5171 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005172 // Return null for empty list.
5173 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005174 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005175
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005176 std::vector<llvm::Constant*> Values(3);
5177 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005178 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005179 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005180 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005181 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005182 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005183 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005184 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005185 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005186
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005187 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005188 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005189 llvm::GlobalValue::InternalLinkage,
5190 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005191 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005192 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005193 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005194 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005195 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005196 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005197 ObjCTypes.MethodListnfABIPtrTy);
5198}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005199
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005200/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5201/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005202llvm::GlobalVariable *
5203CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5204 const ObjCIvarDecl *Ivar) {
5205 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005206 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005207 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005208 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005209 CGM.getModule().getGlobalVariable(Name);
5210 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005212 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005213 false,
5214 llvm::GlobalValue::ExternalLinkage,
5215 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005216 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005217 return IvarOffsetGV;
5218}
5219
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005220llvm::Constant *
5221CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5222 const ObjCIvarDecl *Ivar,
5223 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005224 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005225 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005226 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005227 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005228 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005229
Mike Stump18bb9282009-05-16 07:57:57 +00005230 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5231 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005232 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5233 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5234 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005235 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005236 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005237 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005238 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005239 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005240}
5241
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005242/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005243/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005244/// IvarListnfABIPtrTy.
5245/// struct _ivar_t {
5246/// unsigned long int *offset; // pointer to ivar offset location
5247/// char *name;
5248/// char *type;
5249/// uint32_t alignment;
5250/// uint32_t size;
5251/// }
5252/// struct _ivar_list_t {
5253/// uint32 entsize; // sizeof(struct _ivar_t)
5254/// uint32 count;
5255/// struct _iver_t list[count];
5256/// }
5257///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005258
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005259llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005260 const ObjCImplementationDecl *ID) {
5261
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005262 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005263
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005264 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5265 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005266
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005267 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005268
Daniel Dunbarae032262009-04-20 00:33:43 +00005269 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005270 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005271 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005272
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005273 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5274 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005275 // Ignore unnamed bit-fields.
5276 if (!IVD->getDeclName())
5277 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005278 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005279 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005280 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5281 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005282 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005283 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005284 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005285 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005286 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005287 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005288 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005289 // NOTE. Size of a bitfield does not match gcc's, because of the
5290 // way bitfields are treated special in each. But I am told that
5291 // 'size' for bitfield ivars is ignored by the runtime so it does
5292 // not matter. If it matters, there is enough info to get the
5293 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005294 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005295 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005296 }
5297 // Return null for empty list.
5298 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005299 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005300 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005301 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005302 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5303 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005304 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005305 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005306 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005307 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005308 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5309 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005310 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005311 llvm::GlobalValue::InternalLinkage,
5312 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005313 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005314 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005315 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005316 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005317
Chris Lattnerf56501c2009-07-17 23:57:13 +00005318 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005319 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005320}
5321
5322llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005324 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005325
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005326 if (!Entry) {
5327 // We use the initializer as a marker of whether this is a forward
5328 // reference or not. At module finalization we add the empty
5329 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005330 Entry =
5331 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5332 llvm::GlobalValue::ExternalLinkage,
5333 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005334 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005335 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005336 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005337
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005338 return Entry;
5339}
5340
5341/// GetOrEmitProtocol - Generate the protocol meta-data:
5342/// @code
5343/// struct _protocol_t {
5344/// id isa; // NULL
5345/// const char * const protocol_name;
5346/// const struct _protocol_list_t * protocol_list; // super protocols
5347/// const struct method_list_t * const instance_methods;
5348/// const struct method_list_t * const class_methods;
5349/// const struct method_list_t *optionalInstanceMethods;
5350/// const struct method_list_t *optionalClassMethods;
5351/// const struct _prop_list_t * properties;
5352/// const uint32_t size; // sizeof(struct _protocol_t)
5353/// const uint32_t flags; // = 0
5354/// }
5355/// @endcode
5356///
5357
5358llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005359 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005360 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005361
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005362 // Early exit if a defining object has already been generated.
5363 if (Entry && Entry->hasInitializer())
5364 return Entry;
5365
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005366 // Construct method lists.
5367 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5368 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005369 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005370 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005371 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005372 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005373 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5374 OptInstanceMethods.push_back(C);
5375 } else {
5376 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005377 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005379
5380 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005381 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005382 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005383 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005384 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5385 OptClassMethods.push_back(C);
5386 } else {
5387 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005388 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005389 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005390
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 std::vector<llvm::Constant*> Values(10);
5392 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005393 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005394 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005395 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5396 PD->protocol_begin(),
5397 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005398
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005399 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005400 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005401 "__DATA, __objc_const",
5402 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005404 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005405 "__DATA, __objc_const",
5406 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005407 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005408 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005409 "__DATA, __objc_const",
5410 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005412 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005413 "__DATA, __objc_const",
5414 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005415 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005416 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005417 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005418 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005419 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005420 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005421 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005422 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005423
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005424 if (Entry) {
5425 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005426 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005427 Entry->setInitializer(Init);
5428 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005429 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005430 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5431 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5432 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005433 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005434 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005435 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005436 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005437 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005438 CGM.AddUsedGlobal(Entry);
5439
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005440 // Use this protocol meta-data to build protocol list table in section
5441 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005442 llvm::GlobalVariable *PTGV =
5443 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5444 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5445 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005446 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005447 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005448 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005449 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005450 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005451 return Entry;
5452}
5453
5454/// EmitProtocolList - Generate protocol list meta-data:
5455/// @code
5456/// struct _protocol_list_t {
5457/// long protocol_count; // Note, this is 32/64 bit
5458/// struct _protocol_t[protocol_count];
5459/// }
5460/// @endcode
5461///
5462llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005463CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5464 ObjCProtocolDecl::protocol_iterator begin,
5465 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005466 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005467
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005468 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005469 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005470 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005472 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005473 llvm::SmallString<256> TmpName;
5474 Name.toVector(TmpName);
5475 llvm::GlobalVariable *GV =
5476 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005477 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005478 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005479
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005480 for (; begin != end; ++begin)
5481 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5482
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005483 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005484 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005485 ObjCTypes.ProtocolnfABIPtrTy));
5486
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005487 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005488 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005489 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005490 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005491 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005492 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005493 ProtocolRefs.size()),
5494 ProtocolRefs);
5495
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005496 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005497 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005498 llvm::GlobalValue::InternalLinkage,
5499 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005500 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005501 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005502 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005503 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005504 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005505 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005506 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005507}
5508
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005509/// GetMethodDescriptionConstant - This routine build following meta-data:
5510/// struct _objc_method {
5511/// SEL _cmd;
5512/// char *method_type;
5513/// char *_imp;
5514/// }
5515
5516llvm::Constant *
5517CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5518 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005519 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005520 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5521 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005522 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005523 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005524 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005525 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005526}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005527
5528/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5529/// This code gen. amounts to generating code for:
5530/// @code
5531/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5532/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005534LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005535 CodeGen::CodeGenFunction &CGF,
5536 QualType ObjectTy,
5537 llvm::Value *BaseValue,
5538 const ObjCIvarDecl *Ivar,
5539 unsigned CVRQualifiers) {
5540 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005541 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5542 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005543}
5544
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005545llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005546 CodeGen::CodeGenFunction &CGF,
5547 const ObjCInterfaceDecl *Interface,
5548 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005549 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005550}
5551
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005552CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005553 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005554 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555 QualType ResultType,
5556 Selector Sel,
5557 llvm::Value *Receiver,
5558 QualType Arg0Ty,
5559 bool IsSuper,
5560 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005561 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5562 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005563 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005564 llvm::Value *Arg0 = Receiver;
5565 if (!IsSuper)
5566 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005567
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005568 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005569 // FIXME. This is too much work to get the ABI-specific result type needed to
5570 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005571 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005572 = Types.getFunctionInfo(ResultType, CallArgList(),
5573 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005574 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005575 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005576 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005577#if 0
5578 // unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005579 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005580 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005581 // FIXME. Is there a better way of getting these names.
5582 // They are available in RuntimeFunctions vector pair.
5583 Name += "objc_msgSendId_stret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005584 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005585#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005586 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005587 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005588 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005589 } else {
5590 Fn = ObjCTypes.getMessageSendStretFixupFn();
5591 Name += "objc_msgSend_stret_fixup";
5592 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005593 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5594 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5595 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005596 } else {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005597#if 0
5598// unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005599 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005600 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005601 Name += "objc_msgSendId_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005602 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005603#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005604 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005605 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005606 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005607 } else {
5608 Fn = ObjCTypes.getMessageSendFixupFn();
5609 Name += "objc_msgSend_fixup";
5610 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005611 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005612 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005613 Name += '_';
5614 std::string SelName(Sel.getAsString());
5615 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005616 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005617 if (SelName[i] == ':')
5618 SelName[i] = '_';
5619 Name += SelName;
5620 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5621 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005622 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005623 std::vector<llvm::Constant*> Values(2);
5624 Values[0] = Fn;
5625 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005626 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005627 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005628 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005629 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005630 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005631 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005632 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005633 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005634 }
5635 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005636
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005637 CallArgList ActualArgs;
5638 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005639 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005640 ObjCTypes.MessageRefCPtrTy));
5641 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005642 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005643 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005644 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5645 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005646 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005647 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005648 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005649 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005650}
5651
5652/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005653CodeGen::RValue
5654CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005655 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005656 QualType ResultType,
5657 Selector Sel,
5658 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005659 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005660 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005661 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005662 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005663 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5664 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005665 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005666 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005667 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005668 Receiver, CGF.getContext().getObjCIdType(),
5669 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005670}
5671
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005672llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005673CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005674 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5675
Daniel Dunbara6468342009-03-02 05:18:14 +00005676 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005677 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005678 false, llvm::GlobalValue::ExternalLinkage,
5679 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005680 }
5681
5682 return GV;
5683}
5684
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005685llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5686 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005687 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005688
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005689 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005690 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005691 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005692 Entry =
5693 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005694 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005695 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005696 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005697 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005698 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005700 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005701 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005702 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005704 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005705}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005706
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005707llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005708CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005709 const ObjCInterfaceDecl *ID) {
5710 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005711
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005712 if (!Entry) {
5713 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5714 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715 Entry =
5716 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005717 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005719 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005720 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005721 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005722 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005723 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005724 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005725 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005726
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005727 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005728}
5729
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005730/// EmitMetaClassRef - Return a Value * of the address of _class_t
5731/// meta-data
5732///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005733llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5734 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005735 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5736 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005737 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005738
Daniel Dunbar15894b72009-04-07 05:48:37 +00005739 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005740 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005741 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005742 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005743 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005744 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005745 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005746 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005747 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005748 ObjCTypes.ClassnfABIPtrTy));
5749
Daniel Dunbare60aa052009-04-15 19:03:14 +00005750 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005751 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005752
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005753 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005754}
5755
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005756/// GetClass - Return a reference to the class for the given interface
5757/// decl.
5758llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5759 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005760 if (ID->hasAttr<WeakImportAttr>()) {
5761 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5762 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5763 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5764 }
5765
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005766 return EmitClassRef(Builder, ID);
5767}
5768
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005769/// Generates a message send where the super is the receiver. This is
5770/// a message send to self with special delivery semantics indicating
5771/// which class's method should be called.
5772CodeGen::RValue
5773CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005774 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005775 QualType ResultType,
5776 Selector Sel,
5777 const ObjCInterfaceDecl *Class,
5778 bool isCategoryImpl,
5779 llvm::Value *Receiver,
5780 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005781 const CodeGen::CallArgList &CallArgs,
5782 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005783 // ...
5784 // Create and init a super structure; this is a (receiver, class)
5785 // pair we will pass to objc_msgSendSuper.
5786 llvm::Value *ObjCSuper =
5787 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005788
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005789 llvm::Value *ReceiverAsObject =
5790 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5791 CGF.Builder.CreateStore(ReceiverAsObject,
5792 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005793
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005794 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005795 llvm::Value *Target;
5796 if (IsClassMessage) {
5797 if (isCategoryImpl) {
5798 // Message sent to "super' in a class method defined in
5799 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005800 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005801 Target = CGF.Builder.CreateStructGEP(Target, 0);
5802 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005803 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005804 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005805 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005806 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005807
Mike Stump18bb9282009-05-16 07:57:57 +00005808 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5809 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005810 const llvm::Type *ClassTy =
5811 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5812 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5813 CGF.Builder.CreateStore(Target,
5814 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005815
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005816 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005817 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5818 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005819 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005820 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005821 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005822 ObjCSuper, ObjCTypes.SuperPtrCTy,
5823 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005824}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005825
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005826llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005827 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005828 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005829
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005830 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005831 llvm::Constant *Casted =
5832 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5833 ObjCTypes.SelectorPtrTy);
5834 Entry =
5835 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5836 llvm::GlobalValue::InternalLinkage,
5837 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005838 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005839 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005840 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005841
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005842 if (lval)
5843 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005844 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005845}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005846/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005847/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005848///
5849void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005850 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005851 llvm::Value *dst,
5852 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005853 const llvm::Type * SrcTy = src->getType();
5854 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005855 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005856 assert(Size <= 8 && "does not support size > 8");
5857 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5858 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005859 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5860 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005861 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5862 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005863 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5864 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005865 return;
5866}
5867
5868/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5869/// objc_assign_strongCast (id src, id *dst)
5870///
5871void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005872 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005873 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005874 const llvm::Type * SrcTy = src->getType();
5875 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005876 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005877 assert(Size <= 8 && "does not support size > 8");
5878 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005879 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005880 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5881 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005882 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5883 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005884 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005885 src, dst, "weakassign");
5886 return;
5887}
5888
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005889void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005890 CodeGen::CodeGenFunction &CGF,
5891 llvm::Value *DestPtr,
5892 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005893 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005894 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5895 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005896 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005897 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005898 return;
5899}
5900
Fariborz Jahanian06292952009-02-16 22:52:32 +00005901/// EmitObjCWeakRead - Code gen for loading value of a __weak
5902/// object: objc_read_weak (id *src)
5903///
5904llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005905 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005906 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005907 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005908 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5909 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005910 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005911 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005912 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005913 return read_weak;
5914}
5915
5916/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5917/// objc_assign_weak (id src, id *dst)
5918///
5919void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005920 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005921 const llvm::Type * SrcTy = src->getType();
5922 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005923 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005924 assert(Size <= 8 && "does not support size > 8");
5925 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5926 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005927 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5928 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005929 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5930 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005931 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005932 src, dst, "weakassign");
5933 return;
5934}
5935
5936/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5937/// objc_assign_global (id src, id *dst)
5938///
5939void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005940 llvm::Value *src, llvm::Value *dst,
5941 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005942 const llvm::Type * SrcTy = src->getType();
5943 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005944 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005945 assert(Size <= 8 && "does not support size > 8");
5946 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5947 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005948 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5949 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005950 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5951 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005952 if (!threadlocal)
5953 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5954 src, dst, "globalassign");
5955 else
5956 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5957 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005958 return;
5959}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005960
John McCallc20acd32010-07-21 00:41:47 +00005961namespace {
John McCallcda666c2010-07-21 07:22:38 +00005962 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00005963 llvm::Value *SyncExitFn;
5964 llvm::Value *SyncArg;
5965 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5966 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5967
5968 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5969 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5970 }
5971 };
5972}
5973
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005974void
John McCallbd309292010-07-06 01:34:17 +00005975CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5976 const ObjCAtSynchronizedStmt &S) {
5977 // Evaluate the lock operand. This should dominate the cleanup.
5978 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005979
John McCallbd309292010-07-06 01:34:17 +00005980 // Acquire the lock.
5981 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5982 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5983 ->setDoesNotThrow();
5984
5985 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00005986 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5987 ObjCTypes.getSyncExitFn(),
5988 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005989
John McCallbd309292010-07-06 01:34:17 +00005990 // Emit the body of the statement.
5991 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005992
John McCallbd309292010-07-06 01:34:17 +00005993 // Pop the lock-release cleanup.
5994 CGF.PopCleanupBlock();
5995}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005996
John McCallbd309292010-07-06 01:34:17 +00005997namespace {
5998 struct CatchHandler {
5999 const VarDecl *Variable;
6000 const Stmt *Body;
6001 llvm::BasicBlock *Block;
6002 llvm::Value *TypeInfo;
6003 };
John McCall5c08ab92010-07-13 22:12:14 +00006004
John McCallcda666c2010-07-21 07:22:38 +00006005 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00006006 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6007 MightThrow(MightThrow), Fn(Fn) {}
6008 bool MightThrow;
6009 llvm::Value *Fn;
6010
6011 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6012 if (!MightThrow) {
6013 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6014 return;
6015 }
6016
6017 CGF.EmitCallOrInvoke(Fn, 0, 0);
6018 }
6019 };
John McCallbd309292010-07-06 01:34:17 +00006020}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006021
John McCall2ca705e2010-07-24 00:37:23 +00006022llvm::Constant *
6023CGObjCNonFragileABIMac::GetEHType(QualType T) {
6024 // There's a particular fixed type info for 'id'.
6025 if (T->isObjCIdType() ||
6026 T->isObjCQualifiedIdType()) {
6027 llvm::Constant *IDEHType =
6028 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6029 if (!IDEHType)
6030 IDEHType =
6031 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6032 false,
6033 llvm::GlobalValue::ExternalLinkage,
6034 0, "OBJC_EHTYPE_id");
6035 return IDEHType;
6036 }
6037
6038 // All other types should be Objective-C interface pointer types.
6039 const ObjCObjectPointerType *PT =
6040 T->getAs<ObjCObjectPointerType>();
6041 assert(PT && "Invalid @catch type.");
6042 const ObjCInterfaceType *IT = PT->getInterfaceType();
6043 assert(IT && "Invalid @catch type.");
6044 return GetInterfaceEHType(IT->getDecl(), false);
6045}
6046
John McCallbd309292010-07-06 01:34:17 +00006047void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6048 const ObjCAtTryStmt &S) {
6049 // Jump destination for falling out of catch bodies.
6050 CodeGenFunction::JumpDest Cont;
6051 if (S.getNumCatchStmts())
6052 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006053
John McCallbd309292010-07-06 01:34:17 +00006054 CodeGenFunction::FinallyInfo FinallyInfo;
6055 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6056 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6057 ObjCTypes.getObjCBeginCatchFn(),
6058 ObjCTypes.getObjCEndCatchFn(),
6059 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006060
John McCallbd309292010-07-06 01:34:17 +00006061 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006062
John McCallbd309292010-07-06 01:34:17 +00006063 // Enter the catch, if there is one.
6064 if (S.getNumCatchStmts()) {
6065 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6066 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00006067 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006068
John McCallbd309292010-07-06 01:34:17 +00006069 Handlers.push_back(CatchHandler());
6070 CatchHandler &Handler = Handlers.back();
6071 Handler.Variable = CatchDecl;
6072 Handler.Body = CatchStmt->getCatchBody();
6073 Handler.Block = CGF.createBasicBlock("catch");
6074
6075 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006076 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006077 Handler.TypeInfo = 0; // catch-all
6078 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006079 break;
6080 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006081
John McCall2ca705e2010-07-24 00:37:23 +00006082 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006083 }
John McCallbd309292010-07-06 01:34:17 +00006084
6085 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6086 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6087 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006088 }
John McCallbd309292010-07-06 01:34:17 +00006089
6090 // Emit the try body.
6091 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006092
John McCallbd309292010-07-06 01:34:17 +00006093 // Leave the try.
6094 if (S.getNumCatchStmts())
6095 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006096
John McCallbd309292010-07-06 01:34:17 +00006097 // Remember where we were.
6098 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006099
John McCallbd309292010-07-06 01:34:17 +00006100 // Emit the handlers.
6101 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6102 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006103
John McCallbd309292010-07-06 01:34:17 +00006104 CGF.EmitBlock(Handler.Block);
6105 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006106
John McCallbd309292010-07-06 01:34:17 +00006107 // Enter the catch.
6108 llvm::CallInst *Exn =
6109 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6110 "exn.adjusted");
6111 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006112
John McCallbd309292010-07-06 01:34:17 +00006113 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006114 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006115 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6116 EndCatchMightThrow,
6117 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006118
John McCallbd309292010-07-06 01:34:17 +00006119 // Bind the catch parameter if it exists.
6120 if (const VarDecl *CatchParam = Handler.Variable) {
6121 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6122 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006123
John McCallbd309292010-07-06 01:34:17 +00006124 CGF.EmitLocalBlockVarDecl(*CatchParam);
6125 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006126 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006127
John McCallbd309292010-07-06 01:34:17 +00006128 CGF.ObjCEHValueStack.push_back(Exn);
6129 CGF.EmitStmt(Handler.Body);
6130 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006131
John McCallbd309292010-07-06 01:34:17 +00006132 // Leave the earlier cleanup.
6133 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006134
John McCallbd309292010-07-06 01:34:17 +00006135 CGF.EmitBranchThroughCleanup(Cont);
6136 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006137
John McCallbd309292010-07-06 01:34:17 +00006138 // Go back to the try-statement fallthrough.
6139 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006140
John McCallbd309292010-07-06 01:34:17 +00006141 // Pop out of the normal cleanup on the finally.
6142 if (S.getFinallyStmt())
6143 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006144
John McCallad5d61e2010-07-23 21:56:41 +00006145 if (Cont.isValid())
6146 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006147}
6148
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006149/// EmitThrowStmt - Generate code for a throw statement.
6150void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6151 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006152 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006153 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006154 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006155 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006156 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006157 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006158 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006159 "Unexpected rethrow outside @catch block.");
6160 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006161 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006162 }
6163
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006164 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006165 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6166 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6167 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006168 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00006169 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006170 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00006171 } else {
6172 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6173 ->setDoesNotReturn();
6174 CGF.Builder.CreateUnreachable();
6175 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006176
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006177 // Clear the insertion point to indicate we are in unreachable code.
6178 CGF.Builder.ClearInsertionPoint();
6179}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006180
John McCall2ca705e2010-07-24 00:37:23 +00006181llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006182CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006183 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006184 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006185
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006186 // If we don't need a definition, return the entry if found or check
6187 // if we use an external reference.
6188 if (!ForDefinition) {
6189 if (Entry)
6190 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006191
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006192 // If this type (or a super class) has the __objc_exception__
6193 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006194 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006195 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006196 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006197 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006198 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006199 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006200 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006201 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006202
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006203 // Otherwise we need to either make a new entry or fill in the
6204 // initializer.
6205 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006206 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006207 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006208 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006209 CGM.getModule().getGlobalVariable(VTableName);
6210 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006211 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6212 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006213 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006214 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006215
Chris Lattner5e016ae2010-06-27 07:15:29 +00006216 llvm::Value *VTableIdx =
6217 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006218
6219 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006220 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006221 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006222 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006223 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006224 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006225
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006226 if (Entry) {
6227 Entry->setInitializer(Init);
6228 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006229 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006230 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006231 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006232 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006233 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006234 }
6235
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006236 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006237 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006238 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6239 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006240
6241 if (ForDefinition) {
6242 Entry->setSection("__DATA,__objc_const");
6243 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6244 } else {
6245 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6246 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006247
6248 return Entry;
6249}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006250
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006251/* *** */
6252
Daniel Dunbarb036db82008-08-13 03:21:16 +00006253CodeGen::CGObjCRuntime *
6254CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006255 return new CGObjCMac(CGM);
6256}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006257
6258CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006259CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006260 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006261}