blob: 4db7ec5f9ffa367938e14da2728d334a0b065ac1 [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);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000945
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000946 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000947 unsigned int BytePos, bool ForStrongLayout,
948 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000949 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000950 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000951 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000952 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000953 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000954 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000955
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000956 /// GetIvarLayoutName - Returns a unique constant for the given
957 /// ivar layout bitmap.
958 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
959 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000960
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000961 /// EmitPropertyList - Emit the given property list. The return
962 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000963 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000964 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000965 const ObjCContainerDecl *OCD,
966 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000967
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000968 /// PushProtocolProperties - Push protocol's property on the input stack.
969 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
970 std::vector<llvm::Constant*> &Properties,
971 const Decl *Container,
972 const ObjCProtocolDecl *PROTO,
973 const ObjCCommonTypesHelper &ObjCTypes);
974
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000975 /// GetProtocolRef - Return a reference to the internal protocol
976 /// description, creating an empty one if it has not been
977 /// defined. The return value has type ProtocolPtrTy.
978 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000979
Daniel Dunbar30c65362009-03-09 20:09:19 +0000980 /// CreateMetadataVar - Create a global variable with internal
981 /// linkage for use by the Objective-C runtime.
982 ///
983 /// This is a convenience wrapper which not only creates the
984 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000985 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000986 ///
987 /// \param Name - The variable name.
988 /// \param Init - The variable initializer; this is also used to
989 /// define the type of the variable.
990 /// \param Section - The section the variable should go into, or 0.
991 /// \param Align - The alignment for the variable, or 0.
992 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000993 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000994 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000995 llvm::Constant *Init,
996 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000997 unsigned Align,
998 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000999
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001000 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001001 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001002 QualType ResultType,
1003 llvm::Value *Sel,
1004 llvm::Value *Arg0,
1005 QualType Arg0Ty,
1006 bool IsSuper,
1007 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001008 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001009 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001010
Daniel Dunbar5e639272010-04-25 20:39:01 +00001011 /// EmitImageInfo - Emit the image info marker used to encode some module
1012 /// level information.
1013 void EmitImageInfo();
1014
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001015public:
Owen Andersonae86c192009-07-13 04:10:07 +00001016 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +00001017 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001018
David Chisnall481e3a82010-01-23 02:40:42 +00001019 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001020
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001021 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1022 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001023
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001024 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001025
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001026 /// GetOrEmitProtocol - Get the protocol object for the given
1027 /// declaration, emitting it if necessary. The return value has type
1028 /// ProtocolPtrTy.
1029 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001030
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001031 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1032 /// object for the given declaration, emitting it if needed. These
1033 /// forward references will be filled in with empty bodies if no
1034 /// definition is seen. The return value has type ProtocolPtrTy.
1035 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001036};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001037
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001038class CGObjCMac : public CGObjCCommonMac {
1039private:
1040 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001041
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001042 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001043 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001044 void EmitModuleInfo();
1045
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001046 /// EmitModuleSymols - Emit module symbols, the list of defined
1047 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001048 llvm::Constant *EmitModuleSymbols();
1049
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001050 /// FinishModule - Write out global data structures at the end of
1051 /// processing a translation unit.
1052 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001053
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001054 /// EmitClassExtension - Generate the class extension structure used
1055 /// to store the weak ivar layout and properties. The return value
1056 /// has type ClassExtensionPtrTy.
1057 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1058
1059 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1060 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001061 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001062 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001063
1064 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1065 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001066
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001067 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001068 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001069 QualType ResultType,
1070 Selector Sel,
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001071 llvm::Value *Arg0,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001072 QualType Arg0Ty,
1073 bool IsSuper,
1074 const CallArgList &CallArgs);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001075
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001076 /// EmitIvarList - Emit the ivar list for the given
1077 /// implementation. If ForClass is true the list of class ivars
1078 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1079 /// interface ivars will be emitted. The return value has type
1080 /// IvarListPtrTy.
1081 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001082 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001083
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001084 /// EmitMetaClass - Emit a forward reference to the class structure
1085 /// for the metaclass of the given interface. The return value has
1086 /// type ClassPtrTy.
1087 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1088
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001089 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001090 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001091 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1092 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001093 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001094
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001095 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001096
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001097 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001098
1099 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001100 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001101 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001102 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001103 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001104
1105 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001106 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001107 /// - TypeName: The name for the type containing the methods.
1108 /// - IsProtocol: True iff these methods are for a protocol.
1109 /// - ClassMethds: True iff these are class methods.
1110 /// - Required: When true, only "required" methods are
1111 /// listed. Similarly, when false only "optional" methods are
1112 /// listed. For classes this should always be true.
1113 /// - begin, end: The method list to output.
1114 ///
1115 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001116 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001117 const char *Section,
1118 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001119
Daniel Dunbarc475d422008-10-29 22:36:39 +00001120 /// GetOrEmitProtocol - Get the protocol object for the given
1121 /// declaration, emitting it if necessary. The return value has type
1122 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001123 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001124
1125 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1126 /// object for the given declaration, emitting it if needed. These
1127 /// forward references will be filled in with empty bodies if no
1128 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001129 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001130
Daniel Dunbarb036db82008-08-13 03:21:16 +00001131 /// EmitProtocolExtension - Generate the protocol extension
1132 /// structure used to store optional instance and class methods, and
1133 /// protocol properties. The return value has type
1134 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001135 llvm::Constant *
1136 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1137 const ConstantVector &OptInstanceMethods,
1138 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001139
1140 /// EmitProtocolList - Generate the list of referenced
1141 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001142 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001143 ObjCProtocolDecl::protocol_iterator begin,
1144 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001145
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001146 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1147 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001148 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1149 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001150
1151public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001152 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001153
Fariborz Jahanian71394042009-01-23 23:53:38 +00001154 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001155
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001156 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001157 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001158 QualType ResultType,
1159 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001160 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001161 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001162 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001163 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001164
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001165 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001166 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001167 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001168 QualType ResultType,
1169 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001170 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001171 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001172 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001173 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001174 const CallArgList &CallArgs,
1175 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001176
Daniel Dunbarcb463852008-11-01 01:53:16 +00001177 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001178 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001179
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001180 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1181 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001182
1183 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1184 /// untyped one.
1185 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1186 const ObjCMethodDecl *Method);
1187
John McCall2ca705e2010-07-24 00:37:23 +00001188 virtual llvm::Constant *GetEHType(QualType T);
1189
Daniel Dunbar92992502008-08-15 22:20:32 +00001190 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001191
Daniel Dunbar92992502008-08-15 22:20:32 +00001192 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001193
Daniel Dunbarcb463852008-11-01 01:53:16 +00001194 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001195 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Chris Lattnerd4808922009-03-22 21:03:39 +00001197 virtual llvm::Constant *GetPropertyGetFunction();
1198 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001199 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001200 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001201
John McCallbd309292010-07-06 01:34:17 +00001202 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1203 const ObjCAtTryStmt &S);
1204 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1205 const ObjCAtSynchronizedStmt &S);
1206 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001207 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1208 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001209 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001210 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001211 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001213 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001214 llvm::Value *src, llvm::Value *dest,
1215 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001216 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001217 llvm::Value *src, llvm::Value *dest,
1218 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001219 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1220 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001221 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1222 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001223 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001224
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001225 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1226 QualType ObjectTy,
1227 llvm::Value *BaseValue,
1228 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001229 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001230 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001231 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001232 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001233};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001235class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001236private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001237 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001238 llvm::GlobalVariable* ObjCEmptyCacheVar;
1239 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001240
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001241 /// SuperClassReferences - uniqued super class references.
1242 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001244 /// MetaClassReferences - uniqued meta class references.
1245 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001246
1247 /// EHTypeReferences - uniqued class ehtype references.
1248 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001249
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001250 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001251 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001252 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001253
Fariborz Jahanian67260552009-11-17 21:37:35 +00001254 /// DefinedMetaClasses - List of defined meta-classes.
1255 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1256
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001257 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001258 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001259 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001260
Fariborz Jahanian71394042009-01-23 23:53:38 +00001261 /// FinishNonFragileABIModule - Write out global data structures at the end of
1262 /// processing a translation unit.
1263 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001264
Daniel Dunbar19573e72009-05-15 21:48:48 +00001265 /// AddModuleClassList - Add the given list of class pointers to the
1266 /// module with the provided symbol and section names.
1267 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1268 const char *SymbolName,
1269 const char *SectionName);
1270
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001271 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1272 unsigned InstanceStart,
1273 unsigned InstanceSize,
1274 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001275 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001276 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001277 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001278 llvm::Constant *ClassRoGV,
1279 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001280
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001281 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001282
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001283 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001284
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001285 /// EmitMethodList - Emit the method list for the given
1286 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001287 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001288 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001289 const ConstantVector &Methods);
1290 /// EmitIvarList - Emit the ivar list for the given
1291 /// implementation. If ForClass is true the list of class ivars
1292 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1293 /// interface ivars will be emitted. The return value has type
1294 /// IvarListnfABIPtrTy.
1295 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001296
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001297 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001298 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001299 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001300
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001301 /// GetOrEmitProtocol - Get the protocol object for the given
1302 /// declaration, emitting it if necessary. The return value has type
1303 /// ProtocolPtrTy.
1304 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001306 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1307 /// object for the given declaration, emitting it if needed. These
1308 /// forward references will be filled in with empty bodies if no
1309 /// definition is seen. The return value has type ProtocolPtrTy.
1310 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001311
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001312 /// EmitProtocolList - Generate the list of referenced
1313 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001314 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001315 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001316 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001317
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001318 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001319 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001320 QualType ResultType,
1321 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001322 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001323 QualType Arg0Ty,
1324 bool IsSuper,
1325 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001326
1327 /// GetClassGlobal - Return the global variable for the Objective-C
1328 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001329 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001330
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001331 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001332 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001333 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001334 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001336 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1337 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001338 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1339 const ObjCInterfaceDecl *ID);
1340
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001341 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1342 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001343 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001344 const ObjCInterfaceDecl *ID);
1345
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001346 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1347 /// the given ivar.
1348 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001349 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001350 const ObjCInterfaceDecl *ID,
1351 const ObjCIvarDecl *Ivar);
1352
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001353 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1354 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001355 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1356 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001357
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001358 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001359 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001360 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001361 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001362
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001363 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001364 return "OBJC_METACLASS_$_";
1365 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001366
Daniel Dunbar15894b72009-04-07 05:48:37 +00001367 const char *getClassSymbolPrefix() const {
1368 return "OBJC_CLASS_$_";
1369 }
1370
Daniel Dunbar961202372009-05-03 12:57:56 +00001371 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001372 uint32_t &InstanceStart,
1373 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001374
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001375 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001376 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001377 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1378 return CGM.getContext().Selectors.getSelector(0, &II);
1379 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001380
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001381 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001382 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1383 return CGM.getContext().Selectors.getSelector(1, &II);
1384 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001385
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001386 /// ImplementationIsNonLazy - Check whether the given category or
1387 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001388 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001389
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001390public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001391 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001392 // FIXME. All stubs for now!
1393 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001394
Fariborz Jahanian71394042009-01-23 23:53:38 +00001395 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001396 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001397 QualType ResultType,
1398 Selector Sel,
1399 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001400 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001401 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001402 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001403
1404 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001405 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001406 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001407 QualType ResultType,
1408 Selector Sel,
1409 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001410 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001411 llvm::Value *Receiver,
1412 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001413 const CallArgList &CallArgs,
1414 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001415
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001417 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001418
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001419 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1420 bool lvalue = false)
1421 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001422
1423 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1424 /// untyped one.
1425 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1426 const ObjCMethodDecl *Method)
1427 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001428
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001429 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001430
Fariborz Jahanian71394042009-01-23 23:53:38 +00001431 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001432 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001433 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001434
John McCall2ca705e2010-07-24 00:37:23 +00001435 virtual llvm::Constant *GetEHType(QualType T);
1436
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001437 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001438 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001439 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001440 virtual llvm::Constant *GetPropertySetFunction() {
1441 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001442 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001443
1444 virtual llvm::Constant *GetCopyStructFunction() {
1445 return ObjCTypes.getCopyStructFn();
1446 }
1447
Chris Lattnerd4808922009-03-22 21:03:39 +00001448 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001449 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001450 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001451
John McCallbd309292010-07-06 01:34:17 +00001452 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1453 const ObjCAtTryStmt &S);
1454 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1455 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001456 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001457 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001458 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001459 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001460 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001461 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001462 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001463 llvm::Value *src, llvm::Value *dest,
1464 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001465 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001466 llvm::Value *src, llvm::Value *dest,
1467 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001468 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001469 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001470 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1471 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001472 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001473 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1474 QualType ObjectTy,
1475 llvm::Value *BaseValue,
1476 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001477 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001478 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001479 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001480 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001481};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001482
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001483} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001484
1485/* *** Helper Functions *** */
1486
1487/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001488static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001489 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001490 unsigned idx0,
1491 unsigned idx1) {
1492 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001493 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1494 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001495 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001496 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001497}
1498
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001499/// hasObjCExceptionAttribute - Return true if this class or any super
1500/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001501static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001502 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001503 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001504 return true;
1505 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001506 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001507 return false;
1508}
1509
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001510/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001511
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001512CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001513 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001514 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001515 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001516}
1517
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001518/// GetClass - Return a reference to the class for the given interface
1519/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001520llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001521 const ObjCInterfaceDecl *ID) {
1522 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001523}
1524
1525/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001526llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1527 bool lval) {
1528 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001529}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001530llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001531 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001532 return EmitSelector(Builder, Method->getSelector());
1533}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001534
John McCall2ca705e2010-07-24 00:37:23 +00001535llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1536 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1537 return 0;
1538}
1539
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001540/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001541/*
1542 struct __builtin_CFString {
1543 const int *isa; // point to __CFConstantStringClassReference
1544 int flags;
1545 const char *str;
1546 long length;
1547 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001548*/
1549
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001550/// or Generate a constant NSString object.
1551/*
1552 struct __builtin_NSString {
1553 const int *isa; // point to __NSConstantStringClassReference
1554 const char *str;
1555 unsigned int length;
1556 };
1557*/
1558
Fariborz Jahanian71394042009-01-23 23:53:38 +00001559llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001560 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001561 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1562 CGM.GetAddrOfConstantCFString(SL) :
1563 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001564}
1565
1566/// Generates a message send where the super is the receiver. This is
1567/// a message send to self with special delivery semantics indicating
1568/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001569CodeGen::RValue
1570CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001571 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001572 QualType ResultType,
1573 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001574 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001575 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001576 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001577 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001578 const CodeGen::CallArgList &CallArgs,
1579 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001580 // Create and init a super structure; this is a (receiver, class)
1581 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001582 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001583 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001584 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001585 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001586 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001587 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001588
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001589 // If this is a class message the metaclass is passed as the target.
1590 llvm::Value *Target;
1591 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001592 if (isCategoryImpl) {
1593 // Message sent to 'super' in a class method defined in a category
1594 // implementation requires an odd treatment.
1595 // If we are in a class method, we must retrieve the
1596 // _metaclass_ for the current class, pointed at by
1597 // the class's "isa" pointer. The following assumes that
1598 // isa" is the first ivar in a class (which it must be).
1599 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1600 Target = CGF.Builder.CreateStructGEP(Target, 0);
1601 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001602 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001603 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1604 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1605 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1606 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001607 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001608 }
1609 else if (isCategoryImpl)
1610 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1611 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001612 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1613 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1614 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001615 }
Mike Stump18bb9282009-05-16 07:57:57 +00001616 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1617 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001618 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001619 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001620 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001621 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001622 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001623 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001624 EmitSelector(CGF.Builder, Sel),
1625 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001626 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001627}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001628
1629/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001630CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001631 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001632 QualType ResultType,
1633 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001634 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001635 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001636 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001637 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001638 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001639 EmitSelector(CGF.Builder, Sel),
1640 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001641 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001642}
1643
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001644CodeGen::RValue
1645CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001646 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001647 QualType ResultType,
1648 llvm::Value *Sel,
1649 llvm::Value *Arg0,
1650 QualType Arg0Ty,
1651 bool IsSuper,
1652 const CallArgList &CallArgs,
1653 const ObjCMethodDecl *Method,
1654 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001655 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001656 if (!IsSuper)
1657 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001658 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001659 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001660 CGF.getContext().getObjCSelType()));
1661 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001662
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001663 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001664 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001665 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001666 const llvm::FunctionType *FTy =
1667 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001668
Anders Carlsson280e61f12010-06-21 20:59:55 +00001669 if (Method)
1670 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1671 CGM.getContext().getCanonicalType(ResultType) &&
1672 "Result type mismatch!");
1673
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001674 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001675 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001676 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001677 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001678 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1679 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1680 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001681 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001682 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001683 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001684 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001685 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001686 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001687}
1688
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001689llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001690 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001691 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001692 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001693 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1694
Owen Andersonade90fd2009-07-29 18:54:39 +00001695 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001696 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001697}
1698
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001699void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001700 // FIXME: We shouldn't need this, the protocol decl should contain enough
1701 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001702 DefinedProtocols.insert(PD->getIdentifier());
1703
1704 // If we have generated a forward reference to this protocol, emit
1705 // it now. Otherwise do nothing, the protocol objects are lazily
1706 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001707 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001708 GetOrEmitProtocol(PD);
1709}
1710
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001711llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001712 if (DefinedProtocols.count(PD->getIdentifier()))
1713 return GetOrEmitProtocol(PD);
1714 return GetOrEmitProtocolRef(PD);
1715}
1716
Daniel Dunbarb036db82008-08-13 03:21:16 +00001717/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001718// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1719struct _objc_protocol {
1720struct _objc_protocol_extension *isa;
1721char *protocol_name;
1722struct _objc_protocol_list *protocol_list;
1723struct _objc__method_prototype_list *instance_methods;
1724struct _objc__method_prototype_list *class_methods
1725};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001726
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001727See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001728*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001729llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1730 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1731
1732 // Early exit if a defining object has already been generated.
1733 if (Entry && Entry->hasInitializer())
1734 return Entry;
1735
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001736 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001737 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001738 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1739
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001740 // Construct method lists.
1741 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1742 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001743 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001744 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001745 ObjCMethodDecl *MD = *i;
1746 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1747 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1748 OptInstanceMethods.push_back(C);
1749 } else {
1750 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001751 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001752 }
1753
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001754 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001755 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001756 ObjCMethodDecl *MD = *i;
1757 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1758 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1759 OptClassMethods.push_back(C);
1760 } else {
1761 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001762 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001763 }
1764
Daniel Dunbarb036db82008-08-13 03:21:16 +00001765 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001766 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001767 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001768 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001769 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001770 PD->protocol_begin(),
1771 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001772 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001773 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001774 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1775 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001776 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001777 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001778 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1779 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001780 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001781 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001782
Daniel Dunbarb036db82008-08-13 03:21:16 +00001783 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001784 // Already created, fix the linkage and update the initializer.
1785 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001786 Entry->setInitializer(Init);
1787 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001788 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001789 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001790 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001791 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001792 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001793 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001794 // FIXME: Is this necessary? Why only for protocol?
1795 Entry->setAlignment(4);
1796 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001797 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001798
1799 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001800}
1801
Daniel Dunbarc475d422008-10-29 22:36:39 +00001802llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001803 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1804
1805 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001806 // We use the initializer as a marker of whether this is a forward
1807 // reference or not. At module finalization we add the empty
1808 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001809 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001810 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001811 llvm::GlobalValue::ExternalLinkage,
1812 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001813 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001814 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001815 // FIXME: Is this necessary? Why only for protocol?
1816 Entry->setAlignment(4);
1817 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001818
Daniel Dunbarb036db82008-08-13 03:21:16 +00001819 return Entry;
1820}
1821
1822/*
1823 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001824 uint32_t size;
1825 struct objc_method_description_list *optional_instance_methods;
1826 struct objc_method_description_list *optional_class_methods;
1827 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001828 };
1829*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001830llvm::Constant *
1831CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1832 const ConstantVector &OptInstanceMethods,
1833 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001834 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001835 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001836 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001837 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001838 Values[1] =
1839 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001840 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001841 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1842 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001843 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001844 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001845 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1846 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001847 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001848 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001849
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001850 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001852 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001853 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001855 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001856 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001858 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001859 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001860 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001861 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862}
1863
1864/*
1865 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001866 struct objc_protocol_list *next;
1867 long count;
1868 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869 };
1870*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001871llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001872CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001873 ObjCProtocolDecl::protocol_iterator begin,
1874 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001875 std::vector<llvm::Constant*> ProtocolRefs;
1876
Daniel Dunbardec75f82008-08-21 21:57:41 +00001877 for (; begin != end; ++begin)
1878 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001879
1880 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001881 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001882 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001883
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001884 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001885 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001886
1887 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001888 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001889 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001890 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001891 ProtocolRefs.size() - 1);
1892 Values[2] =
1893 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1894 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001895 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001896
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001897 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001898 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001899 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001900 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001901 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001902}
1903
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001904void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1905 std::vector<llvm::Constant*> &Properties,
1906 const Decl *Container,
1907 const ObjCProtocolDecl *PROTO,
1908 const ObjCCommonTypesHelper &ObjCTypes) {
1909 std::vector<llvm::Constant*> Prop(2);
1910 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1911 E = PROTO->protocol_end(); P != E; ++P)
1912 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1913 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1914 E = PROTO->prop_end(); I != E; ++I) {
1915 const ObjCPropertyDecl *PD = *I;
1916 if (!PropertySet.insert(PD->getIdentifier()))
1917 continue;
1918 Prop[0] = GetPropertyName(PD->getIdentifier());
1919 Prop[1] = GetPropertyTypeString(PD, Container);
1920 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1921 }
1922}
1923
Daniel Dunbarb036db82008-08-13 03:21:16 +00001924/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001925 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001926 const char * const name;
1927 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001928 };
1929
1930 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 uint32_t entsize; // sizeof (struct _objc_property)
1932 uint32_t prop_count;
1933 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001934 };
1935*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001936llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001937 const Decl *Container,
1938 const ObjCContainerDecl *OCD,
1939 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001940 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001941 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001942 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1943 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001944 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001945 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001946 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001947 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001948 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001949 Prop));
1950 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001951 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001952 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1953 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001954 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1955 ObjCTypes);
1956 }
1957 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1958 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1959 E = CD->protocol_end(); P != E; ++P)
1960 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1961 ObjCTypes);
1962 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001963
1964 // Return null for empty list.
1965 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001966 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001967
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001968 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001969 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001970 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001971 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1972 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001973 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001974 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001975 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001976 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001977
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001978 llvm::GlobalVariable *GV =
1979 CreateMetadataVar(Name, Init,
1980 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001981 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001982 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001983 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001984 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001985}
1986
1987/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001988 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001989 int count;
1990 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001991 };
1992*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001993llvm::Constant *
1994CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1995 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001996 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001997 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1998 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001999 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002000 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002001 Desc);
2002}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002003
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002004llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002005 const char *Section,
2006 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002007 // Return null for empty list.
2008 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002009 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002010
2011 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002012 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002013 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002014 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002015 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002016 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002017
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002018 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002019 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002020 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002021}
2022
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002023/*
2024 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 char *category_name;
2026 char *class_name;
2027 struct _objc_method_list *instance_methods;
2028 struct _objc_method_list *class_methods;
2029 struct _objc_protocol_list *protocols;
2030 uint32_t size; // <rdar://4585769>
2031 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002032 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002033*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002034void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002035 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002036
Mike Stump18bb9282009-05-16 07:57:57 +00002037 // FIXME: This is poor design, the OCD should have a pointer to the category
2038 // decl. Additionally, note that Category can be null for the @implementation
2039 // w/o an @interface case. Sema should just create one for us as it does for
2040 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002041 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002043 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002044
2045 llvm::SmallString<256> ExtName;
2046 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2047 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002048
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002049 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002050 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002051 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002052 // Instance methods should always be defined.
2053 InstanceMethods.push_back(GetMethodConstant(*i));
2054 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002055 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002056 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002057 // Class methods should always be defined.
2058 ClassMethods.push_back(GetMethodConstant(*i));
2059 }
2060
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002061 std::vector<llvm::Constant*> Values(7);
2062 Values[0] = GetClassName(OCD->getIdentifier());
2063 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002064 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002065 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002066 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002067 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002068 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002070 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002071 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002072 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002073 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002074 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002075 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002076 Category->protocol_begin(),
2077 Category->protocol_end());
2078 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002079 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002080 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002081 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002082
2083 // If there is no category @interface then there can be no properties.
2084 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002085 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002086 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002087 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002088 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002089 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002090
Owen Anderson0e0189d2009-07-27 22:29:56 +00002091 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002092 Values);
2093
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002094 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002095 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002096 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002097 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002098 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002099 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002100}
2101
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002102// FIXME: Get from somewhere?
2103enum ClassFlags {
2104 eClassFlags_Factory = 0x00001,
2105 eClassFlags_Meta = 0x00002,
2106 // <rdr://5142207>
2107 eClassFlags_HasCXXStructors = 0x02000,
2108 eClassFlags_Hidden = 0x20000,
2109 eClassFlags_ABI2_Hidden = 0x00010,
2110 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2111};
2112
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002113/*
2114 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002115 Class isa;
2116 Class super_class;
2117 const char *name;
2118 long version;
2119 long info;
2120 long instance_size;
2121 struct _objc_ivar_list *ivars;
2122 struct _objc_method_list *methods;
2123 struct _objc_cache *cache;
2124 struct _objc_protocol_list *protocols;
2125 // Objective-C 1.0 extensions (<rdr://4585769>)
2126 const char *ivar_layout;
2127 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002128 };
2129
2130 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002132void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002133 DefinedSymbols.insert(ID->getIdentifier());
2134
Chris Lattner86d7d912008-11-24 03:54:41 +00002135 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002136 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002137 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002138 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002140 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002141 Interface->protocol_begin(),
2142 Interface->protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002143 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002144 if (ID->getNumIvarInitializers())
2145 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002146 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002147 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002148
2149 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002150 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002151 Flags |= eClassFlags_Hidden;
2152
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002153 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002154 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002155 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002156 // Instance methods should always be defined.
2157 InstanceMethods.push_back(GetMethodConstant(*i));
2158 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002159 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002160 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002161 // Class methods should always be defined.
2162 ClassMethods.push_back(GetMethodConstant(*i));
2163 }
2164
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002165 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002166 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002167 ObjCPropertyImplDecl *PID = *i;
2168
2169 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2170 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2171
2172 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2173 if (llvm::Constant *C = GetMethodConstant(MD))
2174 InstanceMethods.push_back(C);
2175 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2176 if (llvm::Constant *C = GetMethodConstant(MD))
2177 InstanceMethods.push_back(C);
2178 }
2179 }
2180
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002181 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002182 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002183 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002184 // Record a reference to the super class.
2185 LazySymbols.insert(Super->getIdentifier());
2186
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002187 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002188 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002189 ObjCTypes.ClassPtrTy);
2190 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002191 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002192 }
2193 Values[ 2] = GetClassName(ID->getIdentifier());
2194 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002195 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2196 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2197 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002198 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002199 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002200 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002201 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002202 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002203 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002204 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002205 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002206 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002207 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002208 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002209 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002210 std::string Name("\01L_OBJC_CLASS_");
2211 Name += ClassName;
2212 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2213 // Check for a forward reference.
2214 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2215 if (GV) {
2216 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2217 "Forward metaclass reference has incorrect type.");
2218 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2219 GV->setInitializer(Init);
2220 GV->setSection(Section);
2221 GV->setAlignment(4);
2222 CGM.AddUsedGlobal(GV);
2223 }
2224 else
2225 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002226 DefinedClasses.push_back(GV);
2227}
2228
2229llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2230 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002231 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002232 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002233 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002234
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002235 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002236 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002237
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238 std::vector<llvm::Constant*> Values(12);
2239 // The isa for the metaclass is the root of the hierarchy.
2240 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2241 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2242 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002243 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002244 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002245 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002246 // The super class for the metaclass is emitted as the name of the
2247 // super class. The runtime fixes this up to point to the
2248 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002249 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002250 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002251 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002252 ObjCTypes.ClassPtrTy);
2253 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002254 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002255 }
2256 Values[ 2] = GetClassName(ID->getIdentifier());
2257 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002258 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2259 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2260 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002261 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002262 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002263 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002264 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002265 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002266 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002267 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002268 Values[ 9] = Protocols;
2269 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002270 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002271 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002272 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002273 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002274 Values);
2275
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002276 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002277 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002278
2279 // Check for a forward reference.
2280 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2281 if (GV) {
2282 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2283 "Forward metaclass reference has incorrect type.");
2284 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2285 GV->setInitializer(Init);
2286 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002287 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002288 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002289 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002290 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002291 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002292 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002293 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002294
2295 return GV;
2296}
2297
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002298llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002299 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002300
Mike Stump18bb9282009-05-16 07:57:57 +00002301 // FIXME: Should we look these up somewhere other than the module. Its a bit
2302 // silly since we only generate these while processing an implementation, so
2303 // exactly one pointer would work if know when we entered/exitted an
2304 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002305
2306 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002307 // Previously, metaclass with internal linkage may have been defined.
2308 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002309 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2310 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002311 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2312 "Forward metaclass reference has incorrect type.");
2313 return GV;
2314 } else {
2315 // Generate as an external reference to keep a consistent
2316 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002317 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002318 llvm::GlobalValue::ExternalLinkage,
2319 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002320 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002321 }
2322}
2323
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002324llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2325 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2326
2327 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2328 true)) {
2329 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2330 "Forward class metadata reference has incorrect type.");
2331 return GV;
2332 } else {
2333 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2334 llvm::GlobalValue::ExternalLinkage,
2335 0,
2336 Name);
2337 }
2338}
2339
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340/*
2341 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002342 uint32_t size;
2343 const char *weak_ivar_layout;
2344 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002345 };
2346*/
2347llvm::Constant *
2348CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002349 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002350 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002351
2352 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002353 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002354 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002355 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002356 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002357
2358 // Return null if no extension bits are used.
2359 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002360 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002361
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002362 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002363 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002364 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002365 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002366 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002367}
2368
2369/*
2370 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002371 char *ivar_name;
2372 char *ivar_type;
2373 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002374 };
2375
2376 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002377 int ivar_count;
2378 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002379 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002380*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002381llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002382 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002383 std::vector<llvm::Constant*> Ivars, Ivar(3);
2384
2385 // When emitting the root class GCC emits ivar entries for the
2386 // actual class structure. It is not clear if we need to follow this
2387 // behavior; for now lets try and get away with not doing it. If so,
2388 // the cleanest solution would be to make up an ObjCInterfaceDecl
2389 // for the class.
2390 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002391 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002392
2393 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002394 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002395
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002396 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002397 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002398
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002399 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2400 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002401 // Ignore unnamed bit-fields.
2402 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002403 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002404 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2405 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002406 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002407 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002408 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002409 }
2410
2411 // Return null for empty list.
2412 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002413 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002414
2415 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002416 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002417 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002418 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002419 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002420 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002422 llvm::GlobalVariable *GV;
2423 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002424 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002426 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002427 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002428 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002429 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002430 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002431 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002432}
2433
2434/*
2435 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002436 SEL method_name;
2437 char *method_types;
2438 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002439 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002440
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002441 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002442 struct objc_method_list *obsolete;
2443 int count;
2444 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002445 };
2446*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002447
2448/// GetMethodConstant - Return a struct objc_method constant for the
2449/// given method if it has been defined. The result is null if the
2450/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002451llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002452 // FIXME: Use DenseMap::lookup
2453 llvm::Function *Fn = MethodDefinitions[MD];
2454 if (!Fn)
2455 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002456
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002457 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002458 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002459 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002460 ObjCTypes.SelectorPtrTy);
2461 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002462 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002463 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002464}
2465
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002466llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002467 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002468 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002469 // Return null for empty list.
2470 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002471 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002472
2473 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002474 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002475 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002476 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002477 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002478 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002479 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002480
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002481 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002482 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002483 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002484}
2485
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002486llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002487 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002488 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002489 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002490
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002491 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002492 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002493 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002494 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002495 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002496 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002497 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002498 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002499 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002500
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002501 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002502}
2503
Daniel Dunbar30c65362009-03-09 20:09:19 +00002504llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002505CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002506 llvm::Constant *Init,
2507 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002508 unsigned Align,
2509 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002510 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002511 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002512 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002513 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002514 if (Section)
2515 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002516 if (Align)
2517 GV->setAlignment(Align);
2518 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002519 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002520 return GV;
2521}
2522
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002523llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002524 // Abuse this interface function as a place to finalize.
2525 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002526 return NULL;
2527}
2528
Chris Lattnerd4808922009-03-22 21:03:39 +00002529llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002530 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002531}
2532
Chris Lattnerd4808922009-03-22 21:03:39 +00002533llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002534 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002535}
2536
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002537llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2538 return ObjCTypes.getCopyStructFn();
2539}
2540
Chris Lattnerd4808922009-03-22 21:03:39 +00002541llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002542 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002543}
2544
John McCallbd309292010-07-06 01:34:17 +00002545void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2546 return EmitTryOrSynchronizedStmt(CGF, S);
2547}
2548
2549void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2550 const ObjCAtSynchronizedStmt &S) {
2551 return EmitTryOrSynchronizedStmt(CGF, S);
2552}
2553
John McCall65bea082010-07-21 06:59:36 +00002554namespace {
John McCallcda666c2010-07-21 07:22:38 +00002555 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002556 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002557 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002558 llvm::Value *CallTryExitVar;
2559 llvm::Value *ExceptionData;
2560 ObjCTypesHelper &ObjCTypes;
2561 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002562 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002563 llvm::Value *CallTryExitVar,
2564 llvm::Value *ExceptionData,
2565 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002566 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002567 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2568
2569 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2570 // Check whether we need to call objc_exception_try_exit.
2571 // In optimized code, this branch will always be folded.
2572 llvm::BasicBlock *FinallyCallExit =
2573 CGF.createBasicBlock("finally.call_exit");
2574 llvm::BasicBlock *FinallyNoCallExit =
2575 CGF.createBasicBlock("finally.no_call_exit");
2576 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2577 FinallyCallExit, FinallyNoCallExit);
2578
2579 CGF.EmitBlock(FinallyCallExit);
2580 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2581 ->setDoesNotThrow();
2582
2583 CGF.EmitBlock(FinallyNoCallExit);
2584
2585 if (isa<ObjCAtTryStmt>(S)) {
2586 if (const ObjCAtFinallyStmt* FinallyStmt =
2587 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2588 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2589
2590 // Currently, the end of the cleanup must always exist.
2591 CGF.EnsureInsertPoint();
2592 } else {
2593 // Emit objc_sync_exit(expr); as finally's sole statement for
2594 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002595 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002596 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2597 ->setDoesNotThrow();
2598 }
2599 }
2600 };
John McCall42227ed2010-07-31 23:20:56 +00002601
2602 class FragileHazards {
2603 CodeGenFunction &CGF;
2604 llvm::SmallVector<llvm::Value*, 20> Locals;
2605 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2606
2607 llvm::InlineAsm *ReadHazard;
2608 llvm::InlineAsm *WriteHazard;
2609
2610 llvm::FunctionType *GetAsmFnType();
2611
2612 void collectLocals();
2613 void emitReadHazard(CGBuilderTy &Builder);
2614
2615 public:
2616 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002617
John McCall42227ed2010-07-31 23:20:56 +00002618 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002619 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002620 };
2621}
2622
2623/// Create the fragile-ABI read and write hazards based on the current
2624/// state of the function, which is presumed to be immediately prior
2625/// to a @try block. These hazards are used to maintain correct
2626/// semantics in the face of optimization and the fragile ABI's
2627/// cavalier use of setjmp/longjmp.
2628FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2629 collectLocals();
2630
2631 if (Locals.empty()) return;
2632
2633 // Collect all the blocks in the function.
2634 for (llvm::Function::iterator
2635 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2636 BlocksBeforeTry.insert(&*I);
2637
2638 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2639
2640 // Create a read hazard for the allocas. This inhibits dead-store
2641 // optimizations and forces the values to memory. This hazard is
2642 // inserted before any 'throwing' calls in the protected scope to
2643 // reflect the possibility that the variables might be read from the
2644 // catch block if the call throws.
2645 {
2646 std::string Constraint;
2647 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2648 if (I) Constraint += ',';
2649 Constraint += "*m";
2650 }
2651
2652 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2653 }
2654
2655 // Create a write hazard for the allocas. This inhibits folding
2656 // loads across the hazard. This hazard is inserted at the
2657 // beginning of the catch path to reflect the possibility that the
2658 // variables might have been written within the protected scope.
2659 {
2660 std::string Constraint;
2661 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2662 if (I) Constraint += ',';
2663 Constraint += "=*m";
2664 }
2665
2666 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2667 }
2668}
2669
2670/// Emit a write hazard at the current location.
2671void FragileHazards::emitWriteHazard() {
2672 if (Locals.empty()) return;
2673
2674 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2675 ->setDoesNotThrow();
2676}
2677
John McCall42227ed2010-07-31 23:20:56 +00002678void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2679 assert(!Locals.empty());
2680 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2681 ->setDoesNotThrow();
2682}
2683
2684/// Emit read hazards in all the protected blocks, i.e. all the blocks
2685/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002686void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002687 if (Locals.empty()) return;
2688
2689 CGBuilderTy Builder(CGF.getLLVMContext());
2690
2691 // Iterate through all blocks, skipping those prior to the try.
2692 for (llvm::Function::iterator
2693 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2694 llvm::BasicBlock &BB = *FI;
2695 if (BlocksBeforeTry.count(&BB)) continue;
2696
2697 // Walk through all the calls in the block.
2698 for (llvm::BasicBlock::iterator
2699 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2700 llvm::Instruction &I = *BI;
2701
2702 // Ignore instructions that aren't non-intrinsic calls.
2703 // These are the only calls that can possibly call longjmp.
2704 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2705 if (isa<llvm::IntrinsicInst>(I))
2706 continue;
2707
2708 // Ignore call sites marked nounwind. This may be questionable,
2709 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2710 llvm::CallSite CS(&I);
2711 if (CS.doesNotThrow()) continue;
2712
John McCall2dd7d442010-08-04 05:59:32 +00002713 // Insert a read hazard before the call. This will ensure that
2714 // any writes to the locals are performed before making the
2715 // call. If the call throws, then this is sufficient to
2716 // guarantee correctness as long as it doesn't also write to any
2717 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002718 Builder.SetInsertPoint(&BB, BI);
2719 emitReadHazard(Builder);
2720 }
2721 }
2722}
2723
2724static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2725 if (V) S.insert(V);
2726}
2727
2728void FragileHazards::collectLocals() {
2729 // Compute a set of allocas to ignore.
2730 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2731 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2732 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2733 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2734
2735 // Collect all the allocas currently in the function. This is
2736 // probably way too aggressive.
2737 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2738 for (llvm::BasicBlock::iterator
2739 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2740 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2741 Locals.push_back(&*I);
2742}
2743
2744llvm::FunctionType *FragileHazards::GetAsmFnType() {
2745 std::vector<const llvm::Type *> Tys(Locals.size());
2746 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2747 Tys[I] = Locals[I]->getType();
2748 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002749}
2750
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002751/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002752
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002753 Objective-C setjmp-longjmp (sjlj) Exception Handling
2754 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002755
John McCallbd309292010-07-06 01:34:17 +00002756 A catch buffer is a setjmp buffer plus:
2757 - a pointer to the exception that was caught
2758 - a pointer to the previous exception data buffer
2759 - two pointers of reserved storage
2760 Therefore catch buffers form a stack, with a pointer to the top
2761 of the stack kept in thread-local storage.
2762
2763 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2764 objc_exception_try_exit pops the given catch buffer, which is
2765 required to be the top of the EH stack.
2766 objc_exception_throw pops the top of the EH stack, writes the
2767 thrown exception into the appropriate field, and longjmps
2768 to the setjmp buffer. It crashes the process (with a printf
2769 and an abort()) if there are no catch buffers on the stack.
2770 objc_exception_extract just reads the exception pointer out of the
2771 catch buffer.
2772
2773 There's no reason an implementation couldn't use a light-weight
2774 setjmp here --- something like __builtin_setjmp, but API-compatible
2775 with the heavyweight setjmp. This will be more important if we ever
2776 want to implement correct ObjC/C++ exception interactions for the
2777 fragile ABI.
2778
2779 Note that for this use of setjmp/longjmp to be correct, we may need
2780 to mark some local variables volatile: if a non-volatile local
2781 variable is modified between the setjmp and the longjmp, it has
2782 indeterminate value. For the purposes of LLVM IR, it may be
2783 sufficient to make loads and stores within the @try (to variables
2784 declared outside the @try) volatile. This is necessary for
2785 optimized correctness, but is not currently being done; this is
2786 being tracked as rdar://problem/8160285
2787
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002788 The basic framework for a @try-catch-finally is as follows:
2789 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002790 objc_exception_data d;
2791 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002792 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002793
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002794 objc_exception_try_enter(&d);
2795 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002796 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002797 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002798 // exception path
2799 id _caught = objc_exception_extract(&d);
2800
2801 // enter new try scope for handlers
2802 if (!setjmp(d.jmp_buf)) {
2803 ... match exception and execute catch blocks ...
2804
2805 // fell off end, rethrow.
2806 _rethrow = _caught;
2807 ... jump-through-finally to finally_rethrow ...
2808 } else {
2809 // exception in catch block
2810 _rethrow = objc_exception_extract(&d);
2811 _call_try_exit = false;
2812 ... jump-through-finally to finally_rethrow ...
2813 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002814 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002815 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002816
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002817 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002818 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002819 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002820
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002821 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002822 ... dispatch to finally destination ...
2823
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002824 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002825 objc_exception_throw(_rethrow);
2826
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002827 finally_end:
2828 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002829
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002830 This framework differs slightly from the one gcc uses, in that gcc
2831 uses _rethrow to determine if objc_exception_try_exit should be called
2832 and if the object should be rethrown. This breaks in the face of
2833 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002834
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002835 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002836
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002837 - If there are no catch blocks, then we avoid emitting the second
2838 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002839
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002840 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2841 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002842
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002843 - FIXME: If there is no @finally block we can do a few more
2844 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002845
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002846 Rethrows and Jumps-Through-Finally
2847 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002848
John McCallbd309292010-07-06 01:34:17 +00002849 '@throw;' is supported by pushing the currently-caught exception
2850 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002851
John McCallbd309292010-07-06 01:34:17 +00002852 Branches through the @finally block are handled with an ordinary
2853 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2854 exceptions are not compatible with C++ exceptions, and this is
2855 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002856
John McCallbd309292010-07-06 01:34:17 +00002857 @synchronized(expr) { stmt; } is emitted as if it were:
2858 id synch_value = expr;
2859 objc_sync_enter(synch_value);
2860 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002861*/
2862
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002863void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2864 const Stmt &S) {
2865 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002866
2867 // A destination for the fall-through edges of the catch handlers to
2868 // jump to.
2869 CodeGenFunction::JumpDest FinallyEnd =
2870 CGF.getJumpDestInCurrentScope("finally.end");
2871
2872 // A destination for the rethrow edge of the catch handlers to jump
2873 // to.
2874 CodeGenFunction::JumpDest FinallyRethrow =
2875 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002876
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002877 // For @synchronized, call objc_sync_enter(sync.expr). The
2878 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002879 // @synchronized. We can't avoid a temp here because we need the
2880 // value to be preserved. If the backend ever does liveness
2881 // correctly after setjmp, this will be unnecessary.
2882 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002883 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002884 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002885 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2886 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002887 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2888 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002889
2890 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2891 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002892 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002893
John McCall2dd7d442010-08-04 05:59:32 +00002894 // Allocate memory for the setjmp buffer. This needs to be kept
2895 // live throughout the try and catch blocks.
2896 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2897 "exceptiondata.ptr");
2898
John McCall42227ed2010-07-31 23:20:56 +00002899 // Create the fragile hazards. Note that this will not capture any
2900 // of the allocas required for exception processing, but will
2901 // capture the current basic block (which extends all the way to the
2902 // setjmp call) as "before the @try".
2903 FragileHazards Hazards(CGF);
2904
John McCallbd309292010-07-06 01:34:17 +00002905 // Create a flag indicating whether the cleanup needs to call
2906 // objc_exception_try_exit. This is true except when
2907 // - no catches match and we're branching through the cleanup
2908 // just to rethrow the exception, or
2909 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002910 // The setjmp-safety rule here is that we should always store to this
2911 // variable in a place that dominates the branch through the cleanup
2912 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002913 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002914 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002915
John McCallbd309292010-07-06 01:34:17 +00002916 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002917 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002918 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002919 CallTryExitVar,
2920 ExceptionData,
2921 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002922
2923 // Enter a try block:
2924 // - Call objc_exception_try_enter to push ExceptionData on top of
2925 // the EH stack.
2926 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2927 ->setDoesNotThrow();
2928
2929 // - Call setjmp on the exception data buffer.
2930 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2931 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2932 llvm::Value *SetJmpBuffer =
2933 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2934 llvm::CallInst *SetJmpResult =
2935 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2936 SetJmpResult->setDoesNotThrow();
2937
2938 // If setjmp returned 0, enter the protected block; otherwise,
2939 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002940 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2941 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002942 llvm::Value *DidCatch =
2943 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2944 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002945
John McCallbd309292010-07-06 01:34:17 +00002946 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002947 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002948 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002949 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002950 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002951
2952 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002953
John McCallbd309292010-07-06 01:34:17 +00002954 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002955 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002956
John McCall42227ed2010-07-31 23:20:56 +00002957 // Don't optimize loads of the in-scope locals across this point.
2958 Hazards.emitWriteHazard();
2959
John McCallbd309292010-07-06 01:34:17 +00002960 // For a @synchronized (or a @try with no catches), just branch
2961 // through the cleanup to the rethrow block.
2962 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2963 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002964 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002965 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002966
2967 // Otherwise, we have to match against the caught exceptions.
2968 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002969 // Retrieve the exception object. We may emit multiple blocks but
2970 // nothing can cross this so the value is already in SSA form.
2971 llvm::CallInst *Caught =
2972 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2973 ExceptionData, "caught");
2974 Caught->setDoesNotThrow();
2975
John McCallbd309292010-07-06 01:34:17 +00002976 // Push the exception to rethrow onto the EH value stack for the
2977 // benefit of any @throws in the handlers.
2978 CGF.ObjCEHValueStack.push_back(Caught);
2979
Douglas Gregor96c79492010-04-23 22:50:49 +00002980 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002981
John McCall2dd7d442010-08-04 05:59:32 +00002982 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00002983
John McCall2dd7d442010-08-04 05:59:32 +00002984 llvm::BasicBlock *CatchBlock = 0;
2985 llvm::BasicBlock *CatchHandler = 0;
2986 if (HasFinally) {
2987 // Enter a new exception try block (in case a @catch block
2988 // throws an exception).
2989 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2990 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002991
John McCall2dd7d442010-08-04 05:59:32 +00002992 llvm::CallInst *SetJmpResult =
2993 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2994 "setjmp.result");
2995 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002996
John McCall2dd7d442010-08-04 05:59:32 +00002997 llvm::Value *Threw =
2998 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2999
3000 CatchBlock = CGF.createBasicBlock("catch");
3001 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3002 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3003
3004 CGF.EmitBlock(CatchBlock);
3005 }
3006
3007 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003009 // Handle catch list. As a special case we check if everything is
3010 // matched and avoid generating code for falling off the end if
3011 // so.
3012 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003013 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3014 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003015
Douglas Gregor46a572b2010-04-26 16:46:50 +00003016 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003017 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003018
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003019 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003020 if (!CatchParam) {
3021 AllMatched = true;
3022 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003023 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003024
John McCallbd309292010-07-06 01:34:17 +00003025 // catch(id e) always matches under this ABI, since only
3026 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003027 // FIXME: For the time being we also match id<X>; this should
3028 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003029 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003030 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003031 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003032
John McCallbd309292010-07-06 01:34:17 +00003033 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003034 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003035 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3036
Anders Carlsson9396a892008-09-11 09:15:33 +00003037 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00003038 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003039 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003040
3041 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003042 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003043 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003044
Anders Carlsson9396a892008-09-11 09:15:33 +00003045 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003046
3047 // The scope of the catch variable ends right here.
3048 CatchVarCleanups.ForceCleanup();
3049
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003050 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003051 break;
3052 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003053
Steve Naroff7cae42b2009-07-10 23:34:53 +00003054 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003055 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003056
3057 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003058 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3059 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003060
3061 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003062 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003063
John McCallbd309292010-07-06 01:34:17 +00003064 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003065 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3066 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003067 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003068
John McCallbd309292010-07-06 01:34:17 +00003069 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3070 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003071
3072 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003073 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003074
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003075 // Emit the @catch block.
3076 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003077
3078 // Collect any cleanups for the catch variable. The scope lasts until
3079 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003080 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003081
Steve Naroff371b8fb2009-03-03 19:52:17 +00003082 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003083 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003084
John McCallbd309292010-07-06 01:34:17 +00003085 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003086 llvm::Value *Tmp =
3087 CGF.Builder.CreateBitCast(Caught,
3088 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003089 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003090 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003091
Anders Carlsson9396a892008-09-11 09:15:33 +00003092 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003093
3094 // We're done with the catch variable.
3095 CatchVarCleanups.ForceCleanup();
3096
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003097 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003098
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003099 CGF.EmitBlock(NextCatchBlock);
3100 }
3101
John McCallbd309292010-07-06 01:34:17 +00003102 CGF.ObjCEHValueStack.pop_back();
3103
John McCall2dd7d442010-08-04 05:59:32 +00003104 // If nothing wanted anything to do with the caught exception,
3105 // kill the extract call.
3106 if (Caught->use_empty())
3107 Caught->eraseFromParent();
3108
3109 if (!AllMatched)
3110 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3111
3112 if (HasFinally) {
3113 // Emit the exception handler for the @catch blocks.
3114 CGF.EmitBlock(CatchHandler);
3115
3116 // In theory we might now need a write hazard, but actually it's
3117 // unnecessary because there's no local-accessing code between
3118 // the try's write hazard and here.
3119 //Hazards.emitWriteHazard();
3120
3121 // Don't pop the catch handler; the throw already did.
3122 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003123 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003124 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003125 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003126
John McCall42227ed2010-07-31 23:20:56 +00003127 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003128 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003129
John McCallbd309292010-07-06 01:34:17 +00003130 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003131 CGF.Builder.restoreIP(TryFallthroughIP);
3132 if (CGF.HaveInsertPoint())
3133 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003134 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003135 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003136
John McCallbd309292010-07-06 01:34:17 +00003137 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003138 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003139 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003140 if (CGF.HaveInsertPoint()) {
John McCall2dd7d442010-08-04 05:59:32 +00003141 // Just look in the buffer for the exception to throw.
3142 llvm::CallInst *Caught =
3143 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3144 ExceptionData);
3145 Caught->setDoesNotThrow();
3146
3147 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallbd309292010-07-06 01:34:17 +00003148 ->setDoesNotThrow();
3149 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003150 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003151
John McCall42227ed2010-07-31 23:20:56 +00003152 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003153}
3154
3155void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003156 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003157 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003158
Anders Carlssone005aa12008-09-09 16:16:55 +00003159 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3160 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003161 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003162 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3163 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003164 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003165 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003166 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003167 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003168
John McCallbd309292010-07-06 01:34:17 +00003169 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3170 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003171 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003172
3173 // Clear the insertion point to indicate we are in unreachable code.
3174 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003175}
3176
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003177/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003178/// object: objc_read_weak (id *src)
3179///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003180llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003181 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003182 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003183 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3184 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3185 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003186 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003187 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003188 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003189 return read_weak;
3190}
3191
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003192/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3193/// objc_assign_weak (id src, id *dst)
3194///
3195void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003196 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003197 const llvm::Type * SrcTy = src->getType();
3198 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003199 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003200 assert(Size <= 8 && "does not support size > 8");
3201 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003202 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003203 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3204 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003205 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3206 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003207 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003208 src, dst, "weakassign");
3209 return;
3210}
3211
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003212/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3213/// objc_assign_global (id src, id *dst)
3214///
3215void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003216 llvm::Value *src, llvm::Value *dst,
3217 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003218 const llvm::Type * SrcTy = src->getType();
3219 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003220 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003221 assert(Size <= 8 && "does not support size > 8");
3222 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003223 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003224 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3225 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003226 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3227 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003228 if (!threadlocal)
3229 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3230 src, dst, "globalassign");
3231 else
3232 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3233 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003234 return;
3235}
3236
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003237/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003238/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003239///
3240void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003241 llvm::Value *src, llvm::Value *dst,
3242 llvm::Value *ivarOffset) {
3243 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003244 const llvm::Type * SrcTy = src->getType();
3245 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003246 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003247 assert(Size <= 8 && "does not support size > 8");
3248 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003249 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003250 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3251 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003252 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3253 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003254 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3255 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003256 return;
3257}
3258
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003259/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3260/// objc_assign_strongCast (id src, id *dst)
3261///
3262void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003263 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003264 const llvm::Type * SrcTy = src->getType();
3265 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003266 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003267 assert(Size <= 8 && "does not support size > 8");
3268 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003269 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003270 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3271 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003272 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3273 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003274 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003275 src, dst, "weakassign");
3276 return;
3277}
3278
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003279void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003280 llvm::Value *DestPtr,
3281 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003282 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003283 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3284 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003285 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003286 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003287 return;
3288}
3289
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003290/// EmitObjCValueForIvar - Code Gen for ivar reference.
3291///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003292LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3293 QualType ObjectTy,
3294 llvm::Value *BaseValue,
3295 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003296 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003297 const ObjCInterfaceDecl *ID =
3298 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003299 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3300 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003301}
3302
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003303llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003304 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003305 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003306 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003307 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003308 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3309 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003310}
3311
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003312/* *** Private Interface *** */
3313
3314/// EmitImageInfo - Emit the image info marker used to encode some module
3315/// level information.
3316///
3317/// See: <rdr://4810609&4810587&4810587>
3318/// struct IMAGE_INFO {
3319/// unsigned version;
3320/// unsigned flags;
3321/// };
3322enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003323 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003324 eImageInfo_GarbageCollected = (1 << 1),
3325 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003326 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3327
Daniel Dunbar5e639272010-04-25 20:39:01 +00003328 // A flag indicating that the module has no instances of a @synthesize of a
3329 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003330 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003331};
3332
Daniel Dunbar5e639272010-04-25 20:39:01 +00003333void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003334 unsigned version = 0; // Version is unused?
3335 unsigned flags = 0;
3336
3337 // FIXME: Fix and continue?
3338 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3339 flags |= eImageInfo_GarbageCollected;
3340 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3341 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003342
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003343 // We never allow @synthesize of a superclass property.
3344 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003345
Chris Lattner5e016ae2010-06-27 07:15:29 +00003346 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3347
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003348 // Emitted as int[2];
3349 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003350 llvm::ConstantInt::get(Int32Ty, version),
3351 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003352 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003353 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003354
3355 const char *Section;
3356 if (ObjCABI == 1)
3357 Section = "__OBJC, __image_info,regular";
3358 else
3359 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003360 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003361 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003362 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003363 Section,
3364 0,
3365 true);
3366 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003367}
3368
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003369
3370// struct objc_module {
3371// unsigned long version;
3372// unsigned long size;
3373// const char *name;
3374// Symtab symtab;
3375// };
3376
3377// FIXME: Get from somewhere
3378static const int ModuleVersion = 7;
3379
3380void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003381 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003382
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003383 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003384 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3385 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003386 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003387 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003388 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003390 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003391 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003392 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003393}
3394
3395llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003396 unsigned NumClasses = DefinedClasses.size();
3397 unsigned NumCategories = DefinedCategories.size();
3398
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003399 // Return null if no symbols were defined.
3400 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003401 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003402
3403 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003404 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003405 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003406 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3407 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003408
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003409 // The runtime expects exactly the list of defined classes followed
3410 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003411 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003412 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003413 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003414 ObjCTypes.Int8PtrTy);
3415 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003416 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003417 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003418 ObjCTypes.Int8PtrTy);
3419
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003420 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003421 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003422 NumClasses + NumCategories),
3423 Symbols);
3424
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003425 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003426
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003427 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003428 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3429 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003430 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003431 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003432}
3433
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003434llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003435 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003436 LazySymbols.insert(ID->getIdentifier());
3437
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003438 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003439
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003440 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003441 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003442 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003443 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003444 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003445 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3446 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003447 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003448 }
3449
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003450 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003451}
3452
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003453llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3454 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003455 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003456
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003457 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003458 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003459 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003460 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003461 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003462 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3463 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003464 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003465 }
3466
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003467 if (lvalue)
3468 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003469 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003470}
3471
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003472llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003473 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003474
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003475 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003476 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003477 llvm::ConstantArray::get(VMContext,
3478 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003479 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003480 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003481
Owen Anderson170229f2009-07-14 23:10:40 +00003482 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003483}
3484
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003485/// GetIvarLayoutName - Returns a unique constant for the given
3486/// ivar layout bitmap.
3487llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003488 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003489 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003490}
3491
John McCall8ccfcb52009-09-24 19:53:00 +00003492static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003493 if (FQT.isObjCGCStrong())
John McCall8ccfcb52009-09-24 19:53:00 +00003494 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003495
3496 if (FQT.isObjCGCWeak())
John McCall8ccfcb52009-09-24 19:53:00 +00003497 return Qualifiers::Weak;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003498
Fariborz Jahanian10e9bff2009-09-11 17:39:05 +00003499 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003500 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003501
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003502 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003503 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003504
John McCall8ccfcb52009-09-24 19:53:00 +00003505 return Qualifiers::GCNone;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003506}
3507
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003508void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003509 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003510 bool ForStrongLayout,
3511 bool &HasUnion) {
3512 const RecordDecl *RD = RT->getDecl();
3513 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003514 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003515 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003516 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003517 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003518
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003519 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3520 ForStrongLayout, HasUnion);
3521}
3522
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003523void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003524 const llvm::StructLayout *Layout,
3525 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003526 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003527 unsigned int BytePos, bool ForStrongLayout,
3528 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003529 bool IsUnion = (RD && RD->isUnion());
3530 uint64_t MaxUnionIvarSize = 0;
3531 uint64_t MaxSkippedUnionIvarSize = 0;
3532 FieldDecl *MaxField = 0;
3533 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003534 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003535 uint64_t MaxFieldOffset = 0;
3536 uint64_t MaxSkippedFieldOffset = 0;
3537 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003538
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003539 if (RecFields.empty())
3540 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003541 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3542 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3543
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003544 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003545 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003546 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003547 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003548 // Note that 'i' here is actually the field index inside RD of Field,
3549 // although this dependency is hidden.
3550 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3551 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003552 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003553 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003554
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003555 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003556 if (!Field->getIdentifier() || Field->isBitField()) {
3557 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003558 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003559 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003560 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003561
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003562 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003563 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003564 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003565 if (FQT->isUnionType())
3566 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003567
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003568 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003569 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003570 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003571 continue;
3572 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003573
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003574 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003575 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003576 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003577 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003578 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003579 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003580 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3581 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003582 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003583 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003584 FQT = CArray->getElementType();
3585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003586
3587 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003588 "layout for array of unions not supported");
3589 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003590 int OldIndex = IvarsInfo.size() - 1;
3591 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003592
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003593 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003594 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003595 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003596
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003597 // Replicate layout information for each array element. Note that
3598 // one element is already done.
3599 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003600 for (int FirstIndex = IvarsInfo.size() - 1,
3601 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003602 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003603 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3604 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3605 IvarsInfo[i].ivar_size));
3606 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3607 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3608 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003609 }
3610 continue;
3611 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003612 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003613 // At this point, we are done with Record/Union and array there of.
3614 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003615 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003616
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003617 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003618 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3619 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003620 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003621 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003622 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003623 MaxUnionIvarSize = UnionIvarSize;
3624 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003625 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003626 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003627 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003628 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003629 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003630 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003631 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003632 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3633 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003634 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003635 // FIXME: Why the asymmetry? We divide by word size in bits on other
3636 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003637 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003638 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003639 MaxSkippedUnionIvarSize = UnionIvarSize;
3640 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003641 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003642 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003643 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003644 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003645 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003646 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003647 }
3648 }
3649 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003650
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003651 if (LastFieldBitfield) {
3652 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003653 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3654 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003655 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003656 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003657 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003658 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3659 + ((BitFieldSize % ByteSizeInBits) != 0);
3660 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003661 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003663 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003664 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003665 MaxUnionIvarSize));
3666 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003667 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003668 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003669}
3670
3671/// BuildIvarLayout - Builds ivar layout bitmap for the class
3672/// implementation for the __strong or __weak case.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003673/// The layout map displays which words in ivar list must be skipped
3674/// and which must be scanned by GC (see below). String is built of bytes.
3675/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003676/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003677/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003678/// represented by a 0x00 byte which also ends the string.
3679/// 1. when ForStrongLayout is true, following ivars are scanned:
3680/// - id, Class
3681/// - object *
3682/// - __strong anything
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003683///
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003684/// 2. When ForStrongLayout is false, following ivars are scanned:
3685/// - __weak anything
3686///
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003687llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003688 const ObjCImplementationDecl *OMD,
3689 bool ForStrongLayout) {
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003690 bool hasUnion = false;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003691
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003692 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003693 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003694 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson0b75f232009-07-31 20:28:54 +00003695 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003696
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003697 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003698 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003699 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003700
Daniel Dunbar98ba9642009-05-04 04:10:48 +00003701 // Add this implementations synthesized ivars.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003702 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianaef66222010-02-19 00:31:17 +00003703 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003704 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3705 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003707 if (RecFields.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003708 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003709
3710 SkipIvars.clear();
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003711 IvarsInfo.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003712
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003713 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003714 if (IvarsInfo.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003715 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003716
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003717 // Sort on byte position in case we encounterred a union nested in
3718 // the ivar list.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003719 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003720 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003721 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003722 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003723
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003724 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003725 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003726 unsigned int WordSize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003727 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003728 if (IvarsInfo[0].ivar_bytepos == 0) {
3729 WordsToSkip = 0;
3730 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003731 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003732 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3733 WordsToScan = IvarsInfo[0].ivar_size;
3734 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003735 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003736 unsigned int TailPrevGCObjC =
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003737 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003738 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003739 // consecutive 'scanned' object pointers.
3740 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003741 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003742 // Skip over 'gc'able object pointer which lay over each other.
3743 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3744 continue;
3745 // Must skip over 1 or more words. We save current skip/scan values
3746 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003747 SKIP_SCAN SkScan;
3748 SkScan.skip = WordsToSkip;
3749 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003750 SkipScanIvars.push_back(SkScan);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003751
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003752 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003753 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3754 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003755 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003756 WordsToSkip = 0;
3757 WordsToScan = IvarsInfo[i].ivar_size;
3758 }
3759 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003760 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003761 SKIP_SCAN SkScan;
3762 SkScan.skip = WordsToSkip;
3763 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003764 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003765 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003766
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003767 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003768 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003769 int LastByteSkipped =
3770 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003771 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003772 int LastByteScanned =
3773 IvarsInfo[LastIndex].ivar_bytepos +
3774 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003775 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003776 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003777 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003778 SKIP_SCAN SkScan;
3779 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3780 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003781 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003782 }
3783 }
3784 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3785 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003786 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003787 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003788 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3789 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3790 // 0xM0 followed by 0x0N detected.
3791 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3792 for (int j = i+1; j < SkipScan; j++)
3793 SkipScanIvars[j] = SkipScanIvars[j+1];
3794 --SkipScan;
3795 }
3796 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003797
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003798 // Generate the string.
3799 std::string BitMap;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003800 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003801 unsigned char byte;
3802 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3803 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3804 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3805 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3806
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003807 // first skip big.
3808 for (unsigned int ix = 0; ix < skip_big; ix++)
3809 BitMap += (unsigned char)(0xf0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003810
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003811 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003812 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003813 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003814 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003815 byte |= 0xf;
3816 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003817 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003818 byte |= scan_small;
3819 scan_small = 0;
3820 }
3821 BitMap += byte;
3822 }
3823 // next scan big
3824 for (unsigned int ix = 0; ix < scan_big; ix++)
3825 BitMap += (unsigned char)(0x0f);
3826 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003827 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003828 byte = scan_small;
3829 BitMap += byte;
3830 }
3831 }
3832 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003833 unsigned char zero = 0;
3834 BitMap += zero;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003835
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003836 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003837 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003838 ForStrongLayout ? "strong" : "weak",
3839 OMD->getClassInterface()->getNameAsCString());
3840 const unsigned char *s = (unsigned char*)BitMap.c_str();
3841 for (unsigned i = 0; i < BitMap.size(); i++)
3842 if (!(s[i] & 0xf0))
3843 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3844 else
3845 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3846 printf("\n");
3847 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003848 llvm::GlobalVariable * Entry =
3849 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003850 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003851 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003852 1, true);
3853 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003854}
3855
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003856llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003857 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3858
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003859 // FIXME: Avoid std::string copying.
3860 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003861 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003862 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003863 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003864 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003865
Owen Anderson170229f2009-07-14 23:10:40 +00003866 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003867}
3868
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003869// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003870llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003871 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3872}
3873
3874// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003875llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003876 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3877}
3878
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003879llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003880 std::string TypeStr;
3881 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3882
3883 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003884
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003885 if (!Entry)
3886 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003887 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003888 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003889 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003890
Owen Anderson170229f2009-07-14 23:10:40 +00003891 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003892}
3893
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003894llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003895 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003896 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3897 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003898
3899 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3900
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003901 if (!Entry)
3902 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003903 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003904 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003905 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003906
Owen Anderson170229f2009-07-14 23:10:40 +00003907 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003908}
3909
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003910// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003911llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003912 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003913
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003914 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003915 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003916 llvm::ConstantArray::get(VMContext,
3917 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003918 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003919 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003920
Owen Anderson170229f2009-07-14 23:10:40 +00003921 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003922}
3923
3924// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003925// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003926llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003927CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3928 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003929 std::string TypeStr;
3930 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003931 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3932}
3933
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003934void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003935 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003936 llvm::SmallVectorImpl<char> &Name) {
3937 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003938 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003939 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3940 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003941 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003942 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003943 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003944 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003945}
3946
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003947void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003948 EmitModuleInfo();
3949
Daniel Dunbarc475d422008-10-29 22:36:39 +00003950 // Emit the dummy bodies for any protocols which were referenced but
3951 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003952 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003953 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3954 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003955 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003956
Daniel Dunbarc475d422008-10-29 22:36:39 +00003957 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00003958 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003959 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00003960 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003961 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00003962 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003963 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003964 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00003965 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00003966 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003967 }
3968
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003969 // Add assembler directives to add lazy undefined symbol references
3970 // for classes which are referenced but not defined. This is
3971 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00003972 //
3973 // FIXME: It would be nice if we had an LLVM construct for this.
3974 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3975 llvm::SmallString<256> Asm;
3976 Asm += CGM.getModule().getModuleInlineAsm();
3977 if (!Asm.empty() && Asm.back() != '\n')
3978 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003979
Daniel Dunbard027a922009-09-07 00:20:42 +00003980 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00003981 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3982 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003983 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3984 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00003985 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003986 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00003987 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003988 }
3989
3990 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
3991 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
3992 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
3993 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00003994
Daniel Dunbard027a922009-09-07 00:20:42 +00003995 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003996 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003997}
3998
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003999CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004000 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004001 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004002 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004003 ObjCABI = 2;
4004}
4005
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004006/* *** */
4007
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004008ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004009 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004010 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4011 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004012
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004013 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004014 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004015 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004016 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004017 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004018
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004019 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004020 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004021 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004022
Mike Stump18bb9282009-05-16 07:57:57 +00004023 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4024 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004025 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004026 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004027
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004028 // I'm not sure I like this. The implicit coordination is a bit
4029 // gross. We should solve this in a reasonable fashion because this
4030 // is a pretty common task (match some runtime data structure with
4031 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004032
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004033 // FIXME: This is leaked.
4034 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004035
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004036 // struct _objc_super {
4037 // id self;
4038 // Class cls;
4039 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004040 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004041 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004042 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004043 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004044 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004045 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004046 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004047 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004048 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004049
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004050 SuperCTy = Ctx.getTagDeclType(RD);
4051 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004052
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004053 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004054 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4055
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004056 // struct _prop_t {
4057 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004058 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004059 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004060 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004061 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004062 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004063
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004064 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004065 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004066 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004067 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004068 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004069 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004070 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004071 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004072 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004073 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004074 PropertyListTy);
4075 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004076 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004077
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004078 // struct _objc_method {
4079 // SEL _cmd;
4080 // char *method_type;
4081 // char *_imp;
4082 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004083 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004084 Int8PtrTy,
4085 Int8PtrTy,
4086 NULL);
4087 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004088
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004089 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004090 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004091 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004092 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004093}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004094
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004096 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004097 // struct _objc_method_description {
4098 // SEL name;
4099 // char *types;
4100 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004101 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004102 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004103 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004104 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004105 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004106 MethodDescriptionTy);
4107
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004108 // struct _objc_method_description_list {
4109 // int count;
4110 // struct _objc_method_description[1];
4111 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004112 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004113 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004114 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004115 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004116 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004117 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004118
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004119 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004120 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004121 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004122
Daniel Dunbarb036db82008-08-13 03:21:16 +00004123 // Protocol description structures
4124
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004125 // struct _objc_protocol_extension {
4126 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4127 // struct _objc_method_description_list *optional_instance_methods;
4128 // struct _objc_method_description_list *optional_class_methods;
4129 // struct _objc_property_list *instance_properties;
4130 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004131 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004132 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004133 MethodDescriptionListPtrTy,
4134 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004135 PropertyListPtrTy,
4136 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004137 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004138 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004139
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004140 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004141 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004142
Daniel Dunbarc475d422008-10-29 22:36:39 +00004143 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004144
Owen Andersonc36edfe2009-08-13 23:27:53 +00004145 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4146 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004147
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004149 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004150 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004151 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004152 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004153 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004154 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4155
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004156 // struct _objc_protocol {
4157 // struct _objc_protocol_extension *isa;
4158 // char *protocol_name;
4159 // struct _objc_protocol **_objc_protocol_list;
4160 // struct _objc_method_description_list *instance_methods;
4161 // struct _objc_method_description_list *class_methods;
4162 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004163 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004164 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004165 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004166 MethodDescriptionListPtrTy,
4167 MethodDescriptionListPtrTy,
4168 NULL);
4169 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4170
4171 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004172 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004173 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004174 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004175 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004176
4177 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004178 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004179 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004180
4181 // Class description structures
4182
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004183 // struct _objc_ivar {
4184 // char *ivar_name;
4185 // char *ivar_type;
4186 // int ivar_offset;
4187 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004188 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004189 Int8PtrTy,
4190 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004191 NULL);
4192 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4193
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004194 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004195 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004196 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004197 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004198
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004199 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004200 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004201 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004202 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004203
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004204 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004205 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004206 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004207 Int8PtrTy,
4208 PropertyListPtrTy,
4209 NULL);
4210 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004211 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004212
Owen Andersonc36edfe2009-08-13 23:27:53 +00004213 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004214
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004215 // struct _objc_class {
4216 // Class isa;
4217 // Class super_class;
4218 // char *name;
4219 // long version;
4220 // long info;
4221 // long instance_size;
4222 // struct _objc_ivar_list *ivars;
4223 // struct _objc_method_list *methods;
4224 // struct _objc_cache *cache;
4225 // struct _objc_protocol_list *protocols;
4226 // char *ivar_layout;
4227 // struct _objc_class_ext *ext;
4228 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004229 T = llvm::StructType::get(VMContext,
4230 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004231 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004232 Int8PtrTy,
4233 LongTy,
4234 LongTy,
4235 LongTy,
4236 IvarListPtrTy,
4237 MethodListPtrTy,
4238 CachePtrTy,
4239 ProtocolListPtrTy,
4240 Int8PtrTy,
4241 ClassExtensionPtrTy,
4242 NULL);
4243 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004244
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004245 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4246 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004247 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004248
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004249 // struct _objc_category {
4250 // char *category_name;
4251 // char *class_name;
4252 // struct _objc_method_list *instance_method;
4253 // struct _objc_method_list *class_method;
4254 // uint32_t size; // sizeof(struct _objc_category)
4255 // struct _objc_property_list *instance_properties;// category's @property
4256 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004257 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004258 Int8PtrTy,
4259 MethodListPtrTy,
4260 MethodListPtrTy,
4261 ProtocolListPtrTy,
4262 IntTy,
4263 PropertyListPtrTy,
4264 NULL);
4265 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4266
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004267 // Global metadata structures
4268
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004269 // struct _objc_symtab {
4270 // long sel_ref_cnt;
4271 // SEL *refs;
4272 // short cls_def_cnt;
4273 // short cat_def_cnt;
4274 // char *defs[cls_def_cnt + cat_def_cnt];
4275 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004276 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004277 SelectorPtrTy,
4278 ShortTy,
4279 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004280 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004281 NULL);
4282 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004283 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004284
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004285 // struct _objc_module {
4286 // long version;
4287 // long size; // sizeof(struct _objc_module)
4288 // char *name;
4289 // struct _objc_symtab* symtab;
4290 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004291 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004292 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004293 LongTy,
4294 Int8PtrTy,
4295 SymtabPtrTy,
4296 NULL);
4297 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004298
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299
Mike Stump18bb9282009-05-16 07:57:57 +00004300 // FIXME: This is the size of the setjmp buffer and should be target
4301 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004302 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004303
Anders Carlsson9ff22482008-09-09 10:10:21 +00004304 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004305 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004306 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004307
4308 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004309 llvm::StructType::get(VMContext,
4310 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4311 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004312 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004313 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004314 ExceptionDataTy);
4315
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004316}
4317
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004318ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004319 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004320 // struct _method_list_t {
4321 // uint32_t entsize; // sizeof(struct _objc_method)
4322 // uint32_t method_count;
4323 // struct _objc_method method_list[method_count];
4324 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004325 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004326 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004327 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004328 NULL);
4329 CGM.getModule().addTypeName("struct.__method_list_t",
4330 MethodListnfABITy);
4331 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004332 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004333
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004334 // struct _protocol_t {
4335 // id isa; // NULL
4336 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004337 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004338 // const struct method_list_t * const instance_methods;
4339 // const struct method_list_t * const class_methods;
4340 // const struct method_list_t *optionalInstanceMethods;
4341 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004342 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004343 // const uint32_t size; // sizeof(struct _protocol_t)
4344 // const uint32_t flags; // = 0
4345 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004346
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004347 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004348 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004349
Owen Anderson758428f2009-08-05 23:18:46 +00004350 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004351 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004352 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004353 ProtocolListTyHolder),
4354 MethodListnfABIPtrTy,
4355 MethodListnfABIPtrTy,
4356 MethodListnfABIPtrTy,
4357 MethodListnfABIPtrTy,
4358 PropertyListPtrTy,
4359 IntTy,
4360 IntTy,
4361 NULL);
4362 CGM.getModule().addTypeName("struct._protocol_t",
4363 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004364
4365 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004366 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004368 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004369 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004370 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004371 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004372 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004373 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004374 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004375 NULL);
4376 CGM.getModule().addTypeName("struct._objc_protocol_list",
4377 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004378 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379 ProtocolListnfABITy);
4380
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004381 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004382 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004383
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004384 // struct _ivar_t {
4385 // unsigned long int *offset; // pointer to ivar offset location
4386 // char *name;
4387 // char *type;
4388 // uint32_t alignment;
4389 // uint32_t size;
4390 // }
Mike Stump11289f42009-09-09 15:08:12 +00004391 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004392 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004393 Int8PtrTy,
4394 Int8PtrTy,
4395 IntTy,
4396 IntTy,
4397 NULL);
4398 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004399
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004400 // struct _ivar_list_t {
4401 // uint32 entsize; // sizeof(struct _ivar_t)
4402 // uint32 count;
4403 // struct _iver_t list[count];
4404 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004405 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004406 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004407 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004408 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004409 NULL);
4410 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004411
Owen Anderson9793f0e2009-07-29 22:16:19 +00004412 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004413
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004414 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004415 // uint32_t const flags;
4416 // uint32_t const instanceStart;
4417 // uint32_t const instanceSize;
4418 // uint32_t const reserved; // only when building for 64bit targets
4419 // const uint8_t * const ivarLayout;
4420 // const char *const name;
4421 // const struct _method_list_t * const baseMethods;
4422 // const struct _objc_protocol_list *const baseProtocols;
4423 // const struct _ivar_list_t *const ivars;
4424 // const uint8_t * const weakIvarLayout;
4425 // const struct _prop_list_t * const properties;
4426 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004427
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004428 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004429 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004430 IntTy,
4431 IntTy,
4432 Int8PtrTy,
4433 Int8PtrTy,
4434 MethodListnfABIPtrTy,
4435 ProtocolListnfABIPtrTy,
4436 IvarListnfABIPtrTy,
4437 Int8PtrTy,
4438 PropertyListPtrTy,
4439 NULL);
4440 CGM.getModule().addTypeName("struct._class_ro_t",
4441 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004443 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4444 std::vector<const llvm::Type*> Params;
4445 Params.push_back(ObjectPtrTy);
4446 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004447 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4449
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004450 // struct _class_t {
4451 // struct _class_t *isa;
4452 // struct _class_t * const superclass;
4453 // void *cache;
4454 // IMP *vtable;
4455 // struct class_ro_t *ro;
4456 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004457
Owen Andersonc36edfe2009-08-13 23:27:53 +00004458 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004459 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004460 llvm::StructType::get(VMContext,
4461 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004462 llvm::PointerType::getUnqual(ClassTyHolder),
4463 CachePtrTy,
4464 llvm::PointerType::getUnqual(ImpnfABITy),
4465 llvm::PointerType::getUnqual(ClassRonfABITy),
4466 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004467 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4468
4469 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004470 ClassnfABITy);
4471
Fariborz Jahanian71394042009-01-23 23:53:38 +00004472 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004473 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004474
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004475 // struct _category_t {
4476 // const char * const name;
4477 // struct _class_t *const cls;
4478 // const struct _method_list_t * const instance_methods;
4479 // const struct _method_list_t * const class_methods;
4480 // const struct _protocol_list_t * const protocols;
4481 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004482 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004483 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004484 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004485 MethodListnfABIPtrTy,
4486 MethodListnfABIPtrTy,
4487 ProtocolListnfABIPtrTy,
4488 PropertyListPtrTy,
4489 NULL);
4490 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004491
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004492 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004493 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4494 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004495
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004496 // MessageRefTy - LLVM for:
4497 // struct _message_ref_t {
4498 // IMP messenger;
4499 // SEL name;
4500 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004501
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004502 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004503 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004504 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004505 SourceLocation(),
4506 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004507 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004508 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004509 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004510 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004511 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004512
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004513 MessageRefCTy = Ctx.getTagDeclType(RD);
4514 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4515 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004516
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004517 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004518 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004519
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004520 // SuperMessageRefTy - LLVM for:
4521 // struct _super_message_ref_t {
4522 // SUPER_IMP messenger;
4523 // SEL name;
4524 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004525 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004526 SelectorPtrTy,
4527 NULL);
4528 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004530 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004531 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4532
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004533
4534 // struct objc_typeinfo {
4535 // const void** vtable; // objc_ehtype_vtable + 2
4536 // const char* name; // c++ typeinfo string
4537 // Class cls;
4538 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004539 EHTypeTy = llvm::StructType::get(VMContext,
4540 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004541 Int8PtrTy,
4542 ClassnfABIPtrTy,
4543 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004544 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004545 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004546}
4547
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004549 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004550
Fariborz Jahanian71394042009-01-23 23:53:38 +00004551 return NULL;
4552}
4553
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004554void CGObjCNonFragileABIMac::AddModuleClassList(const
4555 std::vector<llvm::GlobalValue*>
4556 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004557 const char *SymbolName,
4558 const char *SectionName) {
4559 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004560
Daniel Dunbar19573e72009-05-15 21:48:48 +00004561 if (!NumClasses)
4562 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004563
Daniel Dunbar19573e72009-05-15 21:48:48 +00004564 std::vector<llvm::Constant*> Symbols(NumClasses);
4565 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004566 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004567 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004568 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004569 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004570 NumClasses),
4571 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Daniel Dunbar19573e72009-05-15 21:48:48 +00004573 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004574 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004575 llvm::GlobalValue::InternalLinkage,
4576 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004577 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004578 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004579 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004580 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004581}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004582
Fariborz Jahanian71394042009-01-23 23:53:38 +00004583void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4584 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004585
Daniel Dunbar19573e72009-05-15 21:48:48 +00004586 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004587 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004588 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004589 "\01L_OBJC_LABEL_CLASS_$",
4590 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004591
Fariborz Jahanian67260552009-11-17 21:37:35 +00004592 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4593 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4594 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4595 continue;
4596 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004597 }
4598
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004599 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4600 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4601 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4602 continue;
4603 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4604 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004605
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004607 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4608 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004609
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004610 // Build list of all implemented category addresses in array
4611 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004612 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004613 "\01L_OBJC_LABEL_CATEGORY_$",
4614 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004616 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4617 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Daniel Dunbar5e639272010-04-25 20:39:01 +00004619 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004620}
4621
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004622/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004623/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004625/// message dispatch call for all the rest.
4626///
4627bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004628 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4629 default:
4630 assert(0 && "Invalid dispatch method!");
4631 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004632 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004633 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004634 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004635 case CodeGenOptions::Mixed:
4636 break;
4637 }
4638
4639 // If so, see whether this selector is in the white-list of things which must
4640 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004641 if (NonLegacyDispatchMethods.empty()) {
4642 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4643 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4644 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4645 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4646 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4647 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4648 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4649 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4650 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4651 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004653 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4654 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4655 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4656 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4657 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4658 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4659 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4660 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004661 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004662 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004663 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4664 &CGM.getContext().Idents.get("objects"),
4665 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004666 };
4667 NonLegacyDispatchMethods.insert(
4668 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004669 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004670
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004671 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004672}
4673
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004674// Metadata flags
4675enum MetaDataDlags {
4676 CLS = 0x0,
4677 CLS_META = 0x1,
4678 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004679 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004680 CLS_EXCEPTION = 0x20
4681};
4682/// BuildClassRoTInitializer - generate meta-data for:
4683/// struct _class_ro_t {
4684/// uint32_t const flags;
4685/// uint32_t const instanceStart;
4686/// uint32_t const instanceSize;
4687/// uint32_t const reserved; // only when building for 64bit targets
4688/// const uint8_t * const ivarLayout;
4689/// const char *const name;
4690/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004691/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004692/// const struct _ivar_list_t *const ivars;
4693/// const uint8_t * const weakIvarLayout;
4694/// const struct _prop_list_t * const properties;
4695/// }
4696///
4697llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004698 unsigned flags,
4699 unsigned InstanceStart,
4700 unsigned InstanceSize,
4701 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004702 std::string ClassName = ID->getNameAsString();
4703 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004704 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4705 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4706 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004707 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004708 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4709 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004710 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004711 // const struct _method_list_t * const baseMethods;
4712 std::vector<llvm::Constant*> Methods;
4713 std::string MethodListName("\01l_OBJC_$_");
4714 if (flags & CLS_META) {
4715 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004716 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004717 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004718 // Class methods should always be defined.
4719 Methods.push_back(GetMethodConstant(*i));
4720 }
4721 } else {
4722 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004723 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004724 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004725 // Instance methods should always be defined.
4726 Methods.push_back(GetMethodConstant(*i));
4727 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004728 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004729 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004730 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004731
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004732 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4733 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004734
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004735 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4736 if (llvm::Constant *C = GetMethodConstant(MD))
4737 Methods.push_back(C);
4738 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4739 if (llvm::Constant *C = GetMethodConstant(MD))
4740 Methods.push_back(C);
4741 }
4742 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004743 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744 Values[ 5] = EmitMethodList(MethodListName,
4745 "__DATA, __objc_const", Methods);
4746
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004747 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4748 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004749 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004750 + OID->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004751 OID->protocol_begin(),
4752 OID->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004753
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004754 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004755 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004756 else
4757 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004758 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4759 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004760 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004761 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004762 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004763 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4764 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004765 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004766 Values);
4767 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004768 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4769 llvm::GlobalValue::InternalLinkage,
4770 Init,
4771 (flags & CLS_META) ?
4772 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4773 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004774 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004775 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004776 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004777 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004778
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004779}
4780
4781/// BuildClassMetaData - This routine defines that to-level meta-data
4782/// for the given ClassName for:
4783/// struct _class_t {
4784/// struct _class_t *isa;
4785/// struct _class_t * const superclass;
4786/// void *cache;
4787/// IMP *vtable;
4788/// struct class_ro_t *ro;
4789/// }
4790///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004791llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004792 std::string &ClassName,
4793 llvm::Constant *IsAGV,
4794 llvm::Constant *SuperClassGV,
4795 llvm::Constant *ClassRoGV,
4796 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004797 std::vector<llvm::Constant*> Values(5);
4798 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799 Values[1] = SuperClassGV;
4800 if (!Values[1])
4801 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004802 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4803 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4804 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004805 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004806 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004807 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4808 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004809 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004810 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004811 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004812 if (HiddenVisibility)
4813 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004814 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004815}
4816
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004817bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004818CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004819 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004820}
4821
Daniel Dunbar961202372009-05-03 12:57:56 +00004822void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004823 uint32_t &InstanceStart,
4824 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004825 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004826 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004827
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004828 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004829 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004830
4831 // If there are no fields, the start is the same as the end.
4832 if (!RL.getFieldCount())
4833 InstanceStart = InstanceSize;
4834 else
4835 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004836}
4837
Fariborz Jahanian71394042009-01-23 23:53:38 +00004838void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4839 std::string ClassName = ID->getNameAsString();
4840 if (!ObjCEmptyCacheVar) {
4841 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842 CGM.getModule(),
4843 ObjCTypes.CacheTy,
4844 false,
4845 llvm::GlobalValue::ExternalLinkage,
4846 0,
4847 "_objc_empty_cache");
4848
Fariborz Jahanian71394042009-01-23 23:53:38 +00004849 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004850 CGM.getModule(),
4851 ObjCTypes.ImpnfABITy,
4852 false,
4853 llvm::GlobalValue::ExternalLinkage,
4854 0,
4855 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004856 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004857 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004858 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004859 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004860 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004861 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004862 uint32_t InstanceSize = InstanceStart;
4863 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004864 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4865 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004866
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004867 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004868
4869 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004870 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004871 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004872 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004873 if (ID->getNumIvarInitializers())
4874 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004875 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004876 // class is root
4877 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004878 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004879 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004880 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004881 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004882 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4883 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4884 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004885 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004886 if (Root->hasAttr<WeakImportAttr>())
4887 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004888 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004889 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004890 ObjCMetaClassName +
4891 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004892 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004893 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4894 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004895 }
4896 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4897 InstanceStart,
4898 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004899 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004900 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004901 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4902 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004903 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004904
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004905 // Metadata for the class
4906 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004907 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004908 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004909 if (ID->getNumIvarInitializers())
4910 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004911
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004912 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004913 flags |= CLS_EXCEPTION;
4914
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004915 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004916 flags |= CLS_ROOT;
4917 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004918 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004919 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004920 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004921 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004922 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004923 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4924 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004925 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004926 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004927 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004928 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004930 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004931
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004932 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004933 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004934 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4935 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004936 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004937
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004938 // Determine if this class is also "non-lazy".
4939 if (ImplementationIsNonLazy(ID))
4940 DefinedNonLazyClasses.push_back(ClassMD);
4941
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004942 // Force the definition of the EHType if necessary.
4943 if (flags & CLS_EXCEPTION)
4944 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00004945}
4946
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004947/// GenerateProtocolRef - This routine is called to generate code for
4948/// a protocol reference expression; as in:
4949/// @code
4950/// @protocol(Proto1);
4951/// @endcode
4952/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4953/// which will hold address of the protocol meta-data.
4954///
4955llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004956 const ObjCProtocolDecl *PD) {
4957
Fariborz Jahanian464423d2009-04-10 18:47:34 +00004958 // This routine is called for @protocol only. So, we must build definition
4959 // of protocol's meta-data (not a reference to it!)
4960 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004961 llvm::Constant *Init =
4962 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4963 ObjCTypes.ExternalProtocolPtrTy);
4964
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004965 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4966 ProtocolName += PD->getNameAsCString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004967
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004968 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4969 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004970 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004971 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004972 CGM.getModule(),
4973 Init->getType(), false,
4974 llvm::GlobalValue::WeakAnyLinkage,
4975 Init,
4976 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004977 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4978 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004979 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004980 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004981}
4982
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004983/// GenerateCategory - Build metadata for a category implementation.
4984/// struct _category_t {
4985/// const char * const name;
4986/// struct _class_t *const cls;
4987/// const struct _method_list_t * const instance_methods;
4988/// const struct _method_list_t * const class_methods;
4989/// const struct _protocol_list_t * const protocols;
4990/// const struct _prop_list_t * const properties;
4991/// }
4992///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004993void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004994 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004995 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004996 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4997 "_$_" + OCD->getNameAsString());
4998 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00004999 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005000
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005001 std::vector<llvm::Constant*> Values(6);
5002 Values[0] = GetClassName(OCD->getIdentifier());
5003 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005004 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005005 if (Interface->hasAttr<WeakImportAttr>())
5006 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5007
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005008 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005009 std::vector<llvm::Constant*> Methods;
5010 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005011 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005012 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005013
5014 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005015 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005016 // Instance methods should always be defined.
5017 Methods.push_back(GetMethodConstant(*i));
5018 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005019
5020 Values[2] = EmitMethodList(MethodListName,
5021 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005022 Methods);
5023
5024 MethodListName = Prefix;
5025 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5026 OCD->getNameAsString();
5027 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005028 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005029 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005030 // Class methods should always be defined.
5031 Methods.push_back(GetMethodConstant(*i));
5032 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005033
5034 Values[3] = EmitMethodList(MethodListName,
5035 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005036 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005037 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005038 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005039 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005040 llvm::SmallString<256> ExtName;
5041 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5042 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005043 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005044 + Interface->getName() + "_$_"
5045 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005046 Category->protocol_begin(),
5047 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005048 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5049 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005050 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005051 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5052 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005053 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005054
5055 llvm::Constant *Init =
5056 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005057 Values);
5058 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005059 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005060 false,
5061 llvm::GlobalValue::InternalLinkage,
5062 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005063 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005064 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005065 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005066 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005067 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005068 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005069
5070 // Determine if this category is also "non-lazy".
5071 if (ImplementationIsNonLazy(OCD))
5072 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005073}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005074
5075/// GetMethodConstant - Return a struct objc_method constant for the
5076/// given method if it has been defined. The result is null if the
5077/// method has not been defined. The return value has type MethodPtrTy.
5078llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079 const ObjCMethodDecl *MD) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005080 // FIXME: Use DenseMap::lookup
5081 llvm::Function *Fn = MethodDefinitions[MD];
5082 if (!Fn)
5083 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005084
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005085 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005086 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005087 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005089 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005090 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005091 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005092}
5093
5094/// EmitMethodList - Build meta-data for method declarations
5095/// struct _method_list_t {
5096/// uint32_t entsize; // sizeof(struct _objc_method)
5097/// uint32_t method_count;
5098/// struct _objc_method method_list[method_count];
5099/// }
5100///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005101llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5102 const char *Section,
5103 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005104 // Return null for empty list.
5105 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005106 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005107
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005108 std::vector<llvm::Constant*> Values(3);
5109 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005110 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005111 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005112 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005113 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005114 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005115 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005116 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005117 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005118
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005119 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005120 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005121 llvm::GlobalValue::InternalLinkage,
5122 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005123 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005124 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005125 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005126 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005127 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005128 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005129 ObjCTypes.MethodListnfABIPtrTy);
5130}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005131
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005132/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5133/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005134llvm::GlobalVariable *
5135CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5136 const ObjCIvarDecl *Ivar) {
5137 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005138 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005139 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005140 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005141 CGM.getModule().getGlobalVariable(Name);
5142 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005143 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005144 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005145 false,
5146 llvm::GlobalValue::ExternalLinkage,
5147 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005148 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005149 return IvarOffsetGV;
5150}
5151
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005152llvm::Constant *
5153CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5154 const ObjCIvarDecl *Ivar,
5155 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005156 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005157 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005158 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005159 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005160 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005161
Mike Stump18bb9282009-05-16 07:57:57 +00005162 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5163 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005164 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5165 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5166 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005167 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005168 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005169 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005170 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005171 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005172}
5173
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005174/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005175/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005176/// IvarListnfABIPtrTy.
5177/// struct _ivar_t {
5178/// unsigned long int *offset; // pointer to ivar offset location
5179/// char *name;
5180/// char *type;
5181/// uint32_t alignment;
5182/// uint32_t size;
5183/// }
5184/// struct _ivar_list_t {
5185/// uint32 entsize; // sizeof(struct _ivar_t)
5186/// uint32 count;
5187/// struct _iver_t list[count];
5188/// }
5189///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005190
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005191llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005192 const ObjCImplementationDecl *ID) {
5193
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005194 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005195
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005196 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5197 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005198
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005199 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005200
Daniel Dunbarae032262009-04-20 00:33:43 +00005201 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005202 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005203 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005204
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005205 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5206 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005207 // Ignore unnamed bit-fields.
5208 if (!IVD->getDeclName())
5209 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005210 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005211 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005212 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5213 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005214 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005215 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005216 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005217 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005218 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005219 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005220 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005221 // NOTE. Size of a bitfield does not match gcc's, because of the
5222 // way bitfields are treated special in each. But I am told that
5223 // 'size' for bitfield ivars is ignored by the runtime so it does
5224 // not matter. If it matters, there is enough info to get the
5225 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005226 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005227 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005228 }
5229 // Return null for empty list.
5230 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005231 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005232 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005233 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005234 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5235 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005236 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005237 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005238 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005239 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005240 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5241 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005242 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005243 llvm::GlobalValue::InternalLinkage,
5244 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005245 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005246 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005247 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005248 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005249
Chris Lattnerf56501c2009-07-17 23:57:13 +00005250 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005251 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005252}
5253
5254llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005255 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005256 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005257
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005258 if (!Entry) {
5259 // We use the initializer as a marker of whether this is a forward
5260 // reference or not. At module finalization we add the empty
5261 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005262 Entry =
5263 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5264 llvm::GlobalValue::ExternalLinkage,
5265 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005266 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005267 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005269
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005270 return Entry;
5271}
5272
5273/// GetOrEmitProtocol - Generate the protocol meta-data:
5274/// @code
5275/// struct _protocol_t {
5276/// id isa; // NULL
5277/// const char * const protocol_name;
5278/// const struct _protocol_list_t * protocol_list; // super protocols
5279/// const struct method_list_t * const instance_methods;
5280/// const struct method_list_t * const class_methods;
5281/// const struct method_list_t *optionalInstanceMethods;
5282/// const struct method_list_t *optionalClassMethods;
5283/// const struct _prop_list_t * properties;
5284/// const uint32_t size; // sizeof(struct _protocol_t)
5285/// const uint32_t flags; // = 0
5286/// }
5287/// @endcode
5288///
5289
5290llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005291 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005292 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005293
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005294 // Early exit if a defining object has already been generated.
5295 if (Entry && Entry->hasInitializer())
5296 return Entry;
5297
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005298 // Construct method lists.
5299 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5300 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005301 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005302 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005303 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005304 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005305 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5306 OptInstanceMethods.push_back(C);
5307 } else {
5308 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005309 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005310 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005311
5312 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005313 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005314 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005315 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005316 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5317 OptClassMethods.push_back(C);
5318 } else {
5319 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005320 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005322
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005323 std::vector<llvm::Constant*> Values(10);
5324 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005325 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005326 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005327 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5328 PD->protocol_begin(),
5329 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005330
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005331 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005332 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005333 "__DATA, __objc_const",
5334 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005335 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005336 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005337 "__DATA, __objc_const",
5338 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005339 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005340 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005341 "__DATA, __objc_const",
5342 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005343 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005344 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005345 "__DATA, __objc_const",
5346 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005347 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005348 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005349 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005350 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005351 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005352 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005353 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005354 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005355
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005356 if (Entry) {
5357 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005358 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005359 Entry->setInitializer(Init);
5360 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005361 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005362 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5363 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5364 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005365 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005366 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005367 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005368 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005369 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005370 CGM.AddUsedGlobal(Entry);
5371
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005372 // Use this protocol meta-data to build protocol list table in section
5373 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005374 llvm::GlobalVariable *PTGV =
5375 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5376 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5377 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005378 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005379 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005380 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005381 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005382 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 return Entry;
5384}
5385
5386/// EmitProtocolList - Generate protocol list meta-data:
5387/// @code
5388/// struct _protocol_list_t {
5389/// long protocol_count; // Note, this is 32/64 bit
5390/// struct _protocol_t[protocol_count];
5391/// }
5392/// @endcode
5393///
5394llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005395CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5396 ObjCProtocolDecl::protocol_iterator begin,
5397 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005398 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005399
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005401 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005402 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005404 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005405 llvm::SmallString<256> TmpName;
5406 Name.toVector(TmpName);
5407 llvm::GlobalVariable *GV =
5408 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005409 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005410 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005412 for (; begin != end; ++begin)
5413 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5414
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005415 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005416 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005417 ObjCTypes.ProtocolnfABIPtrTy));
5418
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005419 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005420 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005421 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005422 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005423 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005424 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005425 ProtocolRefs.size()),
5426 ProtocolRefs);
5427
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005428 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005429 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005430 llvm::GlobalValue::InternalLinkage,
5431 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005432 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005434 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005435 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005436 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005437 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005438 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005439}
5440
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005441/// GetMethodDescriptionConstant - This routine build following meta-data:
5442/// struct _objc_method {
5443/// SEL _cmd;
5444/// char *method_type;
5445/// char *_imp;
5446/// }
5447
5448llvm::Constant *
5449CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5450 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005451 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5453 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005454 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005455 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005456 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005457 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005458}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005459
5460/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5461/// This code gen. amounts to generating code for:
5462/// @code
5463/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5464/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005465///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005466LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005467 CodeGen::CodeGenFunction &CGF,
5468 QualType ObjectTy,
5469 llvm::Value *BaseValue,
5470 const ObjCIvarDecl *Ivar,
5471 unsigned CVRQualifiers) {
5472 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005473 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5474 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005475}
5476
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005477llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478 CodeGen::CodeGenFunction &CGF,
5479 const ObjCInterfaceDecl *Interface,
5480 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005481 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005482}
5483
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005484CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005485 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005486 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005487 QualType ResultType,
5488 Selector Sel,
5489 llvm::Value *Receiver,
5490 QualType Arg0Ty,
5491 bool IsSuper,
5492 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005493 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5494 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005495 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005496 llvm::Value *Arg0 = Receiver;
5497 if (!IsSuper)
5498 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005499
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005500 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005501 // FIXME. This is too much work to get the ABI-specific result type needed to
5502 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005503 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005504 = Types.getFunctionInfo(ResultType, CallArgList(),
5505 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005506 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005507 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005508 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005509#if 0
5510 // unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005511 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005512 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005513 // FIXME. Is there a better way of getting these names.
5514 // They are available in RuntimeFunctions vector pair.
5515 Name += "objc_msgSendId_stret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005516 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005517#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005518 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005519 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005520 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005521 } else {
5522 Fn = ObjCTypes.getMessageSendStretFixupFn();
5523 Name += "objc_msgSend_stret_fixup";
5524 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005525 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5526 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5527 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005528 } else {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005529#if 0
5530// unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005531 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005532 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005533 Name += "objc_msgSendId_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005534 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005535#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005536 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005537 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005538 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005539 } else {
5540 Fn = ObjCTypes.getMessageSendFixupFn();
5541 Name += "objc_msgSend_fixup";
5542 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005543 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005544 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005545 Name += '_';
5546 std::string SelName(Sel.getAsString());
5547 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005548 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005549 if (SelName[i] == ':')
5550 SelName[i] = '_';
5551 Name += SelName;
5552 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5553 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005554 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005555 std::vector<llvm::Constant*> Values(2);
5556 Values[0] = Fn;
5557 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005558 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005559 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005560 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005561 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005562 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005563 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005564 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005565 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005566 }
5567 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005568
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005569 CallArgList ActualArgs;
5570 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005571 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005572 ObjCTypes.MessageRefCPtrTy));
5573 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005574 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005575 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005576 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5577 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005578 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005579 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005580 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005581 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005582}
5583
5584/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005585CodeGen::RValue
5586CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005587 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005588 QualType ResultType,
5589 Selector Sel,
5590 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005591 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005592 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005593 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005594 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005595 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5596 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005597 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005598 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005599 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005600 Receiver, CGF.getContext().getObjCIdType(),
5601 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005602}
5603
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005604llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005605CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005606 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5607
Daniel Dunbara6468342009-03-02 05:18:14 +00005608 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005609 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005610 false, llvm::GlobalValue::ExternalLinkage,
5611 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005612 }
5613
5614 return GV;
5615}
5616
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005617llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5618 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005619 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005620
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005621 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005622 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005623 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005624 Entry =
5625 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005626 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005627 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005628 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005629 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005630 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005631 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005632 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005633 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005634 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005636 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005637}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005638
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005639llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005640CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005641 const ObjCInterfaceDecl *ID) {
5642 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005643
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005644 if (!Entry) {
5645 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5646 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005647 Entry =
5648 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005649 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005650 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005651 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005652 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005653 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005654 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005655 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005656 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005657 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005658
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005659 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005660}
5661
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005662/// EmitMetaClassRef - Return a Value * of the address of _class_t
5663/// meta-data
5664///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005665llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5666 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005667 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5668 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005669 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005670
Daniel Dunbar15894b72009-04-07 05:48:37 +00005671 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005672 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005673 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005674 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005675 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005676 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005677 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005678 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005679 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005680 ObjCTypes.ClassnfABIPtrTy));
5681
Daniel Dunbare60aa052009-04-15 19:03:14 +00005682 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005683 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005684
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005685 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005686}
5687
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005688/// GetClass - Return a reference to the class for the given interface
5689/// decl.
5690llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5691 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005692 if (ID->hasAttr<WeakImportAttr>()) {
5693 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5694 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5695 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5696 }
5697
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005698 return EmitClassRef(Builder, ID);
5699}
5700
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005701/// Generates a message send where the super is the receiver. This is
5702/// a message send to self with special delivery semantics indicating
5703/// which class's method should be called.
5704CodeGen::RValue
5705CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005706 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005707 QualType ResultType,
5708 Selector Sel,
5709 const ObjCInterfaceDecl *Class,
5710 bool isCategoryImpl,
5711 llvm::Value *Receiver,
5712 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005713 const CodeGen::CallArgList &CallArgs,
5714 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005715 // ...
5716 // Create and init a super structure; this is a (receiver, class)
5717 // pair we will pass to objc_msgSendSuper.
5718 llvm::Value *ObjCSuper =
5719 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005720
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005721 llvm::Value *ReceiverAsObject =
5722 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5723 CGF.Builder.CreateStore(ReceiverAsObject,
5724 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005725
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005726 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005727 llvm::Value *Target;
5728 if (IsClassMessage) {
5729 if (isCategoryImpl) {
5730 // Message sent to "super' in a class method defined in
5731 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005732 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005733 Target = CGF.Builder.CreateStructGEP(Target, 0);
5734 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005735 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005736 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005737 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005738 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005739
Mike Stump18bb9282009-05-16 07:57:57 +00005740 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5741 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005742 const llvm::Type *ClassTy =
5743 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5744 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5745 CGF.Builder.CreateStore(Target,
5746 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005747
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005748 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005749 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5750 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005751 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005752 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005753 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005754 ObjCSuper, ObjCTypes.SuperPtrCTy,
5755 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005756}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005757
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005758llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005759 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005760 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005762 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005763 llvm::Constant *Casted =
5764 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5765 ObjCTypes.SelectorPtrTy);
5766 Entry =
5767 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5768 llvm::GlobalValue::InternalLinkage,
5769 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005770 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005771 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005772 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005773
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005774 if (lval)
5775 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005776 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005777}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005778/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005779/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005780///
5781void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005782 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005783 llvm::Value *dst,
5784 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005785 const llvm::Type * SrcTy = src->getType();
5786 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005787 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005788 assert(Size <= 8 && "does not support size > 8");
5789 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5790 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005791 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5792 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005793 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5794 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005795 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5796 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005797 return;
5798}
5799
5800/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5801/// objc_assign_strongCast (id src, id *dst)
5802///
5803void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005804 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005805 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005806 const llvm::Type * SrcTy = src->getType();
5807 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005808 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005809 assert(Size <= 8 && "does not support size > 8");
5810 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005811 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005812 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5813 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005814 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5815 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005816 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005817 src, dst, "weakassign");
5818 return;
5819}
5820
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005821void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005822 CodeGen::CodeGenFunction &CGF,
5823 llvm::Value *DestPtr,
5824 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005825 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005826 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5827 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005828 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005829 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005830 return;
5831}
5832
Fariborz Jahanian06292952009-02-16 22:52:32 +00005833/// EmitObjCWeakRead - Code gen for loading value of a __weak
5834/// object: objc_read_weak (id *src)
5835///
5836llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005837 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005838 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005839 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005840 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5841 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005842 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005843 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005844 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005845 return read_weak;
5846}
5847
5848/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5849/// objc_assign_weak (id src, id *dst)
5850///
5851void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005852 llvm::Value *src, llvm::Value *dst) {
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);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005863 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005864 src, dst, "weakassign");
5865 return;
5866}
5867
5868/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5869/// objc_assign_global (id src, id *dst)
5870///
5871void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005872 llvm::Value *src, llvm::Value *dst,
5873 bool threadlocal) {
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)
5879 : 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);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005884 if (!threadlocal)
5885 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5886 src, dst, "globalassign");
5887 else
5888 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5889 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005890 return;
5891}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005892
John McCallc20acd32010-07-21 00:41:47 +00005893namespace {
John McCallcda666c2010-07-21 07:22:38 +00005894 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00005895 llvm::Value *SyncExitFn;
5896 llvm::Value *SyncArg;
5897 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5898 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5899
5900 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5901 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5902 }
5903 };
5904}
5905
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005906void
John McCallbd309292010-07-06 01:34:17 +00005907CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5908 const ObjCAtSynchronizedStmt &S) {
5909 // Evaluate the lock operand. This should dominate the cleanup.
5910 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005911
John McCallbd309292010-07-06 01:34:17 +00005912 // Acquire the lock.
5913 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5914 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5915 ->setDoesNotThrow();
5916
5917 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00005918 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5919 ObjCTypes.getSyncExitFn(),
5920 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005921
John McCallbd309292010-07-06 01:34:17 +00005922 // Emit the body of the statement.
5923 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005924
John McCallbd309292010-07-06 01:34:17 +00005925 // Pop the lock-release cleanup.
5926 CGF.PopCleanupBlock();
5927}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005928
John McCallbd309292010-07-06 01:34:17 +00005929namespace {
5930 struct CatchHandler {
5931 const VarDecl *Variable;
5932 const Stmt *Body;
5933 llvm::BasicBlock *Block;
5934 llvm::Value *TypeInfo;
5935 };
John McCall5c08ab92010-07-13 22:12:14 +00005936
John McCallcda666c2010-07-21 07:22:38 +00005937 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00005938 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5939 MightThrow(MightThrow), Fn(Fn) {}
5940 bool MightThrow;
5941 llvm::Value *Fn;
5942
5943 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5944 if (!MightThrow) {
5945 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5946 return;
5947 }
5948
5949 CGF.EmitCallOrInvoke(Fn, 0, 0);
5950 }
5951 };
John McCallbd309292010-07-06 01:34:17 +00005952}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005953
John McCall2ca705e2010-07-24 00:37:23 +00005954llvm::Constant *
5955CGObjCNonFragileABIMac::GetEHType(QualType T) {
5956 // There's a particular fixed type info for 'id'.
5957 if (T->isObjCIdType() ||
5958 T->isObjCQualifiedIdType()) {
5959 llvm::Constant *IDEHType =
5960 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5961 if (!IDEHType)
5962 IDEHType =
5963 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5964 false,
5965 llvm::GlobalValue::ExternalLinkage,
5966 0, "OBJC_EHTYPE_id");
5967 return IDEHType;
5968 }
5969
5970 // All other types should be Objective-C interface pointer types.
5971 const ObjCObjectPointerType *PT =
5972 T->getAs<ObjCObjectPointerType>();
5973 assert(PT && "Invalid @catch type.");
5974 const ObjCInterfaceType *IT = PT->getInterfaceType();
5975 assert(IT && "Invalid @catch type.");
5976 return GetInterfaceEHType(IT->getDecl(), false);
5977}
5978
John McCallbd309292010-07-06 01:34:17 +00005979void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5980 const ObjCAtTryStmt &S) {
5981 // Jump destination for falling out of catch bodies.
5982 CodeGenFunction::JumpDest Cont;
5983 if (S.getNumCatchStmts())
5984 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005985
John McCallbd309292010-07-06 01:34:17 +00005986 CodeGenFunction::FinallyInfo FinallyInfo;
5987 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
5988 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
5989 ObjCTypes.getObjCBeginCatchFn(),
5990 ObjCTypes.getObjCEndCatchFn(),
5991 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005992
John McCallbd309292010-07-06 01:34:17 +00005993 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005994
John McCallbd309292010-07-06 01:34:17 +00005995 // Enter the catch, if there is one.
5996 if (S.getNumCatchStmts()) {
5997 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
5998 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00005999 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006000
John McCallbd309292010-07-06 01:34:17 +00006001 Handlers.push_back(CatchHandler());
6002 CatchHandler &Handler = Handlers.back();
6003 Handler.Variable = CatchDecl;
6004 Handler.Body = CatchStmt->getCatchBody();
6005 Handler.Block = CGF.createBasicBlock("catch");
6006
6007 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006008 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00006009 Handler.TypeInfo = 0; // catch-all
6010 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00006011 break;
6012 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006013
John McCall2ca705e2010-07-24 00:37:23 +00006014 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006015 }
John McCallbd309292010-07-06 01:34:17 +00006016
6017 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6018 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6019 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006020 }
John McCallbd309292010-07-06 01:34:17 +00006021
6022 // Emit the try body.
6023 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006024
John McCallbd309292010-07-06 01:34:17 +00006025 // Leave the try.
6026 if (S.getNumCatchStmts())
6027 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006028
John McCallbd309292010-07-06 01:34:17 +00006029 // Remember where we were.
6030 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006031
John McCallbd309292010-07-06 01:34:17 +00006032 // Emit the handlers.
6033 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6034 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006035
John McCallbd309292010-07-06 01:34:17 +00006036 CGF.EmitBlock(Handler.Block);
6037 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006038
John McCallbd309292010-07-06 01:34:17 +00006039 // Enter the catch.
6040 llvm::CallInst *Exn =
6041 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6042 "exn.adjusted");
6043 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006044
John McCallbd309292010-07-06 01:34:17 +00006045 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00006046 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00006047 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6048 EndCatchMightThrow,
6049 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006050
John McCallbd309292010-07-06 01:34:17 +00006051 // Bind the catch parameter if it exists.
6052 if (const VarDecl *CatchParam = Handler.Variable) {
6053 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6054 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006055
John McCallbd309292010-07-06 01:34:17 +00006056 CGF.EmitLocalBlockVarDecl(*CatchParam);
6057 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006058 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006059
John McCallbd309292010-07-06 01:34:17 +00006060 CGF.ObjCEHValueStack.push_back(Exn);
6061 CGF.EmitStmt(Handler.Body);
6062 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006063
John McCallbd309292010-07-06 01:34:17 +00006064 // Leave the earlier cleanup.
6065 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006066
John McCallbd309292010-07-06 01:34:17 +00006067 CGF.EmitBranchThroughCleanup(Cont);
6068 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006069
John McCallbd309292010-07-06 01:34:17 +00006070 // Go back to the try-statement fallthrough.
6071 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006072
John McCallbd309292010-07-06 01:34:17 +00006073 // Pop out of the normal cleanup on the finally.
6074 if (S.getFinallyStmt())
6075 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006076
John McCallad5d61e2010-07-23 21:56:41 +00006077 if (Cont.isValid())
6078 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006079}
6080
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006081/// EmitThrowStmt - Generate code for a throw statement.
6082void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6083 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006084 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006085 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006086 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006087 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006088 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006089 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006090 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006091 "Unexpected rethrow outside @catch block.");
6092 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006093 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006094 }
6095
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006096 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006097 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6098 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6099 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00006100 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00006101 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006102 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00006103 } else {
6104 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6105 ->setDoesNotReturn();
6106 CGF.Builder.CreateUnreachable();
6107 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00006108
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006109 // Clear the insertion point to indicate we are in unreachable code.
6110 CGF.Builder.ClearInsertionPoint();
6111}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006112
John McCall2ca705e2010-07-24 00:37:23 +00006113llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006114CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006115 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006116 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006117
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006118 // If we don't need a definition, return the entry if found or check
6119 // if we use an external reference.
6120 if (!ForDefinition) {
6121 if (Entry)
6122 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006123
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006124 // If this type (or a super class) has the __objc_exception__
6125 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006126 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006127 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006128 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006129 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006130 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006131 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006132 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006133 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006134
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006135 // Otherwise we need to either make a new entry or fill in the
6136 // initializer.
6137 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006138 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006139 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006140 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006141 CGM.getModule().getGlobalVariable(VTableName);
6142 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006143 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6144 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006145 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006146 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006147
Chris Lattner5e016ae2010-06-27 07:15:29 +00006148 llvm::Value *VTableIdx =
6149 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006150
6151 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006152 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006153 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006154 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006155 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006156 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006157
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006158 if (Entry) {
6159 Entry->setInitializer(Init);
6160 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006161 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006162 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006163 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006164 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006165 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006166 }
6167
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006168 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006169 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006170 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6171 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006172
6173 if (ForDefinition) {
6174 Entry->setSection("__DATA,__objc_const");
6175 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6176 } else {
6177 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6178 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006179
6180 return Entry;
6181}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006182
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006183/* *** */
6184
Daniel Dunbarb036db82008-08-13 03:21:16 +00006185CodeGen::CGObjCRuntime *
6186CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006187 return new CGObjCMac(CGM);
6188}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006189
6190CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006191CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006192 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006193}