blob: fea98ba720c6517e225abb96b5b3de792a89c8b5 [file] [log] [blame]
Daniel Dunbarc17a4d32008-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 Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallf1549f62010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000027
John McCall87bb5822010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000043
Daniel Dunbar97776872009-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 Dunbar1d7e5392009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000053
Daniel Dunbar61ac1d22010-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 Dunbar3a2c80f2010-04-02 15:43:29 +000057
Daniel Dunbar532d4da2009-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 Dunbar6bff2512009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-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 Dunbare83be122010-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 Dunbar532d4da2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-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 Dunbar97776872009-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 Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000109
John McCall0953e762009-09-24 19:53:00 +0000110 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
111 Quals.addCVRQualifiers(CVRQualifiers);
112
Daniel Dunbar56229f52010-04-05 16:20:33 +0000113 if (!Ivar->isBitField())
114 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000115
Daniel Dunbar56229f52010-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 Dunbar97776872009-04-22 07:32:20 +0000123
Daniel Dunbarf0fe5bc2010-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 Dunbarf0fe5bc2010-04-05 21:36:35 +0000130
Daniel Dunbarab970f92010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-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 Dunbar2df25692010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbar56229f52010-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 Dunbarefbf4872010-04-06 01:07:44 +0000147 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000148}
149
150///
151
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000152namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000153
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000154typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000155
Daniel Dunbar6bff2512009-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 Dunbar6efc0c52008-08-13 03:21:16 +0000158
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000159class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000160protected:
161 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000170 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
171 Params, true),
172 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000173 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174
Fariborz Jahaniand0f8a8d2009-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 Anderson0032b272009-08-13 21:57:51 +0000181 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182 Params, true),
183 "objc_msgSend_stret");
184
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000186
Fariborz Jahaniand0f8a8d2009-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 Anderson0032b272009-08-13 21:57:51 +0000194 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
195 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000196 Params,
197 true),
198 "objc_msgSend_fpret");
199
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000201
Fariborz Jahaniand0f8a8d2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000209 Params, true),
210 SuperName);
211 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Fariborz Jahaniand0f8a8d2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000219 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000220 Params, true),
221 SuperName);
222 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000223
Fariborz Jahaniand0f8a8d2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000231 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000232 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000233 Params, true),
234 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000235 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000236
Fariborz Jahaniand0f8a8d2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000244 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000245 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000246 Params, true),
247 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000248 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000249
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000254
Fariborz Jahaniand0f8a8d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000259
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260protected:
261 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000262
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000263public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000264 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000265 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000267 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
268 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000270 /// PtrObjectPtrTy - LLVM type for id *
271 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000273 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000274 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000275 /// ProtocolPtrTy - LLVM type for external protocol handles
276 /// (typeof(Protocol))
277 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000278
Daniel Dunbar19cd87e2008-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 Dunbar6bff2512009-08-03 17:06:42 +0000283
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000284 /// SuperTy - LLVM type for struct objc_super.
285 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000286 /// SuperPtrTy - LLVM type for struct objc_super *.
287 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000288
Fariborz Jahanian30bc5712009-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 Dunbar6bff2512009-08-03 17:06:42 +0000292
Fariborz Jahanian30bc5712009-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 Dunbar6bff2512009-08-03 17:06:42 +0000298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000299 // MethodTy - LLVM type for struct objc_method.
300 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000301
Fariborz Jahaniand55b6fc2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000306
Chris Lattner72db6c32009-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 McCallead608a2010-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 Lattner72db6c32009-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 McCall04a67a62010-02-05 21:31:56 +0000319 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000320 FunctionType::ExtInfo()),
321 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000324
Chris Lattner72db6c32009-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 McCallead608a2010-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 Lattner72db6c32009-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 McCall04a67a62010-02-05 21:31:56 +0000339 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000340 FunctionType::ExtInfo()),
341 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000344
Fariborz Jahanian6cc59062010-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 Lattner72db6c32009-04-22 02:44:54 +0000363 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
365 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000366 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000367 llvm::SmallVector<CanQualType,1> Params;
368 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000369 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000370 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000371 FunctionType::ExtInfo()),
372 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
374 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000375
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000376 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-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 Dunbar6bff2512009-08-03 17:06:42 +0000381 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000382 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000384 }
385
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000386 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-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 Anderson96e0fc72009-07-29 22:16:19 +0000392 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000395
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000396 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-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 Andersona1cf15f2009-07-14 23:10:40 +0000401 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000402 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000403 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000405
Fariborz Jahanian021a7a62010-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 Jahaniandb286862009-01-22 00:37:21 +0000416 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000418 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000419 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
420 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000421 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000422 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000423 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
425 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000426
Fariborz Jahanian082b02e2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
435 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000436
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000437 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000438 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000439 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000440 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
441 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000443 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
445 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000446
447 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-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 Anderson0032b272009-08-13 21:57:51 +0000452 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000453 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000455
Fariborz Jahanian69677ea2010-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 Dunbar1c566672009-02-24 01:43:46 +0000465 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-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 Anderson0032b272009-08-13 21:57:51 +0000470 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000471 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000473
Daniel Dunbar1c566672009-02-24 01:43:46 +0000474 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-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 Anderson0032b272009-08-13 21:57:51 +0000479 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000480 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
481 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000482
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000486
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
489 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000490
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendStretFn(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
493 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000494
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn2(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
497 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000498
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendFpretFn(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
501 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
505 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000506
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000507 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
508 ~ObjCCommonTypesHelper(){}
509};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000510
Fariborz Jahanianee0af742009-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 Jahanianee0af742009-01-21 22:04:16 +0000514public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000515 /// SymtabTy - LLVM type for struct objc_symtab.
516 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000517 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
518 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000519 /// ModuleTy - LLVM type for struct objc_module.
520 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000521
Daniel Dunbar6efc0c52008-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 Dunbar6efc0c52008-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 Dunbar86e253a2008-08-22 20:34:54 +0000545 /// CategoryTy - LLVM type for struct objc_category.
546 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar6bff2512009-08-03 17:06:42 +0000565
Anders Carlsson124526b2008-09-09 10:10:21 +0000566 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
567 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Anders Carlsson124526b2008-09-09 10:10:21 +0000569 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionTryEnterFn() {
571 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000572 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000573 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000574 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575 Params, false),
576 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000578
579 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000580 llvm::Constant *getExceptionTryExitFn() {
581 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000582 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000583 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000584 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585 Params, false),
586 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000588
589 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000590 llvm::Constant *getExceptionExtractFn() {
591 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000592 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000594 Params, false),
595 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000596
Chris Lattner34b02a12009-04-22 02:26:14 +0000597 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Anders Carlsson124526b2008-09-09 10:10:21 +0000599 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-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 Dunbar6bff2512009-08-03 17:06:42 +0000604 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000605 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606 Params, false),
607 "objc_exception_match");
608
Chris Lattner34b02a12009-04-22 02:26:14 +0000609 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610
Anders Carlsson124526b2008-09-09 10:10:21 +0000611 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000612 llvm::Constant *getSetJmpFn() {
613 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000614 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000615 return
Owen Anderson0032b272009-08-13 21:57:51 +0000616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000617 Params, false),
618 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Chris Lattner34b02a12009-04-22 02:26:14 +0000620 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000622public:
623 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000624 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000625};
626
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000627/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000628/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000629class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000630public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // MethodListnfABITy - LLVM for struct _method_list_t
633 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
636 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // ProtocolnfABITy = LLVM for struct _protocol_t
639 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Daniel Dunbar948e2582009-02-15 07:36:20 +0000641 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
642 const llvm::Type *ProtocolnfABIPtrTy;
643
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000644 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
645 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000647 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
648 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000650 // ClassnfABITy - LLVM for struct _class_t
651 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000652
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000653 // ClassnfABIPtrTy - LLVM for struct _class_t*
654 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000656 // IvarnfABITy - LLVM for struct _ivar_t
657 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000659 // IvarListnfABITy - LLVM for struct _ivar_list_t
660 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000662 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
663 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000665 // ClassRonfABITy - LLVM for struct _class_ro_t
666 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000668 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
669 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000671 // CategorynfABITy - LLVM for struct _category_t
672 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000673
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000674 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000675
Fariborz Jahanian2e4672b2009-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 Jahanian83a8a752009-02-04 20:42:28 +0000682 // MessageRefCTy - clang type for struct _message_ref_t
683 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000685 // MessageRefPtrTy - LLVM for struct _message_ref_t*
686 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000687 // MessageRefCPtrTy - clang type for struct _message_ref_t*
688 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Fariborz Jahanianef163782009-02-05 01:13:09 +0000690 // MessengerTy - Type of the messenger (shown as IMP above)
691 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000692
Fariborz Jahanian2e4672b2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000699
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000700 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
701 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSend_fixup");
711 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000719 Params, true),
720 "objc_msgSend_fpret_fixup");
721 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000722
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000728 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000729 Params, true),
730 "objc_msgSend_stret_fixup");
731 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000738 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000739 Params, true),
740 "objc_msgSendId_fixup");
741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000748 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000749 Params, true),
750 "objc_msgSendId_stret_fixup");
751 }
752 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000758 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000759 Params, true),
760 "objc_msgSendSuper2_fixup");
761 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Chris Lattner1c02f862009-04-22 02:53:24 +0000763 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-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 Anderson96e0fc72009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000770 Params, true),
771 "objc_msgSendSuper2_stret_fixup");
772 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773
774
775
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000776 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
777 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000778 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000780 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000781 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000783 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000784 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000785
Chris Lattner8a569112009-04-22 02:15:23 +0000786 llvm::Constant *getUnwindResumeOrRethrowFn() {
787 std::vector<const llvm::Type*> Params;
788 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000789 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000790 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000792 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
793 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000794 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Chris Lattner8a569112009-04-22 02:15:23 +0000796 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000797 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000798 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000799 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000800
Chris Lattner8a569112009-04-22 02:15:23 +0000801 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Chris Lattner8a569112009-04-22 02:15:23 +0000803 llvm::Constant *getObjCBeginCatchFn() {
804 std::vector<const llvm::Type*> Params;
805 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000806 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000807 Params, false),
808 "objc_begin_catch");
809 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000810
811 const llvm::StructType *EHTypeTy;
812 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000813
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000814 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
815 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000816};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000817
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000818class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000819public:
820 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000821 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000822 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000823 unsigned ivar_bytepos;
824 unsigned ivar_size;
825 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000826 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-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 Jahaniana5a10c32009-03-10 16:22:08 +0000832 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000833
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000834 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000835 public:
836 unsigned skip;
837 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000839 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000840 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000842protected:
843 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000844 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000845 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000846 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Fariborz Jahanian9397e1d2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000852 /// LazySymbols - Symbols to generate a lazy reference for. See
853 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000854 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000855
Daniel Dunbar242d4dc2008-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 Dunbar33063492009-09-07 00:20:42 +0000860 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000861
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000862 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000863 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000864
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000865 /// MethodVarNames - uniqued method variable names.
866 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000867
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000868 /// DefinedCategoryNames - list of category names in form Class_Category.
869 llvm::SetVector<std::string> DefinedCategoryNames;
870
Daniel Dunbar6efc0c52008-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 Dunbar6bff2512009-08-03 17:06:42 +0000874
Daniel Dunbarc45ef602008-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 Dunbar6bff2512009-08-03 17:06:42 +0000878
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000879 /// PropertyNames - uniqued method variable names.
880 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 /// ClassReferences - uniqued class references.
883 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000885 /// SelectorReferences - uniqued selector references.
886 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Daniel Dunbar6efc0c52008-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 Dunbar6bff2512009-08-03 17:06:42 +0000892
Daniel Dunbar0c0e7a62008-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 Dunbar6bff2512009-08-03 17:06:42 +0000896
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000897 /// DefinedClasses - List of defined classes.
898 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000899
900 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
901 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000903 /// DefinedCategories - List of defined categories.
904 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000905
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000906 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
907 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Fariborz Jahanian56210f72009-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 Dunbarc575ce72009-10-19 01:21:19 +0000913 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914
Fariborz Jahanian56210f72009-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 Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanian56210f72009-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 Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000924 // FIXME: This is a horrible name.
925 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000926 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927
Fariborz Jahanian56210f72009-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 Dunbar6bff2512009-08-03 17:06:42 +0000931
Fariborz Jahanian56210f72009-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 Dunbar6bff2512009-08-03 17:06:42 +0000935
Fariborz Jahanian058a1b72009-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 Dunbar6bff2512009-08-03 17:06:42 +0000939
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000940 /// BuildIvarLayout - Builds ivar layout bitmap for the class
941 /// implementation for the __strong or __weak case.
942 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000943 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
944 bool ForStrongLayout);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000945
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000946 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000947 unsigned int BytePos, bool ForStrongLayout,
948 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000949 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000950 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000951 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000952 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000953 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000954 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000955
Fariborz Jahaniand80d81b2009-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 Dunbar6bff2512009-08-03 17:06:42 +0000960
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000961 /// EmitPropertyList - Emit the given property list. The return
962 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000963 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000964 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000965 const ObjCContainerDecl *OCD,
966 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000967
Fariborz Jahanian191dcd72009-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 Jahanianda320092009-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 Jahanianb21f07e2009-03-08 20:18:37 +0000979
Daniel Dunbarfd65d372009-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 Lattnerad64e022009-07-17 23:57:13 +0000985 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-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 Dunbarc1583062009-04-14 17:42:51 +0000993 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000994 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000995 llvm::Constant *Init,
996 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000997 unsigned Align,
998 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000999
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001000 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001001 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-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 Dunbard6c93d72009-09-17 04:01:22 +00001008 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001009 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001010
Daniel Dunbarfce176b2010-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 Jahanianee0af742009-01-21 22:04:16 +00001015public:
Owen Anderson69243822009-07-13 04:10:07 +00001016 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001017 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001018
David Chisnall0d13f6f2010-01-23 02:40:42 +00001019 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001020
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001021 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1022 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001023
Fariborz Jahanianda320092009-01-29 19:24:30 +00001024 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001025
Fariborz Jahanianda320092009-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 Dunbar6bff2512009-08-03 17:06:42 +00001030
Fariborz Jahanianda320092009-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 Jahanian89ecd412010-08-04 16:57:49 +00001036 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1037 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1038
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001039};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001040
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001041class CGObjCMac : public CGObjCCommonMac {
1042private:
1043 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001044
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001045 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001046 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001047 void EmitModuleInfo();
1048
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001049 /// EmitModuleSymols - Emit module symbols, the list of defined
1050 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001051 llvm::Constant *EmitModuleSymbols();
1052
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001053 /// FinishModule - Write out global data structures at the end of
1054 /// processing a translation unit.
1055 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001057 /// EmitClassExtension - Generate the class extension structure used
1058 /// to store the weak ivar layout and properties. The return value
1059 /// has type ClassExtensionPtrTy.
1060 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1061
1062 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1063 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001064 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001066
1067 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1068 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001069
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001070 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001071 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001072 QualType ResultType,
1073 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001074 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001075 QualType Arg0Ty,
1076 bool IsSuper,
1077 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001078
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001079 /// EmitIvarList - Emit the ivar list for the given
1080 /// implementation. If ForClass is true the list of class ivars
1081 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1082 /// interface ivars will be emitted. The return value has type
1083 /// IvarListPtrTy.
1084 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001085 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001086
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001087 /// EmitMetaClass - Emit a forward reference to the class structure
1088 /// for the metaclass of the given interface. The return value has
1089 /// type ClassPtrTy.
1090 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1091
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001092 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001093 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001094 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1095 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001096 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001097
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001098 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001099
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001101
1102 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001103 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001104 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001105 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001106 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001107
1108 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001109 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001110 /// - TypeName: The name for the type containing the methods.
1111 /// - IsProtocol: True iff these methods are for a protocol.
1112 /// - ClassMethds: True iff these are class methods.
1113 /// - Required: When true, only "required" methods are
1114 /// listed. Similarly, when false only "optional" methods are
1115 /// listed. For classes this should always be true.
1116 /// - begin, end: The method list to output.
1117 ///
1118 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001119 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001120 const char *Section,
1121 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001122
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001123 /// GetOrEmitProtocol - Get the protocol object for the given
1124 /// declaration, emitting it if necessary. The return value has type
1125 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001126 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001127
1128 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1129 /// object for the given declaration, emitting it if needed. These
1130 /// forward references will be filled in with empty bodies if no
1131 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001132 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001133
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134 /// EmitProtocolExtension - Generate the protocol extension
1135 /// structure used to store optional instance and class methods, and
1136 /// protocol properties. The return value has type
1137 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001138 llvm::Constant *
1139 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1140 const ConstantVector &OptInstanceMethods,
1141 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001142
1143 /// EmitProtocolList - Generate the list of referenced
1144 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001145 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001146 ObjCProtocolDecl::protocol_iterator begin,
1147 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001148
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001149 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1150 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001151 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1152 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001153
1154public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001155 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001156
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001157 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001158
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001159 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001160 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001161 QualType ResultType,
1162 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001163 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001164 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001165 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001166 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001167
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001168 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001169 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001170 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001171 QualType ResultType,
1172 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001173 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001174 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001175 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001176 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001177 const CallArgList &CallArgs,
1178 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001179
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001180 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001181 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001182
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001183 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1184 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001185
1186 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1187 /// untyped one.
1188 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1189 const ObjCMethodDecl *Method);
1190
John McCall5a180392010-07-24 00:37:23 +00001191 virtual llvm::Constant *GetEHType(QualType T);
1192
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001193 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001194
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001195 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001196
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001197 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001198 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001199
Chris Lattner74391b42009-03-22 21:03:39 +00001200 virtual llvm::Constant *GetPropertyGetFunction();
1201 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001202 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001203 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001204
John McCallf1549f62010-07-06 01:34:17 +00001205 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1206 const ObjCAtTryStmt &S);
1207 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1208 const ObjCAtSynchronizedStmt &S);
1209 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001210 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1211 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001212 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001213 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001214 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001215 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001216 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001217 llvm::Value *src, llvm::Value *dest,
1218 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001219 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001220 llvm::Value *src, llvm::Value *dest,
1221 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001222 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1223 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001224 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1225 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001226 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001227
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001228 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1229 QualType ObjectTy,
1230 llvm::Value *BaseValue,
1231 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001232 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001233 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001234 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001235 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001236};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001237
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001238class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001239private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001240 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001241 llvm::GlobalVariable* ObjCEmptyCacheVar;
1242 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001243
Daniel Dunbar11394522009-04-18 08:51:00 +00001244 /// SuperClassReferences - uniqued super class references.
1245 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001247 /// MetaClassReferences - uniqued meta class references.
1248 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001249
1250 /// EHTypeReferences - uniqued class ehtype references.
1251 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001252
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001253 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001254 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001255 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001257 /// DefinedMetaClasses - List of defined meta-classes.
1258 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1259
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001260 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001261 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001262 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001263
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001264 /// FinishNonFragileABIModule - Write out global data structures at the end of
1265 /// processing a translation unit.
1266 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001267
Daniel Dunbar463b8762009-05-15 21:48:48 +00001268 /// AddModuleClassList - Add the given list of class pointers to the
1269 /// module with the provided symbol and section names.
1270 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1271 const char *SymbolName,
1272 const char *SectionName);
1273
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1275 unsigned InstanceStart,
1276 unsigned InstanceSize,
1277 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001278 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001280 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001281 llvm::Constant *ClassRoGV,
1282 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001284 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001285
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001286 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001288 /// EmitMethodList - Emit the method list for the given
1289 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001290 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001291 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001292 const ConstantVector &Methods);
1293 /// EmitIvarList - Emit the ivar list for the given
1294 /// implementation. If ForClass is true the list of class ivars
1295 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1296 /// interface ivars will be emitted. The return value has type
1297 /// IvarListnfABIPtrTy.
1298 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001300 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001301 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001302 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001303
Fariborz Jahanianda320092009-01-29 19:24:30 +00001304 /// GetOrEmitProtocol - Get the protocol object for the given
1305 /// declaration, emitting it if necessary. The return value has type
1306 /// ProtocolPtrTy.
1307 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308
Fariborz Jahanianda320092009-01-29 19:24:30 +00001309 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1310 /// object for the given declaration, emitting it if needed. These
1311 /// forward references will be filled in with empty bodies if no
1312 /// definition is seen. The return value has type ProtocolPtrTy.
1313 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314
Fariborz Jahanianda320092009-01-29 19:24:30 +00001315 /// EmitProtocolList - Generate the list of referenced
1316 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001317 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001318 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001319 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001320
Fariborz Jahanian46551122009-02-04 00:22:57 +00001321 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001322 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001323 QualType ResultType,
1324 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001325 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001326 QualType Arg0Ty,
1327 bool IsSuper,
1328 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001329
1330 /// GetClassGlobal - Return the global variable for the Objective-C
1331 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001332 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001333
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001334 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001335 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001336 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001337 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001338
Daniel Dunbar11394522009-04-18 08:51:00 +00001339 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1340 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001341 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1342 const ObjCInterfaceDecl *ID);
1343
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001344 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1345 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001346 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001347 const ObjCInterfaceDecl *ID);
1348
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001349 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1350 /// the given ivar.
1351 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001352 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001353 const ObjCInterfaceDecl *ID,
1354 const ObjCIvarDecl *Ivar);
1355
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001356 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1357 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001358 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1359 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001360
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001361 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001362 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001363 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001364 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001365
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001366 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001367 return "OBJC_METACLASS_$_";
1368 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001369
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001370 const char *getClassSymbolPrefix() const {
1371 return "OBJC_CLASS_$_";
1372 }
1373
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001374 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001375 uint32_t &InstanceStart,
1376 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001377
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001378 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001379 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001380 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1381 return CGM.getContext().Selectors.getSelector(0, &II);
1382 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001383
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001384 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001385 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1386 return CGM.getContext().Selectors.getSelector(1, &II);
1387 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001388
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001389 /// ImplementationIsNonLazy - Check whether the given category or
1390 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001391 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001392
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001393public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001394 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001395 // FIXME. All stubs for now!
1396 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001398 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001399 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001400 QualType ResultType,
1401 Selector Sel,
1402 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001403 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001404 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001405 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001406
1407 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001408 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001409 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001410 QualType ResultType,
1411 Selector Sel,
1412 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001413 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001414 llvm::Value *Receiver,
1415 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001416 const CallArgList &CallArgs,
1417 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001418
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001419 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001420 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001421
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001422 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1423 bool lvalue = false)
1424 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001425
1426 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1427 /// untyped one.
1428 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1429 const ObjCMethodDecl *Method)
1430 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001431
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001432 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001433
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001434 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001435 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001436 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001437
John McCall5a180392010-07-24 00:37:23 +00001438 virtual llvm::Constant *GetEHType(QualType T);
1439
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001440 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001441 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001443 virtual llvm::Constant *GetPropertySetFunction() {
1444 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001445 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001446
1447 virtual llvm::Constant *GetCopyStructFunction() {
1448 return ObjCTypes.getCopyStructFn();
1449 }
1450
Chris Lattner74391b42009-03-22 21:03:39 +00001451 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001452 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001453 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001454
John McCallf1549f62010-07-06 01:34:17 +00001455 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1456 const ObjCAtTryStmt &S);
1457 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1458 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001459 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001460 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001461 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001462 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001463 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001464 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001465 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001466 llvm::Value *src, llvm::Value *dest,
1467 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001468 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001469 llvm::Value *src, llvm::Value *dest,
1470 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001471 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001472 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001473 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1474 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001475 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001476 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1477 QualType ObjectTy,
1478 llvm::Value *BaseValue,
1479 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001480 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001481 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001482 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001483 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001484};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001485
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001486} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001487
1488/* *** Helper Functions *** */
1489
1490/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001491static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001492 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001493 unsigned idx0,
1494 unsigned idx1) {
1495 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001496 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1497 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001498 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001499 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001500}
1501
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001502/// hasObjCExceptionAttribute - Return true if this class or any super
1503/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001504static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001505 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001506 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001507 return true;
1508 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001509 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001510 return false;
1511}
1512
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001513/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001514
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001515CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001516 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001517 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001518 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001519}
1520
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001521/// GetClass - Return a reference to the class for the given interface
1522/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001523llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001524 const ObjCInterfaceDecl *ID) {
1525 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001526}
1527
1528/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001529llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1530 bool lval) {
1531 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001532}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001533llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001534 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001535 return EmitSelector(Builder, Method->getSelector());
1536}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001537
John McCall5a180392010-07-24 00:37:23 +00001538llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1539 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1540 return 0;
1541}
1542
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001543/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001544/*
1545 struct __builtin_CFString {
1546 const int *isa; // point to __CFConstantStringClassReference
1547 int flags;
1548 const char *str;
1549 long length;
1550 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001551*/
1552
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001553/// or Generate a constant NSString object.
1554/*
1555 struct __builtin_NSString {
1556 const int *isa; // point to __NSConstantStringClassReference
1557 const char *str;
1558 unsigned int length;
1559 };
1560*/
1561
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001562llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001563 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001564 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1565 CGM.GetAddrOfConstantCFString(SL) :
1566 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001567}
1568
1569/// Generates a message send where the super is the receiver. This is
1570/// a message send to self with special delivery semantics indicating
1571/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001572CodeGen::RValue
1573CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001574 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001575 QualType ResultType,
1576 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001577 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001578 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001579 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001580 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001581 const CodeGen::CallArgList &CallArgs,
1582 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001583 // Create and init a super structure; this is a (receiver, class)
1584 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001585 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001586 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001587 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001588 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001589 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001590 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001591
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001592 // If this is a class message the metaclass is passed as the target.
1593 llvm::Value *Target;
1594 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001595 if (isCategoryImpl) {
1596 // Message sent to 'super' in a class method defined in a category
1597 // implementation requires an odd treatment.
1598 // If we are in a class method, we must retrieve the
1599 // _metaclass_ for the current class, pointed at by
1600 // the class's "isa" pointer. The following assumes that
1601 // isa" is the first ivar in a class (which it must be).
1602 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1603 Target = CGF.Builder.CreateStructGEP(Target, 0);
1604 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001605 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001606 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1607 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1608 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1609 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001610 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001611 }
1612 else if (isCategoryImpl)
1613 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1614 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001615 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1616 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1617 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001618 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001619 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1620 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001621 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001622 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001623 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001624 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001625 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001626 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001627 EmitSelector(CGF.Builder, Sel),
1628 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001629 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001630}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001631
1632/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001633CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001634 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001635 QualType ResultType,
1636 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001637 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001638 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001639 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001640 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001641 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001642 EmitSelector(CGF.Builder, Sel),
1643 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001644 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001645}
1646
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001647CodeGen::RValue
1648CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001649 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001650 QualType ResultType,
1651 llvm::Value *Sel,
1652 llvm::Value *Arg0,
1653 QualType Arg0Ty,
1654 bool IsSuper,
1655 const CallArgList &CallArgs,
1656 const ObjCMethodDecl *Method,
1657 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001658 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001659 if (!IsSuper)
1660 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001661 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001662 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001663 CGF.getContext().getObjCSelType()));
1664 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001665
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001666 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001667 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001668 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001669 const llvm::FunctionType *FTy =
1670 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001671
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001672 if (Method)
1673 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1674 CGM.getContext().getCanonicalType(ResultType) &&
1675 "Result type mismatch!");
1676
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001677 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001678 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001679 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001680 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001681 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1682 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1683 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001684 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001685 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001686 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001687 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001688 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001689 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001690}
1691
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001692llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001693 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1694 llvm::Constant *NullPtr =
1695 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
1696 if (DeclRefs.empty())
1697 return NullPtr;
1698 return NullPtr;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001699}
1700
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001701llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001702 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001703 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001704 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001705 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1706
Owen Anderson3c4972d2009-07-29 18:54:39 +00001707 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001708 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001709}
1710
Fariborz Jahanianda320092009-01-29 19:24:30 +00001711void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001712 // FIXME: We shouldn't need this, the protocol decl should contain enough
1713 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001714 DefinedProtocols.insert(PD->getIdentifier());
1715
1716 // If we have generated a forward reference to this protocol, emit
1717 // it now. Otherwise do nothing, the protocol objects are lazily
1718 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001719 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001720 GetOrEmitProtocol(PD);
1721}
1722
Fariborz Jahanianda320092009-01-29 19:24:30 +00001723llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001724 if (DefinedProtocols.count(PD->getIdentifier()))
1725 return GetOrEmitProtocol(PD);
1726 return GetOrEmitProtocolRef(PD);
1727}
1728
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001729/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001730// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1731struct _objc_protocol {
1732struct _objc_protocol_extension *isa;
1733char *protocol_name;
1734struct _objc_protocol_list *protocol_list;
1735struct _objc__method_prototype_list *instance_methods;
1736struct _objc__method_prototype_list *class_methods
1737};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001738
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001740*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001741llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1742 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1743
1744 // Early exit if a defining object has already been generated.
1745 if (Entry && Entry->hasInitializer())
1746 return Entry;
1747
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001748 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001749 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001750 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1751
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001752 // Construct method lists.
1753 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1754 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001755 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001756 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001757 ObjCMethodDecl *MD = *i;
1758 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1759 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1760 OptInstanceMethods.push_back(C);
1761 } else {
1762 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001763 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001764 }
1765
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001766 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001767 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001768 ObjCMethodDecl *MD = *i;
1769 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1770 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1771 OptClassMethods.push_back(C);
1772 } else {
1773 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001774 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001775 }
1776
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001777 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001778 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001779 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001780 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001781 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001782 PD->protocol_begin(),
1783 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001784 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001785 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001786 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1787 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001788 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001789 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001790 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1791 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001792 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001793 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001794
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001795 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001796 // Already created, fix the linkage and update the initializer.
1797 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798 Entry->setInitializer(Init);
1799 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001800 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001801 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001802 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001803 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001804 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001805 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001806 // FIXME: Is this necessary? Why only for protocol?
1807 Entry->setAlignment(4);
1808 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001809 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001810
1811 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001812}
1813
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001814llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001815 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1816
1817 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001818 // We use the initializer as a marker of whether this is a forward
1819 // reference or not. At module finalization we add the empty
1820 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001821 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001822 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001823 llvm::GlobalValue::ExternalLinkage,
1824 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001825 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001826 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827 // FIXME: Is this necessary? Why only for protocol?
1828 Entry->setAlignment(4);
1829 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001830
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001831 return Entry;
1832}
1833
1834/*
1835 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001836 uint32_t size;
1837 struct objc_method_description_list *optional_instance_methods;
1838 struct objc_method_description_list *optional_class_methods;
1839 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001840 };
1841*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001842llvm::Constant *
1843CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1844 const ConstantVector &OptInstanceMethods,
1845 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001847 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001848 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001849 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001850 Values[1] =
1851 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001852 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001853 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1854 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001855 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001856 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001857 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1858 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001859 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001860 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001861
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001862 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001864 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001865 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001866
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001867 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001868 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001870 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001871 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001872 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001873 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001874}
1875
1876/*
1877 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001878 struct objc_protocol_list *next;
1879 long count;
1880 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001881 };
1882*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001883llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001884CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001885 ObjCProtocolDecl::protocol_iterator begin,
1886 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001887 std::vector<llvm::Constant*> ProtocolRefs;
1888
Daniel Dunbardbc93372008-08-21 21:57:41 +00001889 for (; begin != end; ++begin)
1890 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001891
1892 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001893 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001894 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001895
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001896 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001897 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001898
1899 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001900 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001901 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001902 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001903 ProtocolRefs.size() - 1);
1904 Values[2] =
1905 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1906 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001907 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001908
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001909 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001910 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001911 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001912 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001913 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001914}
1915
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001916void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1917 std::vector<llvm::Constant*> &Properties,
1918 const Decl *Container,
1919 const ObjCProtocolDecl *PROTO,
1920 const ObjCCommonTypesHelper &ObjCTypes) {
1921 std::vector<llvm::Constant*> Prop(2);
1922 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1923 E = PROTO->protocol_end(); P != E; ++P)
1924 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1925 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1926 E = PROTO->prop_end(); I != E; ++I) {
1927 const ObjCPropertyDecl *PD = *I;
1928 if (!PropertySet.insert(PD->getIdentifier()))
1929 continue;
1930 Prop[0] = GetPropertyName(PD->getIdentifier());
1931 Prop[1] = GetPropertyTypeString(PD, Container);
1932 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1933 }
1934}
1935
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001936/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001937 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001938 const char * const name;
1939 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001940 };
1941
1942 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001943 uint32_t entsize; // sizeof (struct _objc_property)
1944 uint32_t prop_count;
1945 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001946 };
1947*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001948llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001949 const Decl *Container,
1950 const ObjCContainerDecl *OCD,
1951 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001952 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001953 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001954 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1955 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001956 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001957 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001958 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001959 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001960 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001961 Prop));
1962 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001963 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001964 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1965 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001966 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1967 ObjCTypes);
1968 }
1969 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1970 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1971 E = CD->protocol_end(); P != E; ++P)
1972 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1973 ObjCTypes);
1974 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001975
1976 // Return null for empty list.
1977 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001978 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001979
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001980 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001981 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001982 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001983 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1984 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001985 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001986 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001987 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001988 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001989
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001990 llvm::GlobalVariable *GV =
1991 CreateMetadataVar(Name, Init,
1992 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001993 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001994 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001995 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001996 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001997}
1998
1999/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002000 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002001 int count;
2002 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002003 };
2004*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002005llvm::Constant *
2006CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2007 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002008 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002009 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2010 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002011 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002012 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002013 Desc);
2014}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002015
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002016llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002017 const char *Section,
2018 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002019 // Return null for empty list.
2020 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002021 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022
2023 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002024 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002025 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002026 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002027 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002028 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002029
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002030 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002031 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002032 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002033}
2034
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002035/*
2036 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002037 char *category_name;
2038 char *class_name;
2039 struct _objc_method_list *instance_methods;
2040 struct _objc_method_list *class_methods;
2041 struct _objc_protocol_list *protocols;
2042 uint32_t size; // <rdar://4585769>
2043 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002044 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002045*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002046void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002047 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002048
Mike Stumpf5408fe2009-05-16 07:57:57 +00002049 // FIXME: This is poor design, the OCD should have a pointer to the category
2050 // decl. Additionally, note that Category can be null for the @implementation
2051 // w/o an @interface case. Sema should just create one for us as it does for
2052 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002053 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002054 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002055 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002056
2057 llvm::SmallString<256> ExtName;
2058 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2059 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002060
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002061 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002062 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002063 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002064 // Instance methods should always be defined.
2065 InstanceMethods.push_back(GetMethodConstant(*i));
2066 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002067 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002068 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002069 // Class methods should always be defined.
2070 ClassMethods.push_back(GetMethodConstant(*i));
2071 }
2072
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002073 std::vector<llvm::Constant*> Values(7);
2074 Values[0] = GetClassName(OCD->getIdentifier());
2075 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002076 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002077 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002078 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002079 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002080 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002081 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002082 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002083 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002084 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002085 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002086 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002087 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002088 Category->protocol_begin(),
2089 Category->protocol_end());
2090 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002091 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002092 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002093 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002094
2095 // If there is no category @interface then there can be no properties.
2096 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002097 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002098 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002099 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002100 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002101 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002102
Owen Anderson08e25242009-07-27 22:29:56 +00002103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002104 Values);
2105
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002107 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002108 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002109 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002110 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002111 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002112}
2113
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002114// FIXME: Get from somewhere?
2115enum ClassFlags {
2116 eClassFlags_Factory = 0x00001,
2117 eClassFlags_Meta = 0x00002,
2118 // <rdr://5142207>
2119 eClassFlags_HasCXXStructors = 0x02000,
2120 eClassFlags_Hidden = 0x20000,
2121 eClassFlags_ABI2_Hidden = 0x00010,
2122 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2123};
2124
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002125/*
2126 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002127 Class isa;
2128 Class super_class;
2129 const char *name;
2130 long version;
2131 long info;
2132 long instance_size;
2133 struct _objc_ivar_list *ivars;
2134 struct _objc_method_list *methods;
2135 struct _objc_cache *cache;
2136 struct _objc_protocol_list *protocols;
2137 // Objective-C 1.0 extensions (<rdr://4585769>)
2138 const char *ivar_layout;
2139 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002140 };
2141
2142 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002143*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002144void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002145 DefinedSymbols.insert(ID->getIdentifier());
2146
Chris Lattner8ec03f52008-11-24 03:54:41 +00002147 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002148 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002149 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002151 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002152 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002153 Interface->protocol_begin(),
2154 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002156 if (ID->getNumIvarInitializers())
2157 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002158 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002159 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002160
2161 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002162 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002163 Flags |= eClassFlags_Hidden;
2164
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002165 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002166 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002167 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002168 // Instance methods should always be defined.
2169 InstanceMethods.push_back(GetMethodConstant(*i));
2170 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002171 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002172 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002173 // Class methods should always be defined.
2174 ClassMethods.push_back(GetMethodConstant(*i));
2175 }
2176
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002177 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002178 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002179 ObjCPropertyImplDecl *PID = *i;
2180
2181 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2182 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2183
2184 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2185 if (llvm::Constant *C = GetMethodConstant(MD))
2186 InstanceMethods.push_back(C);
2187 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2188 if (llvm::Constant *C = GetMethodConstant(MD))
2189 InstanceMethods.push_back(C);
2190 }
2191 }
2192
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002194 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002195 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002196 // Record a reference to the super class.
2197 LazySymbols.insert(Super->getIdentifier());
2198
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002199 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002200 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002201 ObjCTypes.ClassPtrTy);
2202 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002203 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 }
2205 Values[ 2] = GetClassName(ID->getIdentifier());
2206 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002207 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2208 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2209 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002210 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002211 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002212 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002213 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002214 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002215 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002216 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002218 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002220 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002222 std::string Name("\01L_OBJC_CLASS_");
2223 Name += ClassName;
2224 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2225 // Check for a forward reference.
2226 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2227 if (GV) {
2228 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2229 "Forward metaclass reference has incorrect type.");
2230 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2231 GV->setInitializer(Init);
2232 GV->setSection(Section);
2233 GV->setAlignment(4);
2234 CGM.AddUsedGlobal(GV);
2235 }
2236 else
2237 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238 DefinedClasses.push_back(GV);
2239}
2240
2241llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2242 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002243 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002245 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246
Daniel Dunbar04d40782009-04-14 06:00:08 +00002247 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002248 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002249
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250 std::vector<llvm::Constant*> Values(12);
2251 // The isa for the metaclass is the root of the hierarchy.
2252 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2253 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2254 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002255 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002256 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002257 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002258 // The super class for the metaclass is emitted as the name of the
2259 // super class. The runtime fixes this up to point to the
2260 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002261 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002262 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002263 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002264 ObjCTypes.ClassPtrTy);
2265 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002266 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 }
2268 Values[ 2] = GetClassName(ID->getIdentifier());
2269 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002270 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2271 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2272 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002273 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002274 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002275 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002276 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002277 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002278 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002279 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 Values[ 9] = Protocols;
2281 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002282 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002284 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002285 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002286 Values);
2287
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002288 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002289 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002290
2291 // Check for a forward reference.
2292 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2293 if (GV) {
2294 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2295 "Forward metaclass reference has incorrect type.");
2296 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2297 GV->setInitializer(Init);
2298 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002299 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002300 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002301 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002302 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002304 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002305 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306
2307 return GV;
2308}
2309
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002310llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002311 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002312
Mike Stumpf5408fe2009-05-16 07:57:57 +00002313 // FIXME: Should we look these up somewhere other than the module. Its a bit
2314 // silly since we only generate these while processing an implementation, so
2315 // exactly one pointer would work if know when we entered/exitted an
2316 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002317
2318 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002319 // Previously, metaclass with internal linkage may have been defined.
2320 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002321 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2322 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002323 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2324 "Forward metaclass reference has incorrect type.");
2325 return GV;
2326 } else {
2327 // Generate as an external reference to keep a consistent
2328 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002329 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002330 llvm::GlobalValue::ExternalLinkage,
2331 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002332 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002333 }
2334}
2335
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002336llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2337 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2338
2339 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2340 true)) {
2341 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2342 "Forward class metadata reference has incorrect type.");
2343 return GV;
2344 } else {
2345 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2346 llvm::GlobalValue::ExternalLinkage,
2347 0,
2348 Name);
2349 }
2350}
2351
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002352/*
2353 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002354 uint32_t size;
2355 const char *weak_ivar_layout;
2356 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357 };
2358*/
2359llvm::Constant *
2360CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002361 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002362 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002363
2364 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002365 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002366 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002367 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002368 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002369
2370 // Return null if no extension bits are used.
2371 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002372 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002374 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002375 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002376 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002377 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002378 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379}
2380
2381/*
2382 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002383 char *ivar_name;
2384 char *ivar_type;
2385 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002386 };
2387
2388 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002389 int ivar_count;
2390 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002392*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002394 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395 std::vector<llvm::Constant*> Ivars, Ivar(3);
2396
2397 // When emitting the root class GCC emits ivar entries for the
2398 // actual class structure. It is not clear if we need to follow this
2399 // behavior; for now lets try and get away with not doing it. If so,
2400 // the cleanest solution would be to make up an ObjCInterfaceDecl
2401 // for the class.
2402 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002403 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002404
2405 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002406 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002407
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002408 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002409 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002410
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002411 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2412 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002413 // Ignore unnamed bit-fields.
2414 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002415 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002416 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2417 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002418 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002419 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002420 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 }
2422
2423 // Return null for empty list.
2424 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002425 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426
2427 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002428 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002429 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002431 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002432 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002434 llvm::GlobalVariable *GV;
2435 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002436 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002437 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002438 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002439 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002440 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002441 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002442 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444}
2445
2446/*
2447 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 SEL method_name;
2449 char *method_types;
2450 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002452
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002454 struct objc_method_list *obsolete;
2455 int count;
2456 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002457 };
2458*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002459
2460/// GetMethodConstant - Return a struct objc_method constant for the
2461/// given method if it has been defined. The result is null if the
2462/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002463llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002464 // FIXME: Use DenseMap::lookup
2465 llvm::Function *Fn = MethodDefinitions[MD];
2466 if (!Fn)
2467 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002469 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002470 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002471 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002472 ObjCTypes.SelectorPtrTy);
2473 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002474 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002475 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002476}
2477
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002478llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002479 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002480 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481 // Return null for empty list.
2482 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002483 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
2485 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002486 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002487 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002488 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002489 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002490 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002491 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002493 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002494 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002495 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002496}
2497
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002498llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002499 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002500 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002501 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002502
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002503 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002504 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002505 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002506 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002507 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002508 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002509 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002510 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002511 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002512
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002513 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002514}
2515
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002516llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002517CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002518 llvm::Constant *Init,
2519 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002520 unsigned Align,
2521 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002522 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002523 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002524 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002525 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002526 if (Section)
2527 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002528 if (Align)
2529 GV->setAlignment(Align);
2530 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002531 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002532 return GV;
2533}
2534
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002535llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002536 // Abuse this interface function as a place to finalize.
2537 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002538 return NULL;
2539}
2540
Chris Lattner74391b42009-03-22 21:03:39 +00002541llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002542 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002543}
2544
Chris Lattner74391b42009-03-22 21:03:39 +00002545llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002546 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002547}
2548
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002549llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2550 return ObjCTypes.getCopyStructFn();
2551}
2552
Chris Lattner74391b42009-03-22 21:03:39 +00002553llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002554 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002555}
2556
John McCallf1549f62010-07-06 01:34:17 +00002557void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2558 return EmitTryOrSynchronizedStmt(CGF, S);
2559}
2560
2561void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2562 const ObjCAtSynchronizedStmt &S) {
2563 return EmitTryOrSynchronizedStmt(CGF, S);
2564}
2565
John McCallcc505292010-07-21 06:59:36 +00002566namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002567 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002568 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002569 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002570 llvm::Value *CallTryExitVar;
2571 llvm::Value *ExceptionData;
2572 ObjCTypesHelper &ObjCTypes;
2573 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002574 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002575 llvm::Value *CallTryExitVar,
2576 llvm::Value *ExceptionData,
2577 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002578 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002579 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2580
2581 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2582 // Check whether we need to call objc_exception_try_exit.
2583 // In optimized code, this branch will always be folded.
2584 llvm::BasicBlock *FinallyCallExit =
2585 CGF.createBasicBlock("finally.call_exit");
2586 llvm::BasicBlock *FinallyNoCallExit =
2587 CGF.createBasicBlock("finally.no_call_exit");
2588 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2589 FinallyCallExit, FinallyNoCallExit);
2590
2591 CGF.EmitBlock(FinallyCallExit);
2592 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2593 ->setDoesNotThrow();
2594
2595 CGF.EmitBlock(FinallyNoCallExit);
2596
2597 if (isa<ObjCAtTryStmt>(S)) {
2598 if (const ObjCAtFinallyStmt* FinallyStmt =
2599 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2600 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2601
2602 // Currently, the end of the cleanup must always exist.
2603 CGF.EnsureInsertPoint();
2604 } else {
2605 // Emit objc_sync_exit(expr); as finally's sole statement for
2606 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002607 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002608 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2609 ->setDoesNotThrow();
2610 }
2611 }
2612 };
John McCall87bb5822010-07-31 23:20:56 +00002613
2614 class FragileHazards {
2615 CodeGenFunction &CGF;
2616 llvm::SmallVector<llvm::Value*, 20> Locals;
2617 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2618
2619 llvm::InlineAsm *ReadHazard;
2620 llvm::InlineAsm *WriteHazard;
2621
2622 llvm::FunctionType *GetAsmFnType();
2623
2624 void collectLocals();
2625 void emitReadHazard(CGBuilderTy &Builder);
2626
2627 public:
2628 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002629
John McCall87bb5822010-07-31 23:20:56 +00002630 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002631 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002632 };
2633}
2634
2635/// Create the fragile-ABI read and write hazards based on the current
2636/// state of the function, which is presumed to be immediately prior
2637/// to a @try block. These hazards are used to maintain correct
2638/// semantics in the face of optimization and the fragile ABI's
2639/// cavalier use of setjmp/longjmp.
2640FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2641 collectLocals();
2642
2643 if (Locals.empty()) return;
2644
2645 // Collect all the blocks in the function.
2646 for (llvm::Function::iterator
2647 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2648 BlocksBeforeTry.insert(&*I);
2649
2650 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2651
2652 // Create a read hazard for the allocas. This inhibits dead-store
2653 // optimizations and forces the values to memory. This hazard is
2654 // inserted before any 'throwing' calls in the protected scope to
2655 // reflect the possibility that the variables might be read from the
2656 // catch block if the call throws.
2657 {
2658 std::string Constraint;
2659 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2660 if (I) Constraint += ',';
2661 Constraint += "*m";
2662 }
2663
2664 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2665 }
2666
2667 // Create a write hazard for the allocas. This inhibits folding
2668 // loads across the hazard. This hazard is inserted at the
2669 // beginning of the catch path to reflect the possibility that the
2670 // variables might have been written within the protected scope.
2671 {
2672 std::string Constraint;
2673 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2674 if (I) Constraint += ',';
2675 Constraint += "=*m";
2676 }
2677
2678 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2679 }
2680}
2681
2682/// Emit a write hazard at the current location.
2683void FragileHazards::emitWriteHazard() {
2684 if (Locals.empty()) return;
2685
2686 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2687 ->setDoesNotThrow();
2688}
2689
John McCall87bb5822010-07-31 23:20:56 +00002690void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2691 assert(!Locals.empty());
2692 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2693 ->setDoesNotThrow();
2694}
2695
2696/// Emit read hazards in all the protected blocks, i.e. all the blocks
2697/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002698void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002699 if (Locals.empty()) return;
2700
2701 CGBuilderTy Builder(CGF.getLLVMContext());
2702
2703 // Iterate through all blocks, skipping those prior to the try.
2704 for (llvm::Function::iterator
2705 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2706 llvm::BasicBlock &BB = *FI;
2707 if (BlocksBeforeTry.count(&BB)) continue;
2708
2709 // Walk through all the calls in the block.
2710 for (llvm::BasicBlock::iterator
2711 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2712 llvm::Instruction &I = *BI;
2713
2714 // Ignore instructions that aren't non-intrinsic calls.
2715 // These are the only calls that can possibly call longjmp.
2716 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2717 if (isa<llvm::IntrinsicInst>(I))
2718 continue;
2719
2720 // Ignore call sites marked nounwind. This may be questionable,
2721 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2722 llvm::CallSite CS(&I);
2723 if (CS.doesNotThrow()) continue;
2724
John McCall0b251722010-08-04 05:59:32 +00002725 // Insert a read hazard before the call. This will ensure that
2726 // any writes to the locals are performed before making the
2727 // call. If the call throws, then this is sufficient to
2728 // guarantee correctness as long as it doesn't also write to any
2729 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002730 Builder.SetInsertPoint(&BB, BI);
2731 emitReadHazard(Builder);
2732 }
2733 }
2734}
2735
2736static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2737 if (V) S.insert(V);
2738}
2739
2740void FragileHazards::collectLocals() {
2741 // Compute a set of allocas to ignore.
2742 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2743 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2744 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2745 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2746
2747 // Collect all the allocas currently in the function. This is
2748 // probably way too aggressive.
2749 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2750 for (llvm::BasicBlock::iterator
2751 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2752 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2753 Locals.push_back(&*I);
2754}
2755
2756llvm::FunctionType *FragileHazards::GetAsmFnType() {
2757 std::vector<const llvm::Type *> Tys(Locals.size());
2758 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2759 Tys[I] = Locals[I]->getType();
2760 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002761}
2762
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002763/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002764
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002765 Objective-C setjmp-longjmp (sjlj) Exception Handling
2766 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002767
John McCallf1549f62010-07-06 01:34:17 +00002768 A catch buffer is a setjmp buffer plus:
2769 - a pointer to the exception that was caught
2770 - a pointer to the previous exception data buffer
2771 - two pointers of reserved storage
2772 Therefore catch buffers form a stack, with a pointer to the top
2773 of the stack kept in thread-local storage.
2774
2775 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2776 objc_exception_try_exit pops the given catch buffer, which is
2777 required to be the top of the EH stack.
2778 objc_exception_throw pops the top of the EH stack, writes the
2779 thrown exception into the appropriate field, and longjmps
2780 to the setjmp buffer. It crashes the process (with a printf
2781 and an abort()) if there are no catch buffers on the stack.
2782 objc_exception_extract just reads the exception pointer out of the
2783 catch buffer.
2784
2785 There's no reason an implementation couldn't use a light-weight
2786 setjmp here --- something like __builtin_setjmp, but API-compatible
2787 with the heavyweight setjmp. This will be more important if we ever
2788 want to implement correct ObjC/C++ exception interactions for the
2789 fragile ABI.
2790
2791 Note that for this use of setjmp/longjmp to be correct, we may need
2792 to mark some local variables volatile: if a non-volatile local
2793 variable is modified between the setjmp and the longjmp, it has
2794 indeterminate value. For the purposes of LLVM IR, it may be
2795 sufficient to make loads and stores within the @try (to variables
2796 declared outside the @try) volatile. This is necessary for
2797 optimized correctness, but is not currently being done; this is
2798 being tracked as rdar://problem/8160285
2799
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002800 The basic framework for a @try-catch-finally is as follows:
2801 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002802 objc_exception_data d;
2803 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002804 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002805
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002806 objc_exception_try_enter(&d);
2807 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002808 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002809 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002810 // exception path
2811 id _caught = objc_exception_extract(&d);
2812
2813 // enter new try scope for handlers
2814 if (!setjmp(d.jmp_buf)) {
2815 ... match exception and execute catch blocks ...
2816
2817 // fell off end, rethrow.
2818 _rethrow = _caught;
2819 ... jump-through-finally to finally_rethrow ...
2820 } else {
2821 // exception in catch block
2822 _rethrow = objc_exception_extract(&d);
2823 _call_try_exit = false;
2824 ... jump-through-finally to finally_rethrow ...
2825 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002826 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002827 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002828
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002829 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002830 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002832
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002833 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002834 ... dispatch to finally destination ...
2835
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002836 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002837 objc_exception_throw(_rethrow);
2838
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002839 finally_end:
2840 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002841
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002842 This framework differs slightly from the one gcc uses, in that gcc
2843 uses _rethrow to determine if objc_exception_try_exit should be called
2844 and if the object should be rethrown. This breaks in the face of
2845 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002846
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002847 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002848
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002849 - If there are no catch blocks, then we avoid emitting the second
2850 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002851
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002852 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2853 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002854
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002855 - FIXME: If there is no @finally block we can do a few more
2856 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002857
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002858 Rethrows and Jumps-Through-Finally
2859 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002860
John McCallf1549f62010-07-06 01:34:17 +00002861 '@throw;' is supported by pushing the currently-caught exception
2862 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002863
John McCallf1549f62010-07-06 01:34:17 +00002864 Branches through the @finally block are handled with an ordinary
2865 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2866 exceptions are not compatible with C++ exceptions, and this is
2867 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002868
John McCallf1549f62010-07-06 01:34:17 +00002869 @synchronized(expr) { stmt; } is emitted as if it were:
2870 id synch_value = expr;
2871 objc_sync_enter(synch_value);
2872 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002873*/
2874
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002875void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2876 const Stmt &S) {
2877 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002878
2879 // A destination for the fall-through edges of the catch handlers to
2880 // jump to.
2881 CodeGenFunction::JumpDest FinallyEnd =
2882 CGF.getJumpDestInCurrentScope("finally.end");
2883
2884 // A destination for the rethrow edge of the catch handlers to jump
2885 // to.
2886 CodeGenFunction::JumpDest FinallyRethrow =
2887 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002888
Daniel Dunbar1c566672009-02-24 01:43:46 +00002889 // For @synchronized, call objc_sync_enter(sync.expr). The
2890 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002891 // @synchronized. We can't avoid a temp here because we need the
2892 // value to be preserved. If the backend ever does liveness
2893 // correctly after setjmp, this will be unnecessary.
2894 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002895 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002896 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002897 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2898 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002899 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2900 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002901
2902 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2903 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002904 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002905
John McCall0b251722010-08-04 05:59:32 +00002906 // Allocate memory for the setjmp buffer. This needs to be kept
2907 // live throughout the try and catch blocks.
2908 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2909 "exceptiondata.ptr");
2910
John McCall87bb5822010-07-31 23:20:56 +00002911 // Create the fragile hazards. Note that this will not capture any
2912 // of the allocas required for exception processing, but will
2913 // capture the current basic block (which extends all the way to the
2914 // setjmp call) as "before the @try".
2915 FragileHazards Hazards(CGF);
2916
John McCallf1549f62010-07-06 01:34:17 +00002917 // Create a flag indicating whether the cleanup needs to call
2918 // objc_exception_try_exit. This is true except when
2919 // - no catches match and we're branching through the cleanup
2920 // just to rethrow the exception, or
2921 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002922 // The setjmp-safety rule here is that we should always store to this
2923 // variable in a place that dominates the branch through the cleanup
2924 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002925 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002926 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002927
John McCallf1549f62010-07-06 01:34:17 +00002928 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002929 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002930 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002931 CallTryExitVar,
2932 ExceptionData,
2933 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002934
2935 // Enter a try block:
2936 // - Call objc_exception_try_enter to push ExceptionData on top of
2937 // the EH stack.
2938 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2939 ->setDoesNotThrow();
2940
2941 // - Call setjmp on the exception data buffer.
2942 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2943 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2944 llvm::Value *SetJmpBuffer =
2945 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2946 llvm::CallInst *SetJmpResult =
2947 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2948 SetJmpResult->setDoesNotThrow();
2949
2950 // If setjmp returned 0, enter the protected block; otherwise,
2951 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002952 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2953 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002954 llvm::Value *DidCatch =
2955 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2956 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson80f25672008-09-09 17:59:25 +00002957
John McCallf1549f62010-07-06 01:34:17 +00002958 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002959 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002960 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002961 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002962 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002963
2964 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002965
John McCallf1549f62010-07-06 01:34:17 +00002966 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002967 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002968
John McCall87bb5822010-07-31 23:20:56 +00002969 // Don't optimize loads of the in-scope locals across this point.
2970 Hazards.emitWriteHazard();
2971
John McCallf1549f62010-07-06 01:34:17 +00002972 // For a @synchronized (or a @try with no catches), just branch
2973 // through the cleanup to the rethrow block.
2974 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2975 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002976 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002977 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002978
2979 // Otherwise, we have to match against the caught exceptions.
2980 } else {
John McCall0b251722010-08-04 05:59:32 +00002981 // Retrieve the exception object. We may emit multiple blocks but
2982 // nothing can cross this so the value is already in SSA form.
2983 llvm::CallInst *Caught =
2984 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2985 ExceptionData, "caught");
2986 Caught->setDoesNotThrow();
2987
John McCallf1549f62010-07-06 01:34:17 +00002988 // Push the exception to rethrow onto the EH value stack for the
2989 // benefit of any @throws in the handlers.
2990 CGF.ObjCEHValueStack.push_back(Caught);
2991
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002992 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002993
John McCall0b251722010-08-04 05:59:32 +00002994 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00002995
John McCall0b251722010-08-04 05:59:32 +00002996 llvm::BasicBlock *CatchBlock = 0;
2997 llvm::BasicBlock *CatchHandler = 0;
2998 if (HasFinally) {
2999 // Enter a new exception try block (in case a @catch block
3000 // throws an exception).
3001 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3002 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003003
John McCall0b251722010-08-04 05:59:32 +00003004 llvm::CallInst *SetJmpResult =
3005 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3006 "setjmp.result");
3007 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003008
John McCall0b251722010-08-04 05:59:32 +00003009 llvm::Value *Threw =
3010 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3011
3012 CatchBlock = CGF.createBasicBlock("catch");
3013 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3014 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3015
3016 CGF.EmitBlock(CatchBlock);
3017 }
3018
3019 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003020
Daniel Dunbar55e40722008-09-27 07:03:52 +00003021 // Handle catch list. As a special case we check if everything is
3022 // matched and avoid generating code for falling off the end if
3023 // so.
3024 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003025 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3026 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003027
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003028 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003029 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003030
Anders Carlsson80f25672008-09-09 17:59:25 +00003031 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003032 if (!CatchParam) {
3033 AllMatched = true;
3034 } else {
John McCall183700f2009-09-21 23:43:11 +00003035 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003036
John McCallf1549f62010-07-06 01:34:17 +00003037 // catch(id e) always matches under this ABI, since only
3038 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003039 // FIXME: For the time being we also match id<X>; this should
3040 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003041 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003042 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003043 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003044
John McCallf1549f62010-07-06 01:34:17 +00003045 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003046 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003047 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3048
Anders Carlssondde0a942008-09-11 09:15:33 +00003049 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003050 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003051 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003052
3053 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003054 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003055 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003056
Anders Carlssondde0a942008-09-11 09:15:33 +00003057 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003058
3059 // The scope of the catch variable ends right here.
3060 CatchVarCleanups.ForceCleanup();
3061
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003062 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003063 break;
3064 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003065
Steve Naroff14108da2009-07-10 23:34:53 +00003066 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003067 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003068
3069 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003070 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3071 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003072
3073 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003074 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003075
John McCallf1549f62010-07-06 01:34:17 +00003076 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003077 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3078 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003079 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003080
John McCallf1549f62010-07-06 01:34:17 +00003081 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3082 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003083
3084 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003085 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086
Anders Carlsson80f25672008-09-09 17:59:25 +00003087 // Emit the @catch block.
3088 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003089
3090 // Collect any cleanups for the catch variable. The scope lasts until
3091 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003092 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003093
Steve Naroff7ba138a2009-03-03 19:52:17 +00003094 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003095 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003096
John McCallf1549f62010-07-06 01:34:17 +00003097 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003098 llvm::Value *Tmp =
3099 CGF.Builder.CreateBitCast(Caught,
3100 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003101 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003102 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003103
Anders Carlssondde0a942008-09-11 09:15:33 +00003104 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003105
3106 // We're done with the catch variable.
3107 CatchVarCleanups.ForceCleanup();
3108
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003109 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003110
Anders Carlsson80f25672008-09-09 17:59:25 +00003111 CGF.EmitBlock(NextCatchBlock);
3112 }
3113
John McCallf1549f62010-07-06 01:34:17 +00003114 CGF.ObjCEHValueStack.pop_back();
3115
John McCall0b251722010-08-04 05:59:32 +00003116 // If nothing wanted anything to do with the caught exception,
3117 // kill the extract call.
3118 if (Caught->use_empty())
3119 Caught->eraseFromParent();
3120
3121 if (!AllMatched)
3122 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3123
3124 if (HasFinally) {
3125 // Emit the exception handler for the @catch blocks.
3126 CGF.EmitBlock(CatchHandler);
3127
3128 // In theory we might now need a write hazard, but actually it's
3129 // unnecessary because there's no local-accessing code between
3130 // the try's write hazard and here.
3131 //Hazards.emitWriteHazard();
3132
3133 // Don't pop the catch handler; the throw already did.
3134 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003135 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003136 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003137 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003138
John McCall87bb5822010-07-31 23:20:56 +00003139 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003140 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003141
John McCallf1549f62010-07-06 01:34:17 +00003142 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003143 CGF.Builder.restoreIP(TryFallthroughIP);
3144 if (CGF.HaveInsertPoint())
3145 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003146 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003147 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003148
John McCallf1549f62010-07-06 01:34:17 +00003149 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003150 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003151 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003152 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003153 // Just look in the buffer for the exception to throw.
3154 llvm::CallInst *Caught =
3155 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3156 ExceptionData);
3157 Caught->setDoesNotThrow();
3158
3159 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003160 ->setDoesNotThrow();
3161 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003162 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003163
John McCall87bb5822010-07-31 23:20:56 +00003164 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003165}
3166
3167void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003168 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003169 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003170
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003171 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3172 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003173 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003174 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3175 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003177 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003178 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003179 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180
John McCallf1549f62010-07-06 01:34:17 +00003181 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3182 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003183 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003184
3185 // Clear the insertion point to indicate we are in unreachable code.
3186 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003187}
3188
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003189/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003190/// object: objc_read_weak (id *src)
3191///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003192llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003193 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003194 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003195 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3196 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3197 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003198 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003199 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003200 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003201 return read_weak;
3202}
3203
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003204/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3205/// objc_assign_weak (id src, id *dst)
3206///
3207void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003208 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003209 const llvm::Type * SrcTy = src->getType();
3210 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003211 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003212 assert(Size <= 8 && "does not support size > 8");
3213 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003214 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003215 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3216 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003217 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3218 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003219 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003220 src, dst, "weakassign");
3221 return;
3222}
3223
Fariborz Jahanian58626502008-11-19 00:59:10 +00003224/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3225/// objc_assign_global (id src, id *dst)
3226///
3227void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003228 llvm::Value *src, llvm::Value *dst,
3229 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003230 const llvm::Type * SrcTy = src->getType();
3231 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003232 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003233 assert(Size <= 8 && "does not support size > 8");
3234 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003235 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003236 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3237 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003238 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3239 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003240 if (!threadlocal)
3241 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3242 src, dst, "globalassign");
3243 else
3244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3245 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003246 return;
3247}
3248
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003249/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003250/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003251///
3252void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003253 llvm::Value *src, llvm::Value *dst,
3254 llvm::Value *ivarOffset) {
3255 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003256 const llvm::Type * SrcTy = src->getType();
3257 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003258 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003259 assert(Size <= 8 && "does not support size > 8");
3260 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003261 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003262 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3263 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003264 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3265 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003266 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3267 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003268 return;
3269}
3270
Fariborz Jahanian58626502008-11-19 00:59:10 +00003271/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3272/// objc_assign_strongCast (id src, id *dst)
3273///
3274void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003275 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003276 const llvm::Type * SrcTy = src->getType();
3277 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003278 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003279 assert(Size <= 8 && "does not support size > 8");
3280 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003282 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3283 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003284 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3285 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003286 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003287 src, dst, "weakassign");
3288 return;
3289}
3290
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003291void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003292 llvm::Value *DestPtr,
3293 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003294 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003295 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3296 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003297 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003298 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003299 return;
3300}
3301
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003302/// EmitObjCValueForIvar - Code Gen for ivar reference.
3303///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003304LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3305 QualType ObjectTy,
3306 llvm::Value *BaseValue,
3307 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003308 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003309 const ObjCInterfaceDecl *ID =
3310 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003311 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3312 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003313}
3314
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003315llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003316 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003317 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003318 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003319 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3321 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003322}
3323
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003324/* *** Private Interface *** */
3325
3326/// EmitImageInfo - Emit the image info marker used to encode some module
3327/// level information.
3328///
3329/// See: <rdr://4810609&4810587&4810587>
3330/// struct IMAGE_INFO {
3331/// unsigned version;
3332/// unsigned flags;
3333/// };
3334enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003335 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336 eImageInfo_GarbageCollected = (1 << 1),
3337 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003338 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3339
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003340 // A flag indicating that the module has no instances of a @synthesize of a
3341 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003342 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003343};
3344
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003345void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003346 unsigned version = 0; // Version is unused?
3347 unsigned flags = 0;
3348
3349 // FIXME: Fix and continue?
3350 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3351 flags |= eImageInfo_GarbageCollected;
3352 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3353 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003355 // We never allow @synthesize of a superclass property.
3356 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003357
Chris Lattner77b89b82010-06-27 07:15:29 +00003358 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3359
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003360 // Emitted as int[2];
3361 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003362 llvm::ConstantInt::get(Int32Ty, version),
3363 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003364 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003365 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003366
3367 const char *Section;
3368 if (ObjCABI == 1)
3369 Section = "__OBJC, __image_info,regular";
3370 else
3371 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003372 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003373 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003374 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003375 Section,
3376 0,
3377 true);
3378 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003379}
3380
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003381
3382// struct objc_module {
3383// unsigned long version;
3384// unsigned long size;
3385// const char *name;
3386// Symtab symtab;
3387// };
3388
3389// FIXME: Get from somewhere
3390static const int ModuleVersion = 7;
3391
3392void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003393 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003395 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003396 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3397 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003398 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003399 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003400 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003401 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003402 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003403 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003404 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003405}
3406
3407llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003408 unsigned NumClasses = DefinedClasses.size();
3409 unsigned NumCategories = DefinedCategories.size();
3410
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003411 // Return null if no symbols were defined.
3412 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003413 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003414
3415 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003416 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003417 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003418 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3419 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003420
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003421 // The runtime expects exactly the list of defined classes followed
3422 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003423 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003424 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003425 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003426 ObjCTypes.Int8PtrTy);
3427 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003428 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003429 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003430 ObjCTypes.Int8PtrTy);
3431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003432 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003433 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003434 NumClasses + NumCategories),
3435 Symbols);
3436
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003437 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003438
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003439 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003440 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3441 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003442 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444}
3445
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003446llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003447 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003448 LazySymbols.insert(ID->getIdentifier());
3449
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003450 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003451
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003452 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003454 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003455 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003456 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003457 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3458 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003459 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460 }
3461
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003462 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003463}
3464
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003465llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3466 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003467 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003469 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003470 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003471 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003472 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003473 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003474 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3475 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003476 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003477 }
3478
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003479 if (lvalue)
3480 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003481 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003482}
3483
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003484llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003485 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003486
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003487 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003488 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003489 llvm::ConstantArray::get(VMContext,
3490 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003491 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003492 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003493
Owen Andersona1cf15f2009-07-14 23:10:40 +00003494 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003495}
3496
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003497/// GetIvarLayoutName - Returns a unique constant for the given
3498/// ivar layout bitmap.
3499llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003500 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003501 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003502}
3503
John McCall0953e762009-09-24 19:53:00 +00003504static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003505 if (FQT.isObjCGCStrong())
John McCall0953e762009-09-24 19:53:00 +00003506 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003507
3508 if (FQT.isObjCGCWeak())
John McCall0953e762009-09-24 19:53:00 +00003509 return Qualifiers::Weak;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003510
Fariborz Jahanian039e6a12009-09-11 17:39:05 +00003511 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003512 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003513
Ted Kremenek6217b802009-07-29 21:53:49 +00003514 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003515 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003516
John McCall0953e762009-09-24 19:53:00 +00003517 return Qualifiers::GCNone;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003518}
3519
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003520void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003521 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003522 bool ForStrongLayout,
3523 bool &HasUnion) {
3524 const RecordDecl *RD = RT->getDecl();
3525 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003526 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003527 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003528 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003529 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003530
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003531 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3532 ForStrongLayout, HasUnion);
3533}
3534
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003535void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536 const llvm::StructLayout *Layout,
3537 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003538 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003539 unsigned int BytePos, bool ForStrongLayout,
3540 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003541 bool IsUnion = (RD && RD->isUnion());
3542 uint64_t MaxUnionIvarSize = 0;
3543 uint64_t MaxSkippedUnionIvarSize = 0;
3544 FieldDecl *MaxField = 0;
3545 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003546 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003547 uint64_t MaxFieldOffset = 0;
3548 uint64_t MaxSkippedFieldOffset = 0;
3549 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003550
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003551 if (RecFields.empty())
3552 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003553 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3554 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3555
Chris Lattnerf1690852009-03-31 08:48:01 +00003556 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003557 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003558 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003559 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003560 // Note that 'i' here is actually the field index inside RD of Field,
3561 // although this dependency is hidden.
3562 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3563 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003564 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003565 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003566
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003567 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003568 if (!Field->getIdentifier() || Field->isBitField()) {
3569 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003570 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003571 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003572 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003573
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003574 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003575 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003576 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003577 if (FQT->isUnionType())
3578 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003579
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003581 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003582 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003583 continue;
3584 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585
Chris Lattnerf1690852009-03-31 08:48:01 +00003586 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003588 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003589 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003590 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003591 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003592 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3593 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003594 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003595 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003596 FQT = CArray->getElementType();
3597 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003598
3599 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003600 "layout for array of unions not supported");
3601 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003602 int OldIndex = IvarsInfo.size() - 1;
3603 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003604
Ted Kremenek6217b802009-07-29 21:53:49 +00003605 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003606 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003607 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003608
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003609 // Replicate layout information for each array element. Note that
3610 // one element is already done.
3611 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003612 for (int FirstIndex = IvarsInfo.size() - 1,
3613 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003614 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003615 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3616 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3617 IvarsInfo[i].ivar_size));
3618 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3619 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3620 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003621 }
3622 continue;
3623 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003624 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003625 // At this point, we are done with Record/Union and array there of.
3626 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003627 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003628
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003629 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003630 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3631 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003632 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003633 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003634 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003635 MaxUnionIvarSize = UnionIvarSize;
3636 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003637 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003638 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003639 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003640 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003641 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003642 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003643 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003644 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3645 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003646 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003647 // FIXME: Why the asymmetry? We divide by word size in bits on other
3648 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003649 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003650 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003651 MaxSkippedUnionIvarSize = UnionIvarSize;
3652 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003653 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003654 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003655 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003656 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003657 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003658 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003659 }
3660 }
3661 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003662
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003663 if (LastFieldBitfield) {
3664 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003665 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3666 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003667 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003668 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003669 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003670 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3671 + ((BitFieldSize % ByteSizeInBits) != 0);
3672 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003673 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003675 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003677 MaxUnionIvarSize));
3678 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003679 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003680 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003681}
3682
3683/// BuildIvarLayout - Builds ivar layout bitmap for the class
3684/// implementation for the __strong or __weak case.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685/// The layout map displays which words in ivar list must be skipped
3686/// and which must be scanned by GC (see below). String is built of bytes.
3687/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003688/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003689/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003690/// represented by a 0x00 byte which also ends the string.
3691/// 1. when ForStrongLayout is true, following ivars are scanned:
3692/// - id, Class
3693/// - object *
3694/// - __strong anything
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003695///
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003696/// 2. When ForStrongLayout is false, following ivars are scanned:
3697/// - __weak anything
3698///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003699llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003700 const ObjCImplementationDecl *OMD,
3701 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003702 bool hasUnion = false;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003703
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003704 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003705 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003706 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003707 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003708
Chris Lattnerf1690852009-03-31 08:48:01 +00003709 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003710 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003711 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003712
Daniel Dunbar37153282009-05-04 04:10:48 +00003713 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003714 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian11062e12010-02-19 00:31:17 +00003715 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003716 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3717 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003718
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003719 if (RecFields.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003720 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003721
3722 SkipIvars.clear();
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003723 IvarsInfo.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003724
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003725 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003726 if (IvarsInfo.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003727 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003728
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003729 // Sort on byte position in case we encounterred a union nested in
3730 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003731 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003732 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003733 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003734 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003735
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003736 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003737 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003738 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003739 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003740 if (IvarsInfo[0].ivar_bytepos == 0) {
3741 WordsToSkip = 0;
3742 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003743 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003744 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3745 WordsToScan = IvarsInfo[0].ivar_size;
3746 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003747 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748 unsigned int TailPrevGCObjC =
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003749 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003750 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003751 // consecutive 'scanned' object pointers.
3752 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003753 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003754 // Skip over 'gc'able object pointer which lay over each other.
3755 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3756 continue;
3757 // Must skip over 1 or more words. We save current skip/scan values
3758 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003759 SKIP_SCAN SkScan;
3760 SkScan.skip = WordsToSkip;
3761 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003762 SkipScanIvars.push_back(SkScan);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003763
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003764 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003765 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3766 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003767 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003768 WordsToSkip = 0;
3769 WordsToScan = IvarsInfo[i].ivar_size;
3770 }
3771 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003772 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003773 SKIP_SCAN SkScan;
3774 SkScan.skip = WordsToSkip;
3775 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003776 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003777 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003778
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003779 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003780 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003781 int LastByteSkipped =
3782 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003783 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003784 int LastByteScanned =
3785 IvarsInfo[LastIndex].ivar_bytepos +
3786 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003787 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003788 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003790 SKIP_SCAN SkScan;
3791 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3792 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003793 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003794 }
3795 }
3796 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3797 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003798 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003799 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003800 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3801 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3802 // 0xM0 followed by 0x0N detected.
3803 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3804 for (int j = i+1; j < SkipScan; j++)
3805 SkipScanIvars[j] = SkipScanIvars[j+1];
3806 --SkipScan;
3807 }
3808 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003809
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003810 // Generate the string.
3811 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003812 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003813 unsigned char byte;
3814 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3815 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3816 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3817 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3818
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003819 // first skip big.
3820 for (unsigned int ix = 0; ix < skip_big; ix++)
3821 BitMap += (unsigned char)(0xf0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003822
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003823 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003824 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003825 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003826 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003827 byte |= 0xf;
3828 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003829 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003830 byte |= scan_small;
3831 scan_small = 0;
3832 }
3833 BitMap += byte;
3834 }
3835 // next scan big
3836 for (unsigned int ix = 0; ix < scan_big; ix++)
3837 BitMap += (unsigned char)(0x0f);
3838 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003839 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003840 byte = scan_small;
3841 BitMap += byte;
3842 }
3843 }
3844 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003845 unsigned char zero = 0;
3846 BitMap += zero;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003847
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003848 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003849 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003850 ForStrongLayout ? "strong" : "weak",
3851 OMD->getClassInterface()->getNameAsCString());
3852 const unsigned char *s = (unsigned char*)BitMap.c_str();
3853 for (unsigned i = 0; i < BitMap.size(); i++)
3854 if (!(s[i] & 0xf0))
3855 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3856 else
3857 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3858 printf("\n");
3859 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003860 llvm::GlobalVariable * Entry =
3861 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003862 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003863 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003864 1, true);
3865 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003866}
3867
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003868llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003869 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3870
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003871 // FIXME: Avoid std::string copying.
3872 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003873 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003874 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003875 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003876 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003877
Owen Andersona1cf15f2009-07-14 23:10:40 +00003878 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003879}
3880
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003881// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003882llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003883 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3884}
3885
3886// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003887llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003888 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3889}
3890
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003891llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003892 std::string TypeStr;
3893 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3894
3895 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003896
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003897 if (!Entry)
3898 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003899 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003900 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003901 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003902
Owen Andersona1cf15f2009-07-14 23:10:40 +00003903 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003904}
3905
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003906llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003907 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003908 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3909 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003910
3911 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3912
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003913 if (!Entry)
3914 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003915 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003916 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003917 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003918
Owen Andersona1cf15f2009-07-14 23:10:40 +00003919 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003920}
3921
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003922// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003923llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003924 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003925
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003926 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003927 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003928 llvm::ConstantArray::get(VMContext,
3929 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003930 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003931 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003932
Owen Andersona1cf15f2009-07-14 23:10:40 +00003933 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003934}
3935
3936// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003937// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003938llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003939CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3940 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003941 std::string TypeStr;
3942 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003943 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3944}
3945
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003947 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003948 llvm::SmallVectorImpl<char> &Name) {
3949 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003950 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003951 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3952 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003953 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003954 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003955 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003956 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003957}
3958
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003959void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003960 EmitModuleInfo();
3961
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003962 // Emit the dummy bodies for any protocols which were referenced but
3963 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003964 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003965 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3966 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003967 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003968
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003969 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003970 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003971 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003972 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003973 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003974 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003975 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003976 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003977 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003978 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003979 }
3980
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003981 // Add assembler directives to add lazy undefined symbol references
3982 // for classes which are referenced but not defined. This is
3983 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00003984 //
3985 // FIXME: It would be nice if we had an LLVM construct for this.
3986 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3987 llvm::SmallString<256> Asm;
3988 Asm += CGM.getModule().getModuleInlineAsm();
3989 if (!Asm.empty() && Asm.back() != '\n')
3990 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003991
Daniel Dunbar33063492009-09-07 00:20:42 +00003992 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00003993 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3994 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003995 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3996 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003997 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00003998 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003999 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004000 }
4001
4002 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4003 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4004 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4005 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004006
Daniel Dunbar33063492009-09-07 00:20:42 +00004007 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004008 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004009}
4010
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004011CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004012 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004013 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004014 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004015 ObjCABI = 2;
4016}
4017
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004018/* *** */
4019
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004020ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004021 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004022 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4023 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004024
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004025 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004026 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004027 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004028 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004029 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004030
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004031 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004032 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004033 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004034
Mike Stumpf5408fe2009-05-16 07:57:57 +00004035 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4036 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004037 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004038 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004039
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004040 // I'm not sure I like this. The implicit coordination is a bit
4041 // gross. We should solve this in a reasonable fashion because this
4042 // is a pretty common task (match some runtime data structure with
4043 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004044
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004045 // FIXME: This is leaked.
4046 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004047
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004048 // struct _objc_super {
4049 // id self;
4050 // Class cls;
4051 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004052 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004053 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004054 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004055 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004056 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004057 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004058 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004059 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004060 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004061
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004062 SuperCTy = Ctx.getTagDeclType(RD);
4063 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004064
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004065 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004066 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004068 // struct _prop_t {
4069 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004070 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004071 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004072 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004073 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004074 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004075
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004076 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004077 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004078 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004079 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004080 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004081 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004082 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004083 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004084 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004085 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004086 PropertyListTy);
4087 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004088 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004089
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004090 // struct _objc_method {
4091 // SEL _cmd;
4092 // char *method_type;
4093 // char *_imp;
4094 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004095 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004096 Int8PtrTy,
4097 Int8PtrTy,
4098 NULL);
4099 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004100
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004101 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004102 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004103 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004104 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004105}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004106
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004107ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004108 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004109 // struct _objc_method_description {
4110 // SEL name;
4111 // char *types;
4112 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004113 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004114 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004115 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004116 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004117 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004118 MethodDescriptionTy);
4119
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004120 // struct _objc_method_description_list {
4121 // int count;
4122 // struct _objc_method_description[1];
4123 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004125 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004126 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004127 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004128 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004129 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004130
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004131 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004132 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004133 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004134
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004135 // Protocol description structures
4136
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004137 // struct _objc_protocol_extension {
4138 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4139 // struct _objc_method_description_list *optional_instance_methods;
4140 // struct _objc_method_description_list *optional_class_methods;
4141 // struct _objc_property_list *instance_properties;
4142 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004143 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004144 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004145 MethodDescriptionListPtrTy,
4146 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004147 PropertyListPtrTy,
4148 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004149 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004150 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004151
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004152 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004153 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004154
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004155 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004156
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004157 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4158 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004159
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004160 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004161 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004162 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004163 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004164 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004165 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004166 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4167
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004168 // struct _objc_protocol {
4169 // struct _objc_protocol_extension *isa;
4170 // char *protocol_name;
4171 // struct _objc_protocol **_objc_protocol_list;
4172 // struct _objc_method_description_list *instance_methods;
4173 // struct _objc_method_description_list *class_methods;
4174 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004175 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004176 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004178 MethodDescriptionListPtrTy,
4179 MethodDescriptionListPtrTy,
4180 NULL);
4181 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4182
4183 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004184 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004185 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004186 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004187 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004188
4189 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004190 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004191 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004192
4193 // Class description structures
4194
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004195 // struct _objc_ivar {
4196 // char *ivar_name;
4197 // char *ivar_type;
4198 // int ivar_offset;
4199 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004200 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201 Int8PtrTy,
4202 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004203 NULL);
4204 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4205
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004206 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004207 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004208 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004209 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004210
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004211 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004212 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004213 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004214 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004215
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004216 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004217 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004218 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004219 Int8PtrTy,
4220 PropertyListPtrTy,
4221 NULL);
4222 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004223 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004224
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004225 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004226
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004227 // struct _objc_class {
4228 // Class isa;
4229 // Class super_class;
4230 // char *name;
4231 // long version;
4232 // long info;
4233 // long instance_size;
4234 // struct _objc_ivar_list *ivars;
4235 // struct _objc_method_list *methods;
4236 // struct _objc_cache *cache;
4237 // struct _objc_protocol_list *protocols;
4238 // char *ivar_layout;
4239 // struct _objc_class_ext *ext;
4240 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004241 T = llvm::StructType::get(VMContext,
4242 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004243 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004244 Int8PtrTy,
4245 LongTy,
4246 LongTy,
4247 LongTy,
4248 IvarListPtrTy,
4249 MethodListPtrTy,
4250 CachePtrTy,
4251 ProtocolListPtrTy,
4252 Int8PtrTy,
4253 ClassExtensionPtrTy,
4254 NULL);
4255 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004256
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004257 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4258 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004259 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004260
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004261 // struct _objc_category {
4262 // char *category_name;
4263 // char *class_name;
4264 // struct _objc_method_list *instance_method;
4265 // struct _objc_method_list *class_method;
4266 // uint32_t size; // sizeof(struct _objc_category)
4267 // struct _objc_property_list *instance_properties;// category's @property
4268 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004269 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004270 Int8PtrTy,
4271 MethodListPtrTy,
4272 MethodListPtrTy,
4273 ProtocolListPtrTy,
4274 IntTy,
4275 PropertyListPtrTy,
4276 NULL);
4277 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4278
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004279 // Global metadata structures
4280
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004281 // struct _objc_symtab {
4282 // long sel_ref_cnt;
4283 // SEL *refs;
4284 // short cls_def_cnt;
4285 // short cat_def_cnt;
4286 // char *defs[cls_def_cnt + cat_def_cnt];
4287 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004288 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289 SelectorPtrTy,
4290 ShortTy,
4291 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004292 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004293 NULL);
4294 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004295 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004296
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004297 // struct _objc_module {
4298 // long version;
4299 // long size; // sizeof(struct _objc_module)
4300 // char *name;
4301 // struct _objc_symtab* symtab;
4302 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004303 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004304 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004305 LongTy,
4306 Int8PtrTy,
4307 SymtabPtrTy,
4308 NULL);
4309 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004310
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004311
Mike Stumpf5408fe2009-05-16 07:57:57 +00004312 // FIXME: This is the size of the setjmp buffer and should be target
4313 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004314 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004315
Anders Carlsson124526b2008-09-09 10:10:21 +00004316 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004317 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004318 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004319
4320 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004321 llvm::StructType::get(VMContext,
4322 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4323 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004324 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004325 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004326 ExceptionDataTy);
4327
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004328}
4329
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004331 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004332 // struct _method_list_t {
4333 // uint32_t entsize; // sizeof(struct _objc_method)
4334 // uint32_t method_count;
4335 // struct _objc_method method_list[method_count];
4336 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004337 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004338 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004339 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004340 NULL);
4341 CGM.getModule().addTypeName("struct.__method_list_t",
4342 MethodListnfABITy);
4343 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004344 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004345
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004346 // struct _protocol_t {
4347 // id isa; // NULL
4348 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004349 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004350 // const struct method_list_t * const instance_methods;
4351 // const struct method_list_t * const class_methods;
4352 // const struct method_list_t *optionalInstanceMethods;
4353 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004354 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004355 // const uint32_t size; // sizeof(struct _protocol_t)
4356 // const uint32_t flags; // = 0
4357 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004358
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004359 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004360 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004361
Owen Anderson47a434f2009-08-05 23:18:46 +00004362 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004363 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004364 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004365 ProtocolListTyHolder),
4366 MethodListnfABIPtrTy,
4367 MethodListnfABIPtrTy,
4368 MethodListnfABIPtrTy,
4369 MethodListnfABIPtrTy,
4370 PropertyListPtrTy,
4371 IntTy,
4372 IntTy,
4373 NULL);
4374 CGM.getModule().addTypeName("struct._protocol_t",
4375 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004376
4377 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004378 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004379
Fariborz Jahanianda320092009-01-29 19:24:30 +00004380 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004381 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004382 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004383 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004384 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004385 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004386 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004387 NULL);
4388 CGM.getModule().addTypeName("struct._objc_protocol_list",
4389 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004390 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004391 ProtocolListnfABITy);
4392
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004393 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004394 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004395
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004396 // struct _ivar_t {
4397 // unsigned long int *offset; // pointer to ivar offset location
4398 // char *name;
4399 // char *type;
4400 // uint32_t alignment;
4401 // uint32_t size;
4402 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004403 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004404 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004405 Int8PtrTy,
4406 Int8PtrTy,
4407 IntTy,
4408 IntTy,
4409 NULL);
4410 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004412 // struct _ivar_list_t {
4413 // uint32 entsize; // sizeof(struct _ivar_t)
4414 // uint32 count;
4415 // struct _iver_t list[count];
4416 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004417 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004418 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004419 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004420 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004421 NULL);
4422 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423
Owen Anderson96e0fc72009-07-29 22:16:19 +00004424 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004425
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004426 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004427 // uint32_t const flags;
4428 // uint32_t const instanceStart;
4429 // uint32_t const instanceSize;
4430 // uint32_t const reserved; // only when building for 64bit targets
4431 // const uint8_t * const ivarLayout;
4432 // const char *const name;
4433 // const struct _method_list_t * const baseMethods;
4434 // const struct _objc_protocol_list *const baseProtocols;
4435 // const struct _ivar_list_t *const ivars;
4436 // const uint8_t * const weakIvarLayout;
4437 // const struct _prop_list_t * const properties;
4438 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004439
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004440 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004441 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004442 IntTy,
4443 IntTy,
4444 Int8PtrTy,
4445 Int8PtrTy,
4446 MethodListnfABIPtrTy,
4447 ProtocolListnfABIPtrTy,
4448 IvarListnfABIPtrTy,
4449 Int8PtrTy,
4450 PropertyListPtrTy,
4451 NULL);
4452 CGM.getModule().addTypeName("struct._class_ro_t",
4453 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004454
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004455 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4456 std::vector<const llvm::Type*> Params;
4457 Params.push_back(ObjectPtrTy);
4458 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004459 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004460 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4461
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004462 // struct _class_t {
4463 // struct _class_t *isa;
4464 // struct _class_t * const superclass;
4465 // void *cache;
4466 // IMP *vtable;
4467 // struct class_ro_t *ro;
4468 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004469
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004470 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004471 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004472 llvm::StructType::get(VMContext,
4473 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474 llvm::PointerType::getUnqual(ClassTyHolder),
4475 CachePtrTy,
4476 llvm::PointerType::getUnqual(ImpnfABITy),
4477 llvm::PointerType::getUnqual(ClassRonfABITy),
4478 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004479 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4480
4481 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004482 ClassnfABITy);
4483
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004484 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004485 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004486
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004487 // struct _category_t {
4488 // const char * const name;
4489 // struct _class_t *const cls;
4490 // const struct _method_list_t * const instance_methods;
4491 // const struct _method_list_t * const class_methods;
4492 // const struct _protocol_list_t * const protocols;
4493 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004494 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004495 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004496 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004497 MethodListnfABIPtrTy,
4498 MethodListnfABIPtrTy,
4499 ProtocolListnfABIPtrTy,
4500 PropertyListPtrTy,
4501 NULL);
4502 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004503
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004504 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004505 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4506 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004507
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004508 // MessageRefTy - LLVM for:
4509 // struct _message_ref_t {
4510 // IMP messenger;
4511 // SEL name;
4512 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004513
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004514 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004515 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004516 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004517 SourceLocation(),
4518 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004519 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004520 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004521 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004522 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004523 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004525 MessageRefCTy = Ctx.getTagDeclType(RD);
4526 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4527 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004528
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004529 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004530 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004532 // SuperMessageRefTy - LLVM for:
4533 // struct _super_message_ref_t {
4534 // SUPER_IMP messenger;
4535 // SEL name;
4536 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004537 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004538 SelectorPtrTy,
4539 NULL);
4540 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004541
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004542 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004543 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4544
Daniel Dunbare588b992009-03-01 04:46:24 +00004545
4546 // struct objc_typeinfo {
4547 // const void** vtable; // objc_ehtype_vtable + 2
4548 // const char* name; // c++ typeinfo string
4549 // Class cls;
4550 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004551 EHTypeTy = llvm::StructType::get(VMContext,
4552 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004553 Int8PtrTy,
4554 ClassnfABIPtrTy,
4555 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004556 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004557 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004558}
4559
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004560llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004561 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004562
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004563 return NULL;
4564}
4565
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566void CGObjCNonFragileABIMac::AddModuleClassList(const
4567 std::vector<llvm::GlobalValue*>
4568 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004569 const char *SymbolName,
4570 const char *SectionName) {
4571 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004572
Daniel Dunbar463b8762009-05-15 21:48:48 +00004573 if (!NumClasses)
4574 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575
Daniel Dunbar463b8762009-05-15 21:48:48 +00004576 std::vector<llvm::Constant*> Symbols(NumClasses);
4577 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004578 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004579 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004580 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004581 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004582 NumClasses),
4583 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584
Daniel Dunbar463b8762009-05-15 21:48:48 +00004585 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004586 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004587 llvm::GlobalValue::InternalLinkage,
4588 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004589 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004590 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004591 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004592 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004593}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004595void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4596 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004597
Daniel Dunbar463b8762009-05-15 21:48:48 +00004598 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004599 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004600 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004601 "\01L_OBJC_LABEL_CLASS_$",
4602 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004603
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004604 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4605 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4606 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4607 continue;
4608 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004609 }
4610
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004611 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4612 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4613 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4614 continue;
4615 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4616 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004617
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004619 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4620 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004622 // Build list of all implemented category addresses in array
4623 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004624 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004625 "\01L_OBJC_LABEL_CATEGORY_$",
4626 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004627 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004628 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4629 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004630
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004631 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004632}
4633
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004634/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004635/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004636/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004637/// message dispatch call for all the rest.
4638///
4639bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004640 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4641 default:
4642 assert(0 && "Invalid dispatch method!");
4643 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004644 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004645 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004646 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004647 case CodeGenOptions::Mixed:
4648 break;
4649 }
4650
4651 // If so, see whether this selector is in the white-list of things which must
4652 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004653 if (NonLegacyDispatchMethods.empty()) {
4654 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4655 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4656 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4657 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4658 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4659 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4660 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4661 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4662 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4663 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004664
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004665 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4666 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4667 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4668 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4669 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4670 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4671 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4672 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004674 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4676 &CGM.getContext().Idents.get("objects"),
4677 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004678 };
4679 NonLegacyDispatchMethods.insert(
4680 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004681 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004682
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004683 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004684}
4685
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004686// Metadata flags
4687enum MetaDataDlags {
4688 CLS = 0x0,
4689 CLS_META = 0x1,
4690 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004691 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004692 CLS_EXCEPTION = 0x20
4693};
4694/// BuildClassRoTInitializer - generate meta-data for:
4695/// struct _class_ro_t {
4696/// uint32_t const flags;
4697/// uint32_t const instanceStart;
4698/// uint32_t const instanceSize;
4699/// uint32_t const reserved; // only when building for 64bit targets
4700/// const uint8_t * const ivarLayout;
4701/// const char *const name;
4702/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004703/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004704/// const struct _ivar_list_t *const ivars;
4705/// const uint8_t * const weakIvarLayout;
4706/// const struct _prop_list_t * const properties;
4707/// }
4708///
4709llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710 unsigned flags,
4711 unsigned InstanceStart,
4712 unsigned InstanceSize,
4713 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004714 std::string ClassName = ID->getNameAsString();
4715 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004716 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4717 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4718 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004719 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004720 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4721 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004722 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004723 // const struct _method_list_t * const baseMethods;
4724 std::vector<llvm::Constant*> Methods;
4725 std::string MethodListName("\01l_OBJC_$_");
4726 if (flags & CLS_META) {
4727 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004728 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004729 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004730 // Class methods should always be defined.
4731 Methods.push_back(GetMethodConstant(*i));
4732 }
4733 } else {
4734 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004736 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004737 // Instance methods should always be defined.
4738 Methods.push_back(GetMethodConstant(*i));
4739 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004740 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004741 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004742 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004744 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4745 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004746
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004747 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4748 if (llvm::Constant *C = GetMethodConstant(MD))
4749 Methods.push_back(C);
4750 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4751 if (llvm::Constant *C = GetMethodConstant(MD))
4752 Methods.push_back(C);
4753 }
4754 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004755 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004756 Values[ 5] = EmitMethodList(MethodListName,
4757 "__DATA, __objc_const", Methods);
4758
Fariborz Jahanianda320092009-01-29 19:24:30 +00004759 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4760 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004761 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004762 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004763 OID->protocol_begin(),
4764 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004765
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004766 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004767 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004768 else
4769 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4771 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004772 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004773 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004774 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004775 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4776 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004777 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004778 Values);
4779 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004780 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4781 llvm::GlobalValue::InternalLinkage,
4782 Init,
4783 (flags & CLS_META) ?
4784 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4785 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004786 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004787 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004788 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004789 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004790
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004791}
4792
4793/// BuildClassMetaData - This routine defines that to-level meta-data
4794/// for the given ClassName for:
4795/// struct _class_t {
4796/// struct _class_t *isa;
4797/// struct _class_t * const superclass;
4798/// void *cache;
4799/// IMP *vtable;
4800/// struct class_ro_t *ro;
4801/// }
4802///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004803llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004804 std::string &ClassName,
4805 llvm::Constant *IsAGV,
4806 llvm::Constant *SuperClassGV,
4807 llvm::Constant *ClassRoGV,
4808 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004809 std::vector<llvm::Constant*> Values(5);
4810 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004811 Values[1] = SuperClassGV;
4812 if (!Values[1])
4813 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004814 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4815 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4816 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004817 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004818 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004819 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4820 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004821 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004822 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004823 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004824 if (HiddenVisibility)
4825 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004826 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004827}
4828
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004829bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004830CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004831 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004832}
4833
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004834void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004835 uint32_t &InstanceStart,
4836 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004837 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004838 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004839
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004840 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004841 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004842
4843 // If there are no fields, the start is the same as the end.
4844 if (!RL.getFieldCount())
4845 InstanceStart = InstanceSize;
4846 else
4847 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004848}
4849
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004850void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4851 std::string ClassName = ID->getNameAsString();
4852 if (!ObjCEmptyCacheVar) {
4853 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004854 CGM.getModule(),
4855 ObjCTypes.CacheTy,
4856 false,
4857 llvm::GlobalValue::ExternalLinkage,
4858 0,
4859 "_objc_empty_cache");
4860
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004861 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862 CGM.getModule(),
4863 ObjCTypes.ImpnfABITy,
4864 false,
4865 llvm::GlobalValue::ExternalLinkage,
4866 0,
4867 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004868 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004869 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004870 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004871 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004873 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004874 uint32_t InstanceSize = InstanceStart;
4875 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004876 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4877 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004879 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004880
4881 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004882 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004883 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004884 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004885 if (ID->getNumIvarInitializers())
4886 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004887 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004888 // class is root
4889 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004890 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004891 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004892 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004893 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004894 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4895 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4896 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004897 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004898 if (Root->hasAttr<WeakImportAttr>())
4899 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004900 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004901 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004902 ObjCMetaClassName +
4903 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004904 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004905 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4906 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004907 }
4908 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4909 InstanceStart,
4910 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004911 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004912 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004913 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4914 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004915 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004916
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004917 // Metadata for the class
4918 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004919 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004920 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004921 if (ID->getNumIvarInitializers())
4922 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004923
Douglas Gregor68584ed2009-06-18 16:11:24 +00004924 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004925 flags |= CLS_EXCEPTION;
4926
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004927 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004928 flags |= CLS_ROOT;
4929 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004930 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004931 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004932 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004933 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004934 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004935 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4936 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004937 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004938 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004939 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004940 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004941 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004942 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004943
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004944 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004945 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004946 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4947 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004948 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004949
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004950 // Determine if this class is also "non-lazy".
4951 if (ImplementationIsNonLazy(ID))
4952 DefinedNonLazyClasses.push_back(ClassMD);
4953
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004954 // Force the definition of the EHType if necessary.
4955 if (flags & CLS_EXCEPTION)
4956 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004957}
4958
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004959/// GenerateProtocolRef - This routine is called to generate code for
4960/// a protocol reference expression; as in:
4961/// @code
4962/// @protocol(Proto1);
4963/// @endcode
4964/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4965/// which will hold address of the protocol meta-data.
4966///
4967llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968 const ObjCProtocolDecl *PD) {
4969
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004970 // This routine is called for @protocol only. So, we must build definition
4971 // of protocol's meta-data (not a reference to it!)
4972 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004973 llvm::Constant *Init =
4974 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4975 ObjCTypes.ExternalProtocolPtrTy);
4976
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004977 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4978 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004979
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004980 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4981 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004982 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004983 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004984 CGM.getModule(),
4985 Init->getType(), false,
4986 llvm::GlobalValue::WeakAnyLinkage,
4987 Init,
4988 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004989 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4990 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004991 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004992 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004993}
4994
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004995/// GenerateCategory - Build metadata for a category implementation.
4996/// struct _category_t {
4997/// const char * const name;
4998/// struct _class_t *const cls;
4999/// const struct _method_list_t * const instance_methods;
5000/// const struct _method_list_t * const class_methods;
5001/// const struct _protocol_list_t * const protocols;
5002/// const struct _prop_list_t * const properties;
5003/// }
5004///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005005void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005006 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005007 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5009 "_$_" + OCD->getNameAsString());
5010 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005011 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005013 std::vector<llvm::Constant*> Values(6);
5014 Values[0] = GetClassName(OCD->getIdentifier());
5015 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005016 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005017 if (Interface->hasAttr<WeakImportAttr>())
5018 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5019
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005020 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005021 std::vector<llvm::Constant*> Methods;
5022 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005023 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005024 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025
5026 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005027 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005028 // Instance methods should always be defined.
5029 Methods.push_back(GetMethodConstant(*i));
5030 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005031
5032 Values[2] = EmitMethodList(MethodListName,
5033 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005034 Methods);
5035
5036 MethodListName = Prefix;
5037 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5038 OCD->getNameAsString();
5039 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005041 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005042 // Class methods should always be defined.
5043 Methods.push_back(GetMethodConstant(*i));
5044 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005045
5046 Values[3] = EmitMethodList(MethodListName,
5047 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005048 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005049 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005050 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005051 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005052 llvm::SmallString<256> ExtName;
5053 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5054 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005055 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005056 + Interface->getName() + "_$_"
5057 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005058 Category->protocol_begin(),
5059 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005060 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5061 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005062 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005063 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5064 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005065 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005066
5067 llvm::Constant *Init =
5068 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005069 Values);
5070 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005071 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005072 false,
5073 llvm::GlobalValue::InternalLinkage,
5074 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005075 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005076 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005077 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005078 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005079 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005080 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005081
5082 // Determine if this category is also "non-lazy".
5083 if (ImplementationIsNonLazy(OCD))
5084 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005085}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005086
5087/// GetMethodConstant - Return a struct objc_method constant for the
5088/// given method if it has been defined. The result is null if the
5089/// method has not been defined. The return value has type MethodPtrTy.
5090llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005091 const ObjCMethodDecl *MD) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005092 // FIXME: Use DenseMap::lookup
5093 llvm::Function *Fn = MethodDefinitions[MD];
5094 if (!Fn)
5095 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005097 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005098 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005099 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005100 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005101 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005102 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005103 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005104}
5105
5106/// EmitMethodList - Build meta-data for method declarations
5107/// struct _method_list_t {
5108/// uint32_t entsize; // sizeof(struct _objc_method)
5109/// uint32_t method_count;
5110/// struct _objc_method method_list[method_count];
5111/// }
5112///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005113llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5114 const char *Section,
5115 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005116 // Return null for empty list.
5117 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005118 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005119
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005120 std::vector<llvm::Constant*> Values(3);
5121 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005122 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005123 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005124 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005125 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005126 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005127 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005128 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005129 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005130
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005131 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005132 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005133 llvm::GlobalValue::InternalLinkage,
5134 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005135 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005136 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005137 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005138 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005139 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005140 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005141 ObjCTypes.MethodListnfABIPtrTy);
5142}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005143
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005144/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5145/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005146llvm::GlobalVariable *
5147CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5148 const ObjCIvarDecl *Ivar) {
5149 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005150 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005151 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005152 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005153 CGM.getModule().getGlobalVariable(Name);
5154 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005155 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005156 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005157 false,
5158 llvm::GlobalValue::ExternalLinkage,
5159 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005160 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005161 return IvarOffsetGV;
5162}
5163
Daniel Dunbare83be122010-04-02 21:14:02 +00005164llvm::Constant *
5165CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5166 const ObjCIvarDecl *Ivar,
5167 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005168 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005169 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005170 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005171 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005172 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005173
Mike Stumpf5408fe2009-05-16 07:57:57 +00005174 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5175 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005176 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5177 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5178 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005179 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005180 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005181 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005182 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005183 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005184}
5185
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005186/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005187/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005188/// IvarListnfABIPtrTy.
5189/// struct _ivar_t {
5190/// unsigned long int *offset; // pointer to ivar offset location
5191/// char *name;
5192/// char *type;
5193/// uint32_t alignment;
5194/// uint32_t size;
5195/// }
5196/// struct _ivar_list_t {
5197/// uint32 entsize; // sizeof(struct _ivar_t)
5198/// uint32 count;
5199/// struct _iver_t list[count];
5200/// }
5201///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005202
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005203llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005204 const ObjCImplementationDecl *ID) {
5205
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005206 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005207
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005208 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5209 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005210
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005211 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212
Daniel Dunbar91636d62009-04-20 00:33:43 +00005213 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005214 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005215 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005216
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005217 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5218 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005219 // Ignore unnamed bit-fields.
5220 if (!IVD->getDeclName())
5221 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005222 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005223 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005224 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5225 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005226 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005227 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005228 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005229 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005231 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005232 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005233 // NOTE. Size of a bitfield does not match gcc's, because of the
5234 // way bitfields are treated special in each. But I am told that
5235 // 'size' for bitfield ivars is ignored by the runtime so it does
5236 // not matter. If it matters, there is enough info to get the
5237 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005238 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005239 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005240 }
5241 // Return null for empty list.
5242 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005243 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005244 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005245 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005246 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5247 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005248 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005249 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005250 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005251 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005252 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5253 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005254 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005255 llvm::GlobalValue::InternalLinkage,
5256 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005257 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005258 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005259 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005260 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005261
Chris Lattnerad64e022009-07-17 23:57:13 +00005262 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005263 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005264}
5265
5266llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005267 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005268 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005269
Fariborz Jahanianda320092009-01-29 19:24:30 +00005270 if (!Entry) {
5271 // We use the initializer as a marker of whether this is a forward
5272 // reference or not. At module finalization we add the empty
5273 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005274 Entry =
5275 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5276 llvm::GlobalValue::ExternalLinkage,
5277 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005278 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005279 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005280 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005281
Fariborz Jahanianda320092009-01-29 19:24:30 +00005282 return Entry;
5283}
5284
5285/// GetOrEmitProtocol - Generate the protocol meta-data:
5286/// @code
5287/// struct _protocol_t {
5288/// id isa; // NULL
5289/// const char * const protocol_name;
5290/// const struct _protocol_list_t * protocol_list; // super protocols
5291/// const struct method_list_t * const instance_methods;
5292/// const struct method_list_t * const class_methods;
5293/// const struct method_list_t *optionalInstanceMethods;
5294/// const struct method_list_t *optionalClassMethods;
5295/// const struct _prop_list_t * properties;
5296/// const uint32_t size; // sizeof(struct _protocol_t)
5297/// const uint32_t flags; // = 0
5298/// }
5299/// @endcode
5300///
5301
5302llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005304 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005305
Fariborz Jahanianda320092009-01-29 19:24:30 +00005306 // Early exit if a defining object has already been generated.
5307 if (Entry && Entry->hasInitializer())
5308 return Entry;
5309
Fariborz Jahanianda320092009-01-29 19:24:30 +00005310 // Construct method lists.
5311 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5312 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005313 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005314 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005315 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005316 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005317 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5318 OptInstanceMethods.push_back(C);
5319 } else {
5320 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005322 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005323
5324 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005325 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005326 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005327 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005328 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5329 OptClassMethods.push_back(C);
5330 } else {
5331 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005332 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005333 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334
Fariborz Jahanianda320092009-01-29 19:24:30 +00005335 std::vector<llvm::Constant*> Values(10);
5336 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005337 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005338 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005339 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5340 PD->protocol_begin(),
5341 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005343 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005344 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005345 "__DATA, __objc_const",
5346 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005348 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005349 "__DATA, __objc_const",
5350 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005351 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005352 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005353 "__DATA, __objc_const",
5354 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005355 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005356 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005357 "__DATA, __objc_const",
5358 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005359 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005360 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005361 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005362 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005363 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005364 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005365 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367
Fariborz Jahanianda320092009-01-29 19:24:30 +00005368 if (Entry) {
5369 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005370 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005371 Entry->setInitializer(Init);
5372 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005373 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005374 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5375 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5376 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005377 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005378 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005381 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005382 CGM.AddUsedGlobal(Entry);
5383
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005384 // Use this protocol meta-data to build protocol list table in section
5385 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005386 llvm::GlobalVariable *PTGV =
5387 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5388 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5389 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005390 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005391 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005392 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005393 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005394 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005395 return Entry;
5396}
5397
5398/// EmitProtocolList - Generate protocol list meta-data:
5399/// @code
5400/// struct _protocol_list_t {
5401/// long protocol_count; // Note, this is 32/64 bit
5402/// struct _protocol_t[protocol_count];
5403/// }
5404/// @endcode
5405///
5406llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005407CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5408 ObjCProtocolDecl::protocol_iterator begin,
5409 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005411
Fariborz Jahanianda320092009-01-29 19:24:30 +00005412 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005413 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005414 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005415
Daniel Dunbar948e2582009-02-15 07:36:20 +00005416 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005417 llvm::SmallString<256> TmpName;
5418 Name.toVector(TmpName);
5419 llvm::GlobalVariable *GV =
5420 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005421 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005422 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005423
Daniel Dunbar948e2582009-02-15 07:36:20 +00005424 for (; begin != end; ++begin)
5425 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5426
Fariborz Jahanianda320092009-01-29 19:24:30 +00005427 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005428 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005429 ObjCTypes.ProtocolnfABIPtrTy));
5430
Fariborz Jahanianda320092009-01-29 19:24:30 +00005431 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005432 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005433 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005434 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005435 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005436 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005437 ProtocolRefs.size()),
5438 ProtocolRefs);
5439
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005440 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005441 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005442 llvm::GlobalValue::InternalLinkage,
5443 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005444 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005445 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005446 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005447 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005448 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005450 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005451}
5452
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005453/// GetMethodDescriptionConstant - This routine build following meta-data:
5454/// struct _objc_method {
5455/// SEL _cmd;
5456/// char *method_type;
5457/// char *_imp;
5458/// }
5459
5460llvm::Constant *
5461CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5462 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005463 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005464 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5465 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005466 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005467 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005468 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005469 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005470}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005471
5472/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5473/// This code gen. amounts to generating code for:
5474/// @code
5475/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5476/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005478LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005479 CodeGen::CodeGenFunction &CGF,
5480 QualType ObjectTy,
5481 llvm::Value *BaseValue,
5482 const ObjCIvarDecl *Ivar,
5483 unsigned CVRQualifiers) {
5484 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005485 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5486 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005487}
5488
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005489llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005490 CodeGen::CodeGenFunction &CGF,
5491 const ObjCInterfaceDecl *Interface,
5492 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005493 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005494}
5495
Fariborz Jahanian46551122009-02-04 00:22:57 +00005496CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005497 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005498 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 QualType ResultType,
5500 Selector Sel,
5501 llvm::Value *Receiver,
5502 QualType Arg0Ty,
5503 bool IsSuper,
5504 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005505 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5506 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005507 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005508 llvm::Value *Arg0 = Receiver;
5509 if (!IsSuper)
5510 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005512 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005513 // FIXME. This is too much work to get the ABI-specific result type needed to
5514 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005515 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005516 = Types.getFunctionInfo(ResultType, CallArgList(),
5517 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005518 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005519 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005520 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005521#if 0
5522 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005523 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005524 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005525 // FIXME. Is there a better way of getting these names.
5526 // They are available in RuntimeFunctions vector pair.
5527 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005528 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005529#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005530 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005531 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005532 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005533 } else {
5534 Fn = ObjCTypes.getMessageSendStretFixupFn();
5535 Name += "objc_msgSend_stret_fixup";
5536 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005537 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5538 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5539 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005540 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005541#if 0
5542// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005543 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005544 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005545 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005547#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005548 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005549 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005550 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005551 } else {
5552 Fn = ObjCTypes.getMessageSendFixupFn();
5553 Name += "objc_msgSend_fixup";
5554 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005555 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005556 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005557 Name += '_';
5558 std::string SelName(Sel.getAsString());
5559 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005560 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005561 if (SelName[i] == ':')
5562 SelName[i] = '_';
5563 Name += SelName;
5564 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5565 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005566 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005567 std::vector<llvm::Constant*> Values(2);
5568 Values[0] = Fn;
5569 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005570 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005571 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005572 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005573 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005574 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005575 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005576 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005577 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005578 }
5579 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005580
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005581 CallArgList ActualArgs;
5582 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005583 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005584 ObjCTypes.MessageRefCPtrTy));
5585 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005586 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005587 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005588 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5589 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005590 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005591 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005592 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005593 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005594}
5595
5596/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005597CodeGen::RValue
5598CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005599 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005600 QualType ResultType,
5601 Selector Sel,
5602 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005603 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005604 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005605 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005606 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005607 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5608 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005610 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005611 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005612 Receiver, CGF.getContext().getObjCIdType(),
5613 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005614}
5615
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005616llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005617CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005618 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5619
Daniel Dunbardfff2302009-03-02 05:18:14 +00005620 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005621 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005622 false, llvm::GlobalValue::ExternalLinkage,
5623 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005624 }
5625
5626 return GV;
5627}
5628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005629llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5630 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005631 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005632
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005633 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005634 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005635 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005636 Entry =
5637 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005638 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005640 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005641 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005642 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005643 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005644 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005645 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005646 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005647
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005648 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005649}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005650
Daniel Dunbar11394522009-04-18 08:51:00 +00005651llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005653 const ObjCInterfaceDecl *ID) {
5654 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005655
Daniel Dunbar11394522009-04-18 08:51:00 +00005656 if (!Entry) {
5657 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5658 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 Entry =
5660 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005661 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005662 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005663 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005664 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005665 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005666 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005667 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005668 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005669 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005670
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005671 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005672}
5673
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005674/// EmitMetaClassRef - Return a Value * of the address of _class_t
5675/// meta-data
5676///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005677llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5678 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005679 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5680 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005681 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005682
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005683 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005684 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005686 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005687 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005688 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005689 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005690 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005691 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692 ObjCTypes.ClassnfABIPtrTy));
5693
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005694 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005695 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005696
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005697 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005698}
5699
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005700/// GetClass - Return a reference to the class for the given interface
5701/// decl.
5702llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5703 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005704 if (ID->hasAttr<WeakImportAttr>()) {
5705 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5706 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5707 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5708 }
5709
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005710 return EmitClassRef(Builder, ID);
5711}
5712
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005713/// Generates a message send where the super is the receiver. This is
5714/// a message send to self with special delivery semantics indicating
5715/// which class's method should be called.
5716CodeGen::RValue
5717CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005718 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719 QualType ResultType,
5720 Selector Sel,
5721 const ObjCInterfaceDecl *Class,
5722 bool isCategoryImpl,
5723 llvm::Value *Receiver,
5724 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005725 const CodeGen::CallArgList &CallArgs,
5726 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005727 // ...
5728 // Create and init a super structure; this is a (receiver, class)
5729 // pair we will pass to objc_msgSendSuper.
5730 llvm::Value *ObjCSuper =
5731 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005733 llvm::Value *ReceiverAsObject =
5734 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5735 CGF.Builder.CreateStore(ReceiverAsObject,
5736 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005738 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005739 llvm::Value *Target;
5740 if (IsClassMessage) {
5741 if (isCategoryImpl) {
5742 // Message sent to "super' in a class method defined in
5743 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005744 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005745 Target = CGF.Builder.CreateStructGEP(Target, 0);
5746 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005747 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005748 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005749 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005750 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005751
Mike Stumpf5408fe2009-05-16 07:57:57 +00005752 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5753 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 const llvm::Type *ClassTy =
5755 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5756 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5757 CGF.Builder.CreateStore(Target,
5758 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005760 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005761 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5762 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005764 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005765 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005766 ObjCSuper, ObjCTypes.SuperPtrCTy,
5767 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005768}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005769
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005770llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005771 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005772 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005773
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005774 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005775 llvm::Constant *Casted =
5776 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5777 ObjCTypes.SelectorPtrTy);
5778 Entry =
5779 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5780 llvm::GlobalValue::InternalLinkage,
5781 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005782 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005783 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005784 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005785
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005786 if (lval)
5787 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005788 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005789}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005790/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005791/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005792///
5793void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005794 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005795 llvm::Value *dst,
5796 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005797 const llvm::Type * SrcTy = src->getType();
5798 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005799 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005800 assert(Size <= 8 && "does not support size > 8");
5801 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5802 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005803 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5804 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005805 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5806 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005807 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5808 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005809 return;
5810}
5811
5812/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5813/// objc_assign_strongCast (id src, id *dst)
5814///
5815void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005816 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005817 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005818 const llvm::Type * SrcTy = src->getType();
5819 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005820 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005821 assert(Size <= 8 && "does not support size > 8");
5822 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005823 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005824 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5825 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005826 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5827 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005828 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005829 src, dst, "weakassign");
5830 return;
5831}
5832
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005833void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834 CodeGen::CodeGenFunction &CGF,
5835 llvm::Value *DestPtr,
5836 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005837 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005838 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5839 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005840 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005841 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005842 return;
5843}
5844
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005845/// EmitObjCWeakRead - Code gen for loading value of a __weak
5846/// object: objc_read_weak (id *src)
5847///
5848llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005850 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005851 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005852 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5853 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005854 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005855 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005856 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005857 return read_weak;
5858}
5859
5860/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5861/// objc_assign_weak (id src, id *dst)
5862///
5863void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005864 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005865 const llvm::Type * SrcTy = src->getType();
5866 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005867 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005868 assert(Size <= 8 && "does not support size > 8");
5869 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5870 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005871 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5872 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5874 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005875 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005876 src, dst, "weakassign");
5877 return;
5878}
5879
5880/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5881/// objc_assign_global (id src, id *dst)
5882///
5883void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005884 llvm::Value *src, llvm::Value *dst,
5885 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005886 const llvm::Type * SrcTy = src->getType();
5887 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005888 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005889 assert(Size <= 8 && "does not support size > 8");
5890 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5891 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005892 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5893 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005894 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5895 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005896 if (!threadlocal)
5897 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5898 src, dst, "globalassign");
5899 else
5900 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5901 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005902 return;
5903}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005904
John McCall740e8072010-07-21 00:41:47 +00005905namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005906 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005907 llvm::Value *SyncExitFn;
5908 llvm::Value *SyncArg;
5909 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5910 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5911
5912 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5913 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5914 }
5915 };
5916}
5917
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005918void
John McCallf1549f62010-07-06 01:34:17 +00005919CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5920 const ObjCAtSynchronizedStmt &S) {
5921 // Evaluate the lock operand. This should dominate the cleanup.
5922 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005923
John McCallf1549f62010-07-06 01:34:17 +00005924 // Acquire the lock.
5925 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5926 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5927 ->setDoesNotThrow();
5928
5929 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005930 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5931 ObjCTypes.getSyncExitFn(),
5932 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005933
John McCallf1549f62010-07-06 01:34:17 +00005934 // Emit the body of the statement.
5935 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005936
John McCallf1549f62010-07-06 01:34:17 +00005937 // Pop the lock-release cleanup.
5938 CGF.PopCleanupBlock();
5939}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005940
John McCallf1549f62010-07-06 01:34:17 +00005941namespace {
5942 struct CatchHandler {
5943 const VarDecl *Variable;
5944 const Stmt *Body;
5945 llvm::BasicBlock *Block;
5946 llvm::Value *TypeInfo;
5947 };
John McCall8e3f8612010-07-13 22:12:14 +00005948
John McCall1f0fca52010-07-21 07:22:38 +00005949 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005950 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5951 MightThrow(MightThrow), Fn(Fn) {}
5952 bool MightThrow;
5953 llvm::Value *Fn;
5954
5955 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5956 if (!MightThrow) {
5957 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5958 return;
5959 }
5960
5961 CGF.EmitCallOrInvoke(Fn, 0, 0);
5962 }
5963 };
John McCallf1549f62010-07-06 01:34:17 +00005964}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005965
John McCall5a180392010-07-24 00:37:23 +00005966llvm::Constant *
5967CGObjCNonFragileABIMac::GetEHType(QualType T) {
5968 // There's a particular fixed type info for 'id'.
5969 if (T->isObjCIdType() ||
5970 T->isObjCQualifiedIdType()) {
5971 llvm::Constant *IDEHType =
5972 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5973 if (!IDEHType)
5974 IDEHType =
5975 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5976 false,
5977 llvm::GlobalValue::ExternalLinkage,
5978 0, "OBJC_EHTYPE_id");
5979 return IDEHType;
5980 }
5981
5982 // All other types should be Objective-C interface pointer types.
5983 const ObjCObjectPointerType *PT =
5984 T->getAs<ObjCObjectPointerType>();
5985 assert(PT && "Invalid @catch type.");
5986 const ObjCInterfaceType *IT = PT->getInterfaceType();
5987 assert(IT && "Invalid @catch type.");
5988 return GetInterfaceEHType(IT->getDecl(), false);
5989}
5990
John McCallf1549f62010-07-06 01:34:17 +00005991void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5992 const ObjCAtTryStmt &S) {
5993 // Jump destination for falling out of catch bodies.
5994 CodeGenFunction::JumpDest Cont;
5995 if (S.getNumCatchStmts())
5996 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005997
John McCallf1549f62010-07-06 01:34:17 +00005998 CodeGenFunction::FinallyInfo FinallyInfo;
5999 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6000 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6001 ObjCTypes.getObjCBeginCatchFn(),
6002 ObjCTypes.getObjCEndCatchFn(),
6003 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006004
John McCallf1549f62010-07-06 01:34:17 +00006005 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006006
John McCallf1549f62010-07-06 01:34:17 +00006007 // Enter the catch, if there is one.
6008 if (S.getNumCatchStmts()) {
6009 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6010 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006011 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006012
John McCallf1549f62010-07-06 01:34:17 +00006013 Handlers.push_back(CatchHandler());
6014 CatchHandler &Handler = Handlers.back();
6015 Handler.Variable = CatchDecl;
6016 Handler.Body = CatchStmt->getCatchBody();
6017 Handler.Block = CGF.createBasicBlock("catch");
6018
6019 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006020 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006021 Handler.TypeInfo = 0; // catch-all
6022 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006023 break;
6024 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006025
John McCall5a180392010-07-24 00:37:23 +00006026 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006027 }
John McCallf1549f62010-07-06 01:34:17 +00006028
6029 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6030 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6031 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006032 }
John McCallf1549f62010-07-06 01:34:17 +00006033
6034 // Emit the try body.
6035 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006036
John McCallf1549f62010-07-06 01:34:17 +00006037 // Leave the try.
6038 if (S.getNumCatchStmts())
6039 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006040
John McCallf1549f62010-07-06 01:34:17 +00006041 // Remember where we were.
6042 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006043
John McCallf1549f62010-07-06 01:34:17 +00006044 // Emit the handlers.
6045 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6046 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006047
John McCallf1549f62010-07-06 01:34:17 +00006048 CGF.EmitBlock(Handler.Block);
6049 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006050
John McCallf1549f62010-07-06 01:34:17 +00006051 // Enter the catch.
6052 llvm::CallInst *Exn =
6053 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6054 "exn.adjusted");
6055 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006056
John McCallf1549f62010-07-06 01:34:17 +00006057 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006058 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006059 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6060 EndCatchMightThrow,
6061 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006062
John McCallf1549f62010-07-06 01:34:17 +00006063 // Bind the catch parameter if it exists.
6064 if (const VarDecl *CatchParam = Handler.Variable) {
6065 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6066 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006067
John McCallf1549f62010-07-06 01:34:17 +00006068 CGF.EmitLocalBlockVarDecl(*CatchParam);
6069 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006070 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006071
John McCallf1549f62010-07-06 01:34:17 +00006072 CGF.ObjCEHValueStack.push_back(Exn);
6073 CGF.EmitStmt(Handler.Body);
6074 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006075
John McCallf1549f62010-07-06 01:34:17 +00006076 // Leave the earlier cleanup.
6077 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006078
John McCallf1549f62010-07-06 01:34:17 +00006079 CGF.EmitBranchThroughCleanup(Cont);
6080 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006081
John McCallf1549f62010-07-06 01:34:17 +00006082 // Go back to the try-statement fallthrough.
6083 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006084
John McCallf1549f62010-07-06 01:34:17 +00006085 // Pop out of the normal cleanup on the finally.
6086 if (S.getFinallyStmt())
6087 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006088
John McCallff8e1152010-07-23 21:56:41 +00006089 if (Cont.isValid())
6090 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006091}
6092
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006093/// EmitThrowStmt - Generate code for a throw statement.
6094void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6095 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006096 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006097 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006098 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006099 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006100 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006101 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006102 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006103 "Unexpected rethrow outside @catch block.");
6104 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006105 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006106 }
6107
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006108 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006109 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6110 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6111 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006112 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006113 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006114 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006115 } else {
6116 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6117 ->setDoesNotReturn();
6118 CGF.Builder.CreateUnreachable();
6119 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006120
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006121 // Clear the insertion point to indicate we are in unreachable code.
6122 CGF.Builder.ClearInsertionPoint();
6123}
Daniel Dunbare588b992009-03-01 04:46:24 +00006124
John McCall5a180392010-07-24 00:37:23 +00006125llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006126CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006127 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006128 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006129
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006130 // If we don't need a definition, return the entry if found or check
6131 // if we use an external reference.
6132 if (!ForDefinition) {
6133 if (Entry)
6134 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006135
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006136 // If this type (or a super class) has the __objc_exception__
6137 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006138 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006139 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006140 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006141 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006142 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006143 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006144 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006146
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006147 // Otherwise we need to either make a new entry or fill in the
6148 // initializer.
6149 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006150 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006151 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006152 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006153 CGM.getModule().getGlobalVariable(VTableName);
6154 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006155 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6156 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006157 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006158 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006159
Chris Lattner77b89b82010-06-27 07:15:29 +00006160 llvm::Value *VTableIdx =
6161 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006162
6163 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006164 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006165 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006166 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006167 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006168 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006169
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006170 if (Entry) {
6171 Entry->setInitializer(Init);
6172 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006173 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006174 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006175 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006176 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006177 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006178 }
6179
Daniel Dunbar04d40782009-04-14 06:00:08 +00006180 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006181 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006182 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6183 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006184
6185 if (ForDefinition) {
6186 Entry->setSection("__DATA,__objc_const");
6187 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6188 } else {
6189 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6190 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006191
6192 return Entry;
6193}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006194
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006195/* *** */
6196
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006197CodeGen::CGObjCRuntime *
6198CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006199 return new CGObjCMac(CGM);
6200}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006201
6202CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006203CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006204 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006205}