blob: eeb06d8fc83fb0e61c57e7d4c93a85a968b05548 [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
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000028#include "llvm/Intrinsics.h"
Owen Anderson69243822009-07-13 04:10:07 +000029#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000030#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000031#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000032#include "llvm/ADT/SetVector.h"
33#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000034#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000035#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000036#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000037#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000042
Daniel Dunbar97776872009-04-22 07:32:20 +000043// Common CGObjCRuntime functions, these don't belong here, but they
44// don't belong in CGObjCRuntime either so we will live with it for
45// now.
46
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000047static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
48 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000049 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000050 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000051 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000052
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000053 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
54 // in here; it should never be necessary because that should be the lexical
55 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000056
Daniel Dunbar532d4da2009-05-03 13:15:50 +000057 // If we know have an implementation (and the ivar is in it) then
58 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000059 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000060 if (ID && ID->getClassInterface() == Container)
61 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
62 else
63 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000064
65 // Compute field index.
66 //
67 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
68 // implemented. This should be fixed to get the information from the layout
69 // directly.
70 unsigned Index = 0;
71 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
72 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
73 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
74 if (Ivar == Ivars[k])
75 break;
76 ++Index;
77 }
78 assert(Index != Ivars.size() && "Ivar is not inside container!");
79
Daniel Dunbar532d4da2009-05-03 13:15:50 +000080 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000081}
82
83uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
84 const ObjCInterfaceDecl *OID,
85 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000086 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
87}
88
89uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
90 const ObjCImplementationDecl *OID,
91 const ObjCIvarDecl *Ivar) {
92 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000093}
94
95LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
96 const ObjCInterfaceDecl *OID,
97 llvm::Value *BaseValue,
98 const ObjCIvarDecl *Ivar,
99 unsigned CVRQualifiers,
100 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000101 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000102 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 QualType IvarTy = Ivar->getType();
104 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000105 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000107 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000108
John McCall0953e762009-09-24 19:53:00 +0000109 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
110 Quals.addCVRQualifiers(CVRQualifiers);
111
Daniel Dunbar56229f52010-04-05 16:20:33 +0000112 if (!Ivar->isBitField())
113 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000114
Daniel Dunbar56229f52010-04-05 16:20:33 +0000115 // We need to compute the bit offset for the bit-field, the offset is to the
116 // byte. Note, there is a subtle invariant here: we can only call this routine
117 // on non-synthesized ivars but we may be called for synthesized ivars.
118 // However, a synthesized ivar can never be a bit-field, so this is safe.
119 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000122
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000123 // Allocate a new CGBitFieldInfo object to describe this access.
124 //
125 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
126 // layout object. However, this is blocked on other cleanups to the
127 // Objective-C code, so for now we just live with allocating a bunch of these
128 // objects.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000129
Daniel Dunbarab970f92010-04-13 20:58:55 +0000130 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000131 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000132 AI.FieldIndex = 0;
133 AI.FieldByteOffset = 0;
134 AI.FieldBitStart = BitOffset;
135 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
136 AI.AccessAlignment = 0;
137 AI.TargetBitOffset = 0;
138 AI.TargetBitWidth = BitFieldSize;
139
Daniel Dunbar2df25692010-04-15 05:09:32 +0000140 CGBitFieldInfo *Info =
141 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
142 IvarTy->isSignedIntegerType());
143
Daniel Dunbar56229f52010-04-05 16:20:33 +0000144 // FIXME: We need to set a very conservative alignment on this, or make sure
145 // that the runtime is doing the right thing.
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000146 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000147}
148
149///
150
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000151namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000152
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000153typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000154
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155// FIXME: We should find a nicer way to make the labels for metadata, string
156// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000157
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000158class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000159protected:
160 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000161
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000162private:
163 llvm::Constant *getMessageSendFn() const {
164 // id objc_msgSend (id, SEL, ...)
165 std::vector<const llvm::Type*> Params;
166 Params.push_back(ObjectPtrTy);
167 Params.push_back(SelectorPtrTy);
168 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
170 Params, true),
171 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000172 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000173
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000174 llvm::Constant *getMessageSendStretFn() const {
175 // id objc_msgSend_stret (id, SEL, ...)
176 std::vector<const llvm::Type*> Params;
177 Params.push_back(ObjectPtrTy);
178 Params.push_back(SelectorPtrTy);
179 return
Owen Anderson0032b272009-08-13 21:57:51 +0000180 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181 Params, true),
182 "objc_msgSend_stret");
183
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000184 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000185
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000186 llvm::Constant *getMessageSendFpretFn() const {
187 // FIXME: This should be long double on x86_64?
188 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
189 std::vector<const llvm::Type*> Params;
190 Params.push_back(ObjectPtrTy);
191 Params.push_back(SelectorPtrTy);
192 return
Owen Anderson0032b272009-08-13 21:57:51 +0000193 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
194 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000195 Params,
196 true),
197 "objc_msgSend_fpret");
198
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000199 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000200
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000201 llvm::Constant *getMessageSendSuperFn() const {
202 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
203 const char *SuperName = "objc_msgSendSuper";
204 std::vector<const llvm::Type*> Params;
205 Params.push_back(SuperPtrTy);
206 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000207 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000208 Params, true),
209 SuperName);
210 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000211
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000212 llvm::Constant *getMessageSendSuperFn2() const {
213 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
214 const char *SuperName = "objc_msgSendSuper2";
215 std::vector<const llvm::Type*> Params;
216 Params.push_back(SuperPtrTy);
217 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000218 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000219 Params, true),
220 SuperName);
221 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000222
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000223 llvm::Constant *getMessageSendSuperStretFn() const {
224 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
225 // SEL op, ...)
226 std::vector<const llvm::Type*> Params;
227 Params.push_back(Int8PtrTy);
228 Params.push_back(SuperPtrTy);
229 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000230 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000231 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000232 Params, true),
233 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000234 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000235
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000236 llvm::Constant *getMessageSendSuperStretFn2() const {
237 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
238 // SEL op, ...)
239 std::vector<const llvm::Type*> Params;
240 Params.push_back(Int8PtrTy);
241 Params.push_back(SuperPtrTy);
242 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000243 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000244 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000245 Params, true),
246 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000247 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000248
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000249 llvm::Constant *getMessageSendSuperFpretFn() const {
250 // There is no objc_msgSendSuper_fpret? How can that work?
251 return getMessageSendSuperFn();
252 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000253
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000254 llvm::Constant *getMessageSendSuperFpretFn2() const {
255 // There is no objc_msgSendSuper_fpret? How can that work?
256 return getMessageSendSuperFn2();
257 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000258
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000259protected:
260 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000261
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000262public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000263 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000264 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000265
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000266 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
267 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000268
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000269 /// PtrObjectPtrTy - LLVM type for id *
270 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000271
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000272 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000273 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000274 /// ProtocolPtrTy - LLVM type for external protocol handles
275 /// (typeof(Protocol))
276 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000277
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000278 // SuperCTy - clang type for struct objc_super.
279 QualType SuperCTy;
280 // SuperPtrCTy - clang type for struct objc_super *.
281 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000282
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000283 /// SuperTy - LLVM type for struct objc_super.
284 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000285 /// SuperPtrTy - LLVM type for struct objc_super *.
286 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000287
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000288 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
289 /// in GCC parlance).
290 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000291
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000292 /// PropertyListTy - LLVM type for struct objc_property_list
293 /// (_prop_list_t in GCC parlance).
294 const llvm::StructType *PropertyListTy;
295 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
296 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000297
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000298 // MethodTy - LLVM type for struct objc_method.
299 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000300
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000301 /// CacheTy - LLVM type for struct objc_cache.
302 const llvm::Type *CacheTy;
303 /// CachePtrTy - LLVM type for struct objc_cache *.
304 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000305
Chris Lattner72db6c32009-04-22 02:44:54 +0000306 llvm::Constant *getGetPropertyFn() {
307 CodeGen::CodeGenTypes &Types = CGM.getTypes();
308 ASTContext &Ctx = CGM.getContext();
309 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000310 llvm::SmallVector<CanQualType,4> Params;
311 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
312 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000313 Params.push_back(IdType);
314 Params.push_back(SelType);
315 Params.push_back(Ctx.LongTy);
316 Params.push_back(Ctx.BoolTy);
317 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000318 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000319 FunctionType::ExtInfo()),
320 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
322 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000323
Chris Lattner72db6c32009-04-22 02:44:54 +0000324 llvm::Constant *getSetPropertyFn() {
325 CodeGen::CodeGenTypes &Types = CGM.getTypes();
326 ASTContext &Ctx = CGM.getContext();
327 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000328 llvm::SmallVector<CanQualType,6> Params;
329 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
330 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000331 Params.push_back(IdType);
332 Params.push_back(SelType);
333 Params.push_back(Ctx.LongTy);
334 Params.push_back(IdType);
335 Params.push_back(Ctx.BoolTy);
336 Params.push_back(Ctx.BoolTy);
337 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000338 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000339 FunctionType::ExtInfo()),
340 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
342 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000343
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000344
345 llvm::Constant *getCopyStructFn() {
346 CodeGen::CodeGenTypes &Types = CGM.getTypes();
347 ASTContext &Ctx = CGM.getContext();
348 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
349 llvm::SmallVector<CanQualType,5> Params;
350 Params.push_back(Ctx.VoidPtrTy);
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.LongTy);
353 Params.push_back(Ctx.BoolTy);
354 Params.push_back(Ctx.BoolTy);
355 const llvm::FunctionType *FTy =
356 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
357 FunctionType::ExtInfo()),
358 false);
359 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
360 }
361
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000363 CodeGen::CodeGenTypes &Types = CGM.getTypes();
364 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000365 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000366 llvm::SmallVector<CanQualType,1> Params;
367 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000368 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000369 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000370 FunctionType::ExtInfo()),
371 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
373 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000374
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000375 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000376 llvm::Constant *getGcReadWeakFn() {
377 // id objc_read_weak (id *)
378 std::vector<const llvm::Type*> Args;
379 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000380 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000381 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000382 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000383 }
384
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000385 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000386 llvm::Constant *getGcAssignWeakFn() {
387 // id objc_assign_weak (id, id *)
388 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
389 Args.push_back(ObjectPtrTy->getPointerTo());
390 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000391 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000392 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
393 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000394
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000395 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000396 llvm::Constant *getGcAssignGlobalFn() {
397 // id objc_assign_global(id, id *)
398 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
399 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000400 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000401 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000402 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000404
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000405 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
406 llvm::Constant *getGcAssignThreadLocalFn() {
407 // id objc_assign_threadlocal(id src, id * dest)
408 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
409 Args.push_back(ObjectPtrTy->getPointerTo());
410 llvm::FunctionType *FTy =
411 llvm::FunctionType::get(ObjectPtrTy, Args, false);
412 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
413 }
414
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000415 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000417 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
419 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000420 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000421 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000422 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
424 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000425
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000426 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
427 llvm::Constant *GcMemmoveCollectableFn() {
428 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
429 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
430 Args.push_back(Int8PtrTy);
431 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000432 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000433 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000435
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000436 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000437 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000438 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000439 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
440 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000441 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000442 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000443 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
444 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000445
446 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000447 llvm::Constant *getExceptionThrowFn() {
448 // void objc_exception_throw(id)
449 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
450 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000451 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000452 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
453 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000454
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000455 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
456 llvm::Constant *getExceptionRethrowFn() {
457 // void objc_exception_rethrow(void)
458 std::vector<const llvm::Type*> Args;
459 llvm::FunctionType *FTy =
460 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
461 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
462 }
463
Daniel Dunbar1c566672009-02-24 01:43:46 +0000464 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000465 llvm::Constant *getSyncEnterFn() {
466 // void objc_sync_enter (id)
467 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
468 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000469 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000470 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Daniel Dunbar1c566672009-02-24 01:43:46 +0000473 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000474 llvm::Constant *getSyncExitFn() {
475 // void objc_sync_exit (id)
476 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
477 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000478 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000479 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
480 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000481
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000482 llvm::Constant *getSendFn(bool IsSuper) const {
483 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
484 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000486 llvm::Constant *getSendFn2(bool IsSuper) const {
487 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
488 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000489
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000490 llvm::Constant *getSendStretFn(bool IsSuper) const {
491 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
492 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000493
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000494 llvm::Constant *getSendStretFn2(bool IsSuper) const {
495 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
496 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000497
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000498 llvm::Constant *getSendFpretFn(bool IsSuper) const {
499 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
500 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000501
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000502 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
503 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
504 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000505
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000506 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
507 ~ObjCCommonTypesHelper(){}
508};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000509
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000510/// ObjCTypesHelper - Helper class that encapsulates lazy
511/// construction of varies types used during ObjC generation.
512class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000513public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000514 /// SymtabTy - LLVM type for struct objc_symtab.
515 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000516 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
517 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000518 /// ModuleTy - LLVM type for struct objc_module.
519 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000520
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolTy - LLVM type for struct objc_protocol.
522 const llvm::StructType *ProtocolTy;
523 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
524 const llvm::Type *ProtocolPtrTy;
525 /// ProtocolExtensionTy - LLVM type for struct
526 /// objc_protocol_extension.
527 const llvm::StructType *ProtocolExtensionTy;
528 /// ProtocolExtensionTy - LLVM type for struct
529 /// objc_protocol_extension *.
530 const llvm::Type *ProtocolExtensionPtrTy;
531 /// MethodDescriptionTy - LLVM type for struct
532 /// objc_method_description.
533 const llvm::StructType *MethodDescriptionTy;
534 /// MethodDescriptionListTy - LLVM type for struct
535 /// objc_method_description_list.
536 const llvm::StructType *MethodDescriptionListTy;
537 /// MethodDescriptionListPtrTy - LLVM type for struct
538 /// objc_method_description_list *.
539 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000540 /// ProtocolListTy - LLVM type for struct objc_property_list.
541 const llvm::Type *ProtocolListTy;
542 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
543 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000544 /// CategoryTy - LLVM type for struct objc_category.
545 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000546 /// ClassTy - LLVM type for struct objc_class.
547 const llvm::StructType *ClassTy;
548 /// ClassPtrTy - LLVM type for struct objc_class *.
549 const llvm::Type *ClassPtrTy;
550 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
551 const llvm::StructType *ClassExtensionTy;
552 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
553 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000554 // IvarTy - LLVM type for struct objc_ivar.
555 const llvm::StructType *IvarTy;
556 /// IvarListTy - LLVM type for struct objc_ivar_list.
557 const llvm::Type *IvarListTy;
558 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
559 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000560 /// MethodListTy - LLVM type for struct objc_method_list.
561 const llvm::Type *MethodListTy;
562 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
563 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000564
Anders Carlsson124526b2008-09-09 10:10:21 +0000565 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
566 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000567
Anders Carlsson124526b2008-09-09 10:10:21 +0000568 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 llvm::Constant *getExceptionTryEnterFn() {
570 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000571 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000572 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000573 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 Params, false),
575 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000576 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000577
578 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000579 llvm::Constant *getExceptionTryExitFn() {
580 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000581 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000582 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000583 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000584 Params, false),
585 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000586 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000587
588 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000589 llvm::Constant *getExceptionExtractFn() {
590 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000591 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
592 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000593 Params, false),
594 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000595
Chris Lattner34b02a12009-04-22 02:26:14 +0000596 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000597
Anders Carlsson124526b2008-09-09 10:10:21 +0000598 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000599 llvm::Constant *getExceptionMatchFn() {
600 std::vector<const llvm::Type*> Params;
601 Params.push_back(ClassPtrTy);
602 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000604 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605 Params, false),
606 "objc_exception_match");
607
Chris Lattner34b02a12009-04-22 02:26:14 +0000608 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Anders Carlsson124526b2008-09-09 10:10:21 +0000610 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000611 llvm::Constant *getSetJmpFn() {
612 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000613 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000614 return
Owen Anderson0032b272009-08-13 21:57:51 +0000615 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000616 Params, false),
617 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Chris Lattner34b02a12009-04-22 02:26:14 +0000619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000621public:
622 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000623 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000624};
625
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000626/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000627/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000628class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000629public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // MethodListnfABITy - LLVM for struct _method_list_t
632 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
635 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ProtocolnfABITy = LLVM for struct _protocol_t
638 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Daniel Dunbar948e2582009-02-15 07:36:20 +0000640 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
641 const llvm::Type *ProtocolnfABIPtrTy;
642
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000643 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
644 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000645
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000646 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
647 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000648
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000649 // ClassnfABITy - LLVM for struct _class_t
650 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000652 // ClassnfABIPtrTy - LLVM for struct _class_t*
653 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000654
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000655 // IvarnfABITy - LLVM for struct _ivar_t
656 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000658 // IvarListnfABITy - LLVM for struct _ivar_list_t
659 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000661 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
662 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000663
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000664 // ClassRonfABITy - LLVM for struct _class_ro_t
665 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000666
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000667 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
668 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000669
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000670 // CategorynfABITy - LLVM for struct _category_t
671 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000672
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000673 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000675 // MessageRefTy - LLVM for:
676 // struct _message_ref_t {
677 // IMP messenger;
678 // SEL name;
679 // };
680 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000681 // MessageRefCTy - clang type for struct _message_ref_t
682 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000683
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000684 // MessageRefPtrTy - LLVM for struct _message_ref_t*
685 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000686 // MessageRefCPtrTy - clang type for struct _message_ref_t*
687 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000688
Fariborz Jahanianef163782009-02-05 01:13:09 +0000689 // MessengerTy - Type of the messenger (shown as IMP above)
690 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000692 // SuperMessageRefTy - LLVM for:
693 // struct _super_message_ref_t {
694 // SUPER_IMP messenger;
695 // SEL name;
696 // };
697 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000698
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000699 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
700 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000701
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 llvm::Constant *getMessageSendFixupFn() {
703 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
704 std::vector<const llvm::Type*> Params;
705 Params.push_back(ObjectPtrTy);
706 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000708 Params, true),
709 "objc_msgSend_fixup");
710 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000711
Chris Lattner1c02f862009-04-22 02:53:24 +0000712 llvm::Constant *getMessageSendFpretFixupFn() {
713 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
714 std::vector<const llvm::Type*> Params;
715 Params.push_back(ObjectPtrTy);
716 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000718 Params, true),
719 "objc_msgSend_fpret_fixup");
720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721
Chris Lattner1c02f862009-04-22 02:53:24 +0000722 llvm::Constant *getMessageSendStretFixupFn() {
723 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
724 std::vector<const llvm::Type*> Params;
725 Params.push_back(ObjectPtrTy);
726 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000727 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000728 Params, true),
729 "objc_msgSend_stret_fixup");
730 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Chris Lattner1c02f862009-04-22 02:53:24 +0000732 llvm::Constant *getMessageSendIdFixupFn() {
733 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
734 std::vector<const llvm::Type*> Params;
735 Params.push_back(ObjectPtrTy);
736 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000737 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000738 Params, true),
739 "objc_msgSendId_fixup");
740 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000741
Chris Lattner1c02f862009-04-22 02:53:24 +0000742 llvm::Constant *getMessageSendIdStretFixupFn() {
743 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
744 std::vector<const llvm::Type*> Params;
745 Params.push_back(ObjectPtrTy);
746 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000747 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000748 Params, true),
749 "objc_msgSendId_stret_fixup");
750 }
751 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000753 // struct _super_message_ref_t*, ...)
754 std::vector<const llvm::Type*> Params;
755 Params.push_back(SuperPtrTy);
756 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000757 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000758 Params, true),
759 "objc_msgSendSuper2_fixup");
760 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Chris Lattner1c02f862009-04-22 02:53:24 +0000762 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000763 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000764 // struct _super_message_ref_t*, ...)
765 std::vector<const llvm::Type*> Params;
766 Params.push_back(SuperPtrTy);
767 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000768 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000769 Params, true),
770 "objc_msgSendSuper2_stret_fixup");
771 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000772
773
774
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000775 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
776 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000777 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000778 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000779 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000780 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000782 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000783 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000784
Chris Lattner8a569112009-04-22 02:15:23 +0000785 llvm::Constant *getUnwindResumeOrRethrowFn() {
786 std::vector<const llvm::Type*> Params;
787 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000788 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000789 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000790 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000791 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
792 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000793 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000794
Chris Lattner8a569112009-04-22 02:15:23 +0000795 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000796 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000797 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000798 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Chris Lattner8a569112009-04-22 02:15:23 +0000800 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801
Chris Lattner8a569112009-04-22 02:15:23 +0000802 llvm::Constant *getObjCBeginCatchFn() {
803 std::vector<const llvm::Type*> Params;
804 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000805 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000806 Params, false),
807 "objc_begin_catch");
808 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000809
810 const llvm::StructType *EHTypeTy;
811 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000812
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000813 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
814 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000815};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000816
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000817class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000818public:
819 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000820 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000821 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000822 unsigned ivar_bytepos;
823 unsigned ivar_size;
824 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000825 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000826
827 // Allow sorting based on byte pos.
828 bool operator<(const GC_IVAR &b) const {
829 return ivar_bytepos < b.ivar_bytepos;
830 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000831 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000833 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000834 public:
835 unsigned skip;
836 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000837 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000838 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000839 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000841protected:
842 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000843 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000844 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000845 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000847 // gc ivar layout bitmap calculation helper caches.
848 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
849 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000851 /// LazySymbols - Symbols to generate a lazy reference for. See
852 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000853 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000855 /// DefinedSymbols - External symbols which are defined by this
856 /// module. The symbols in this list and LazySymbols are used to add
857 /// special linker symbols which ensure that Objective-C modules are
858 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000859 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000860
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000861 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000862 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000863
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000864 /// MethodVarNames - uniqued method variable names.
865 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000867 /// DefinedCategoryNames - list of category names in form Class_Category.
868 llvm::SetVector<std::string> DefinedCategoryNames;
869
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000870 /// MethodVarTypes - uniqued method type signatures. We have to use
871 /// a StringMap here because have no other unique reference.
872 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000873
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000874 /// MethodDefinitions - map of methods which have been defined in
875 /// this translation unit.
876 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000878 /// PropertyNames - uniqued method variable names.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000881 /// ClassReferences - uniqued class references.
882 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000884 /// SelectorReferences - uniqued selector references.
885 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000887 /// Protocols - Protocols for which an objc_protocol structure has
888 /// been emitted. Forward declarations are handled by creating an
889 /// empty structure whose initializer is filled in when/if defined.
890 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000891
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000892 /// DefinedProtocols - Protocols which have actually been
893 /// defined. We should not need this, see FIXME in GenerateProtocol.
894 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000895
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000896 /// DefinedClasses - List of defined classes.
897 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000898
899 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
900 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000901
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000902 /// DefinedCategories - List of defined categories.
903 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000905 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
906 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 /// GetNameForMethod - Return a name for the given method.
909 /// \param[out] NameOut - The return value.
910 void GetNameForMethod(const ObjCMethodDecl *OMD,
911 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000912 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000914 /// GetMethodVarName - Return a unique constant for the given
915 /// selector's name. The return value has type char *.
916 llvm::Constant *GetMethodVarName(Selector Sel);
917 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
918 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000920 /// GetMethodVarType - Return a unique constant for the given
921 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000922
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000923 // FIXME: This is a horrible name.
924 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000925 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000926
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000927 /// GetPropertyName - Return a unique constant for the given
928 /// name. The return value has type char *.
929 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000930
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000931 // FIXME: This can be dropped once string functions are unified.
932 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
933 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000934
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000935 /// GetClassName - Return a unique constant for the given selector's
936 /// name. The return value has type char *.
937 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000938
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000939 /// BuildIvarLayout - Builds ivar layout bitmap for the class
940 /// implementation for the __strong or __weak case.
941 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000942 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
943 bool ForStrongLayout);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000944
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000945 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000946 unsigned int BytePos, bool ForStrongLayout,
947 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000948 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000949 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000950 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000951 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000952 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000953 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000954
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000955 /// GetIvarLayoutName - Returns a unique constant for the given
956 /// ivar layout bitmap.
957 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
958 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000959
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000960 /// EmitPropertyList - Emit the given property list. The return
961 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000962 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000963 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000964 const ObjCContainerDecl *OCD,
965 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000966
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000967 /// PushProtocolProperties - Push protocol's property on the input stack.
968 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
969 std::vector<llvm::Constant*> &Properties,
970 const Decl *Container,
971 const ObjCProtocolDecl *PROTO,
972 const ObjCCommonTypesHelper &ObjCTypes);
973
Fariborz Jahanianda320092009-01-29 19:24:30 +0000974 /// GetProtocolRef - Return a reference to the internal protocol
975 /// description, creating an empty one if it has not been
976 /// defined. The return value has type ProtocolPtrTy.
977 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000978
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000979 /// CreateMetadataVar - Create a global variable with internal
980 /// linkage for use by the Objective-C runtime.
981 ///
982 /// This is a convenience wrapper which not only creates the
983 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000984 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000985 ///
986 /// \param Name - The variable name.
987 /// \param Init - The variable initializer; this is also used to
988 /// define the type of the variable.
989 /// \param Section - The section the variable should go into, or 0.
990 /// \param Align - The alignment for the variable, or 0.
991 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000992 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000993 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000994 llvm::Constant *Init,
995 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000996 unsigned Align,
997 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000998
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000999 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001000 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001001 QualType ResultType,
1002 llvm::Value *Sel,
1003 llvm::Value *Arg0,
1004 QualType Arg0Ty,
1005 bool IsSuper,
1006 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001007 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001008 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001009
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001010 /// EmitImageInfo - Emit the image info marker used to encode some module
1011 /// level information.
1012 void EmitImageInfo();
1013
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001014public:
Owen Anderson69243822009-07-13 04:10:07 +00001015 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001016 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001017
David Chisnall0d13f6f2010-01-23 02:40:42 +00001018 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001019
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001020 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1021 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001022
Fariborz Jahanianda320092009-01-29 19:24:30 +00001023 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001024
Fariborz Jahanianda320092009-01-29 19:24:30 +00001025 /// GetOrEmitProtocol - Get the protocol object for the given
1026 /// declaration, emitting it if necessary. The return value has type
1027 /// ProtocolPtrTy.
1028 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001029
Fariborz Jahanianda320092009-01-29 19:24:30 +00001030 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1031 /// object for the given declaration, emitting it if needed. These
1032 /// forward references will be filled in with empty bodies if no
1033 /// definition is seen. The return value has type ProtocolPtrTy.
1034 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001035};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001036
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001037class CGObjCMac : public CGObjCCommonMac {
1038private:
1039 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001040
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001041 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001042 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001043 void EmitModuleInfo();
1044
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001045 /// EmitModuleSymols - Emit module symbols, the list of defined
1046 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001047 llvm::Constant *EmitModuleSymbols();
1048
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001049 /// FinishModule - Write out global data structures at the end of
1050 /// processing a translation unit.
1051 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001052
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001053 /// EmitClassExtension - Generate the class extension structure used
1054 /// to store the weak ivar layout and properties. The return value
1055 /// has type ClassExtensionPtrTy.
1056 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1057
1058 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1059 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001060 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001061 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001062
1063 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1064 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001066 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001067 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001068 QualType ResultType,
1069 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001070 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001071 QualType Arg0Ty,
1072 bool IsSuper,
1073 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001074
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001075 /// EmitIvarList - Emit the ivar list for the given
1076 /// implementation. If ForClass is true the list of class ivars
1077 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1078 /// interface ivars will be emitted. The return value has type
1079 /// IvarListPtrTy.
1080 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001081 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001082
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001083 /// EmitMetaClass - Emit a forward reference to the class structure
1084 /// for the metaclass of the given interface. The return value has
1085 /// type ClassPtrTy.
1086 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1087
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001088 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001090 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1091 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001092 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001093
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001094 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001095
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001096 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001097
1098 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001099 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001100 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001101 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001102 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001103
1104 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001105 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001106 /// - TypeName: The name for the type containing the methods.
1107 /// - IsProtocol: True iff these methods are for a protocol.
1108 /// - ClassMethds: True iff these are class methods.
1109 /// - Required: When true, only "required" methods are
1110 /// listed. Similarly, when false only "optional" methods are
1111 /// listed. For classes this should always be true.
1112 /// - begin, end: The method list to output.
1113 ///
1114 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001115 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001116 const char *Section,
1117 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001119 /// GetOrEmitProtocol - Get the protocol object for the given
1120 /// declaration, emitting it if necessary. The return value has type
1121 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001122 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001123
1124 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1125 /// object for the given declaration, emitting it if needed. These
1126 /// forward references will be filled in with empty bodies if no
1127 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001128 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001129
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 /// EmitProtocolExtension - Generate the protocol extension
1131 /// structure used to store optional instance and class methods, and
1132 /// protocol properties. The return value has type
1133 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001134 llvm::Constant *
1135 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1136 const ConstantVector &OptInstanceMethods,
1137 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138
1139 /// EmitProtocolList - Generate the list of referenced
1140 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001141 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001142 ObjCProtocolDecl::protocol_iterator begin,
1143 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001144
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001145 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1146 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001147 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1148 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
1150public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001151 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001152
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001153 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001154
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001155 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001156 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001157 QualType ResultType,
1158 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001159 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001160 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001161 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001162 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001163
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001164 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001165 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001166 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001167 QualType ResultType,
1168 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001169 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001170 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001171 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001172 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001173 const CallArgList &CallArgs,
1174 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001175
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001176 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001177 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001178
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001179 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1180 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001181
1182 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1183 /// untyped one.
1184 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1185 const ObjCMethodDecl *Method);
1186
John McCall5a180392010-07-24 00:37:23 +00001187 virtual llvm::Constant *GetEHType(QualType T);
1188
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001189 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001190
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001191 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001192
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001193 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001194 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001195
Chris Lattner74391b42009-03-22 21:03:39 +00001196 virtual llvm::Constant *GetPropertyGetFunction();
1197 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001198 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001199 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
John McCallf1549f62010-07-06 01:34:17 +00001201 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1202 const ObjCAtTryStmt &S);
1203 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1204 const ObjCAtSynchronizedStmt &S);
1205 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001206 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1207 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001208 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001209 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001210 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001211 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001212 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001213 llvm::Value *src, llvm::Value *dest,
1214 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001215 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001216 llvm::Value *src, llvm::Value *dest,
1217 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001218 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1219 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001220 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1221 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001222 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001223
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001224 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1225 QualType ObjectTy,
1226 llvm::Value *BaseValue,
1227 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001228 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001229 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001230 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001231 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001232};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001233
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001234class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001235private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001236 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001237 llvm::GlobalVariable* ObjCEmptyCacheVar;
1238 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001239
Daniel Dunbar11394522009-04-18 08:51:00 +00001240 /// SuperClassReferences - uniqued super class references.
1241 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001243 /// MetaClassReferences - uniqued meta class references.
1244 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001245
1246 /// EHTypeReferences - uniqued class ehtype references.
1247 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001248
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001249 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001250 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001251 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001252
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001253 /// DefinedMetaClasses - List of defined meta-classes.
1254 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1255
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001256 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001257 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001258 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001259
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001260 /// FinishNonFragileABIModule - Write out global data structures at the end of
1261 /// processing a translation unit.
1262 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001263
Daniel Dunbar463b8762009-05-15 21:48:48 +00001264 /// AddModuleClassList - Add the given list of class pointers to the
1265 /// module with the provided symbol and section names.
1266 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1267 const char *SymbolName,
1268 const char *SectionName);
1269
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001270 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1271 unsigned InstanceStart,
1272 unsigned InstanceSize,
1273 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001274 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001275 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001276 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001277 llvm::Constant *ClassRoGV,
1278 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001280 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001282 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001284 /// EmitMethodList - Emit the method list for the given
1285 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001286 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001287 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001288 const ConstantVector &Methods);
1289 /// EmitIvarList - Emit the ivar list for the given
1290 /// implementation. If ForClass is true the list of class ivars
1291 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1292 /// interface ivars will be emitted. The return value has type
1293 /// IvarListnfABIPtrTy.
1294 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001295
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001296 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001297 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001298 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299
Fariborz Jahanianda320092009-01-29 19:24:30 +00001300 /// GetOrEmitProtocol - Get the protocol object for the given
1301 /// declaration, emitting it if necessary. The return value has type
1302 /// ProtocolPtrTy.
1303 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001304
Fariborz Jahanianda320092009-01-29 19:24:30 +00001305 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1306 /// object for the given declaration, emitting it if needed. These
1307 /// forward references will be filled in with empty bodies if no
1308 /// definition is seen. The return value has type ProtocolPtrTy.
1309 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Fariborz Jahanianda320092009-01-29 19:24:30 +00001311 /// EmitProtocolList - Generate the list of referenced
1312 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001313 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001314 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001315 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001316
Fariborz Jahanian46551122009-02-04 00:22:57 +00001317 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001318 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001319 QualType ResultType,
1320 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001321 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001322 QualType Arg0Ty,
1323 bool IsSuper,
1324 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001325
1326 /// GetClassGlobal - Return the global variable for the Objective-C
1327 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001328 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001329
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001330 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001331 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001332 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001333 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001334
Daniel Dunbar11394522009-04-18 08:51:00 +00001335 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1336 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001337 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1338 const ObjCInterfaceDecl *ID);
1339
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001340 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1341 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001342 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001343 const ObjCInterfaceDecl *ID);
1344
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001345 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1346 /// the given ivar.
1347 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001348 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349 const ObjCInterfaceDecl *ID,
1350 const ObjCIvarDecl *Ivar);
1351
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001352 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1353 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001354 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1355 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001356
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001357 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001358 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001359 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001360 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001361
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001362 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001363 return "OBJC_METACLASS_$_";
1364 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001365
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001366 const char *getClassSymbolPrefix() const {
1367 return "OBJC_CLASS_$_";
1368 }
1369
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001370 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001371 uint32_t &InstanceStart,
1372 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001373
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001374 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001375 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001376 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1377 return CGM.getContext().Selectors.getSelector(0, &II);
1378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001380 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001381 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1382 return CGM.getContext().Selectors.getSelector(1, &II);
1383 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001384
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001385 /// ImplementationIsNonLazy - Check whether the given category or
1386 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001387 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001388
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001389public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001390 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001391 // FIXME. All stubs for now!
1392 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001393
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001394 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001395 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001396 QualType ResultType,
1397 Selector Sel,
1398 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001399 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001400 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001401 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001402
1403 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001404 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001405 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001406 QualType ResultType,
1407 Selector Sel,
1408 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001409 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001410 llvm::Value *Receiver,
1411 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001412 const CallArgList &CallArgs,
1413 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001414
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001415 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001416 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001417
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001418 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1419 bool lvalue = false)
1420 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001421
1422 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1423 /// untyped one.
1424 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1425 const ObjCMethodDecl *Method)
1426 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001427
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001428 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001429
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001430 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001431 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001432 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001433
John McCall5a180392010-07-24 00:37:23 +00001434 virtual llvm::Constant *GetEHType(QualType T);
1435
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001436 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001437 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001438 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001439 virtual llvm::Constant *GetPropertySetFunction() {
1440 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001441 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001442
1443 virtual llvm::Constant *GetCopyStructFunction() {
1444 return ObjCTypes.getCopyStructFn();
1445 }
1446
Chris Lattner74391b42009-03-22 21:03:39 +00001447 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001448 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001449 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001450
John McCallf1549f62010-07-06 01:34:17 +00001451 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1452 const ObjCAtTryStmt &S);
1453 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1454 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001455 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001456 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001457 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001458 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001459 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001460 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001461 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001462 llvm::Value *src, llvm::Value *dest,
1463 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001464 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001465 llvm::Value *src, llvm::Value *dest,
1466 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001467 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001468 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001469 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1470 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001471 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001472 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1473 QualType ObjectTy,
1474 llvm::Value *BaseValue,
1475 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001476 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001477 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001478 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001479 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001480};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001481
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001482} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001483
1484/* *** Helper Functions *** */
1485
1486/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001487static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001488 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001489 unsigned idx0,
1490 unsigned idx1) {
1491 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001492 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1493 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001494 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001495 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001496}
1497
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001498/// hasObjCExceptionAttribute - Return true if this class or any super
1499/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001500static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001501 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001502 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001503 return true;
1504 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001505 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001506 return false;
1507}
1508
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001509/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001510
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001511CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001512 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001513 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001514 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001515}
1516
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001517/// GetClass - Return a reference to the class for the given interface
1518/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001519llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001520 const ObjCInterfaceDecl *ID) {
1521 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001522}
1523
1524/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001525llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1526 bool lval) {
1527 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001528}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001529llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001530 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001531 return EmitSelector(Builder, Method->getSelector());
1532}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001533
John McCall5a180392010-07-24 00:37:23 +00001534llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1535 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1536 return 0;
1537}
1538
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001539/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001540/*
1541 struct __builtin_CFString {
1542 const int *isa; // point to __CFConstantStringClassReference
1543 int flags;
1544 const char *str;
1545 long length;
1546 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001547*/
1548
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001549/// or Generate a constant NSString object.
1550/*
1551 struct __builtin_NSString {
1552 const int *isa; // point to __NSConstantStringClassReference
1553 const char *str;
1554 unsigned int length;
1555 };
1556*/
1557
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001558llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001559 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001560 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1561 CGM.GetAddrOfConstantCFString(SL) :
1562 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001563}
1564
1565/// Generates a message send where the super is the receiver. This is
1566/// a message send to self with special delivery semantics indicating
1567/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001568CodeGen::RValue
1569CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001570 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001571 QualType ResultType,
1572 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001573 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001574 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001575 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001576 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001577 const CodeGen::CallArgList &CallArgs,
1578 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001579 // Create and init a super structure; this is a (receiver, class)
1580 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001581 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001582 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001583 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001584 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001585 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001586 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001587
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001588 // If this is a class message the metaclass is passed as the target.
1589 llvm::Value *Target;
1590 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001591 if (isCategoryImpl) {
1592 // Message sent to 'super' in a class method defined in a category
1593 // implementation requires an odd treatment.
1594 // If we are in a class method, we must retrieve the
1595 // _metaclass_ for the current class, pointed at by
1596 // the class's "isa" pointer. The following assumes that
1597 // isa" is the first ivar in a class (which it must be).
1598 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1599 Target = CGF.Builder.CreateStructGEP(Target, 0);
1600 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001601 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001602 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1603 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1604 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1605 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001606 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001607 }
1608 else if (isCategoryImpl)
1609 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1610 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001611 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1612 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1613 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001614 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001615 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1616 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001617 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001618 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001619 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001620 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001621 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001622 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001623 EmitSelector(CGF.Builder, Sel),
1624 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001625 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001626}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001627
1628/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001629CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001630 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001631 QualType ResultType,
1632 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001633 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001634 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001635 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001636 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001637 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001638 EmitSelector(CGF.Builder, Sel),
1639 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001640 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001641}
1642
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001643CodeGen::RValue
1644CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001645 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001646 QualType ResultType,
1647 llvm::Value *Sel,
1648 llvm::Value *Arg0,
1649 QualType Arg0Ty,
1650 bool IsSuper,
1651 const CallArgList &CallArgs,
1652 const ObjCMethodDecl *Method,
1653 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001654 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001655 if (!IsSuper)
1656 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001657 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001658 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001659 CGF.getContext().getObjCSelType()));
1660 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001661
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001662 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001663 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001664 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001665 const llvm::FunctionType *FTy =
1666 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001667
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001668 if (Method)
1669 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1670 CGM.getContext().getCanonicalType(ResultType) &&
1671 "Result type mismatch!");
1672
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001673 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001674 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001675 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001676 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001677 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1678 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1679 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001680 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001681 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001682 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001683 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001684 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001685 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001686}
1687
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001688llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001689 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001690 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001691 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001692 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1693
Owen Anderson3c4972d2009-07-29 18:54:39 +00001694 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001695 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001696}
1697
Fariborz Jahanianda320092009-01-29 19:24:30 +00001698void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001699 // FIXME: We shouldn't need this, the protocol decl should contain enough
1700 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001701 DefinedProtocols.insert(PD->getIdentifier());
1702
1703 // If we have generated a forward reference to this protocol, emit
1704 // it now. Otherwise do nothing, the protocol objects are lazily
1705 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001706 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001707 GetOrEmitProtocol(PD);
1708}
1709
Fariborz Jahanianda320092009-01-29 19:24:30 +00001710llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001711 if (DefinedProtocols.count(PD->getIdentifier()))
1712 return GetOrEmitProtocol(PD);
1713 return GetOrEmitProtocolRef(PD);
1714}
1715
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001716/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001717// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1718struct _objc_protocol {
1719struct _objc_protocol_extension *isa;
1720char *protocol_name;
1721struct _objc_protocol_list *protocol_list;
1722struct _objc__method_prototype_list *instance_methods;
1723struct _objc__method_prototype_list *class_methods
1724};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001725
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001726See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001727*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001728llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1729 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1730
1731 // Early exit if a defining object has already been generated.
1732 if (Entry && Entry->hasInitializer())
1733 return Entry;
1734
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001735 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001736 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001737 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1738
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001739 // Construct method lists.
1740 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1741 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001742 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001743 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001744 ObjCMethodDecl *MD = *i;
1745 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1746 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1747 OptInstanceMethods.push_back(C);
1748 } else {
1749 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001750 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001751 }
1752
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001753 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001754 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001755 ObjCMethodDecl *MD = *i;
1756 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1757 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1758 OptClassMethods.push_back(C);
1759 } else {
1760 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001761 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001762 }
1763
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001764 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001765 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001766 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001767 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001768 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001769 PD->protocol_begin(),
1770 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001771 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001772 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001773 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1774 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001775 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001776 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001777 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1778 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001779 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001780 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001782 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001783 // Already created, fix the linkage and update the initializer.
1784 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001785 Entry->setInitializer(Init);
1786 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001787 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001788 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001789 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001790 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001791 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001792 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001793 // FIXME: Is this necessary? Why only for protocol?
1794 Entry->setAlignment(4);
1795 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001796 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001797
1798 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001799}
1800
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001801llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001802 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1803
1804 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001805 // We use the initializer as a marker of whether this is a forward
1806 // reference or not. At module finalization we add the empty
1807 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001808 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001809 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001810 llvm::GlobalValue::ExternalLinkage,
1811 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001812 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001813 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001814 // FIXME: Is this necessary? Why only for protocol?
1815 Entry->setAlignment(4);
1816 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001817
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001818 return Entry;
1819}
1820
1821/*
1822 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001823 uint32_t size;
1824 struct objc_method_description_list *optional_instance_methods;
1825 struct objc_method_description_list *optional_class_methods;
1826 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827 };
1828*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001829llvm::Constant *
1830CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1831 const ConstantVector &OptInstanceMethods,
1832 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001833 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001834 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001836 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001837 Values[1] =
1838 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001839 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001840 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1841 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001843 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001844 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1845 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001846 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001847 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001848
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001849 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001850 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001851 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001852 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001854 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001855 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001857 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001858 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001860 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001861}
1862
1863/*
1864 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001865 struct objc_protocol_list *next;
1866 long count;
1867 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001868 };
1869*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001870llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001871CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001872 ObjCProtocolDecl::protocol_iterator begin,
1873 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001874 std::vector<llvm::Constant*> ProtocolRefs;
1875
Daniel Dunbardbc933702008-08-21 21:57:41 +00001876 for (; begin != end; ++begin)
1877 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001878
1879 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001880 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001881 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001882
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001883 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001884 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001885
1886 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001887 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001888 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001889 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001890 ProtocolRefs.size() - 1);
1891 Values[2] =
1892 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1893 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001895
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001896 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001897 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001898 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001899 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001900 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001901}
1902
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001903void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1904 std::vector<llvm::Constant*> &Properties,
1905 const Decl *Container,
1906 const ObjCProtocolDecl *PROTO,
1907 const ObjCCommonTypesHelper &ObjCTypes) {
1908 std::vector<llvm::Constant*> Prop(2);
1909 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1910 E = PROTO->protocol_end(); P != E; ++P)
1911 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1912 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1913 E = PROTO->prop_end(); I != E; ++I) {
1914 const ObjCPropertyDecl *PD = *I;
1915 if (!PropertySet.insert(PD->getIdentifier()))
1916 continue;
1917 Prop[0] = GetPropertyName(PD->getIdentifier());
1918 Prop[1] = GetPropertyTypeString(PD, Container);
1919 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1920 }
1921}
1922
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001924 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 const char * const name;
1926 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001927 };
1928
1929 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 uint32_t entsize; // sizeof (struct _objc_property)
1931 uint32_t prop_count;
1932 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001933 };
1934*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001935llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001936 const Decl *Container,
1937 const ObjCContainerDecl *OCD,
1938 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001939 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001940 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001941 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1942 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001943 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001944 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001945 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001946 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001947 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001948 Prop));
1949 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001950 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001951 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1952 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001953 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1954 ObjCTypes);
1955 }
1956 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1957 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1958 E = CD->protocol_end(); P != E; ++P)
1959 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1960 ObjCTypes);
1961 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001962
1963 // Return null for empty list.
1964 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001965 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001966
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001967 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001968 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001969 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001970 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1971 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001972 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001973 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001974 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001975 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001976
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001977 llvm::GlobalVariable *GV =
1978 CreateMetadataVar(Name, Init,
1979 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001980 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001981 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001982 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001983 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001984}
1985
1986/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001987 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001988 int count;
1989 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001990 };
1991*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001992llvm::Constant *
1993CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1994 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001995 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1997 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001998 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001999 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002000 Desc);
2001}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002002
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002003llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002004 const char *Section,
2005 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002006 // Return null for empty list.
2007 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002008 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002009
2010 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002011 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002012 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002013 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002014 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002015 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002016
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002017 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002018 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002019 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002020}
2021
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002022/*
2023 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002024 char *category_name;
2025 char *class_name;
2026 struct _objc_method_list *instance_methods;
2027 struct _objc_method_list *class_methods;
2028 struct _objc_protocol_list *protocols;
2029 uint32_t size; // <rdar://4585769>
2030 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002031 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002032*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002033void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002034 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002035
Mike Stumpf5408fe2009-05-16 07:57:57 +00002036 // FIXME: This is poor design, the OCD should have a pointer to the category
2037 // decl. Additionally, note that Category can be null for the @implementation
2038 // w/o an @interface case. Sema should just create one for us as it does for
2039 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002040 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002041 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002042 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002043
2044 llvm::SmallString<256> ExtName;
2045 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2046 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002047
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002048 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002049 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002050 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002051 // Instance methods should always be defined.
2052 InstanceMethods.push_back(GetMethodConstant(*i));
2053 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002054 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002055 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002056 // Class methods should always be defined.
2057 ClassMethods.push_back(GetMethodConstant(*i));
2058 }
2059
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002060 std::vector<llvm::Constant*> Values(7);
2061 Values[0] = GetClassName(OCD->getIdentifier());
2062 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002063 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002064 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002065 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002066 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002067 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002069 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002070 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002071 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002072 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002073 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002074 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002075 Category->protocol_begin(),
2076 Category->protocol_end());
2077 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002078 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002079 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002080 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002081
2082 // If there is no category @interface then there can be no properties.
2083 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002084 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002085 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002086 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002087 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002088 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002089
Owen Anderson08e25242009-07-27 22:29:56 +00002090 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002091 Values);
2092
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002093 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002094 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002095 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002096 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002097 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002098 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002099}
2100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101// FIXME: Get from somewhere?
2102enum ClassFlags {
2103 eClassFlags_Factory = 0x00001,
2104 eClassFlags_Meta = 0x00002,
2105 // <rdr://5142207>
2106 eClassFlags_HasCXXStructors = 0x02000,
2107 eClassFlags_Hidden = 0x20000,
2108 eClassFlags_ABI2_Hidden = 0x00010,
2109 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2110};
2111
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002112/*
2113 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002114 Class isa;
2115 Class super_class;
2116 const char *name;
2117 long version;
2118 long info;
2119 long instance_size;
2120 struct _objc_ivar_list *ivars;
2121 struct _objc_method_list *methods;
2122 struct _objc_cache *cache;
2123 struct _objc_protocol_list *protocols;
2124 // Objective-C 1.0 extensions (<rdr://4585769>)
2125 const char *ivar_layout;
2126 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002127 };
2128
2129 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002130*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002131void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002132 DefinedSymbols.insert(ID->getIdentifier());
2133
Chris Lattner8ec03f52008-11-24 03:54:41 +00002134 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002135 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002137 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002138 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002139 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00002140 Interface->protocol_begin(),
2141 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002142 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002143 if (ID->getNumIvarInitializers())
2144 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002146 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002147
2148 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002149 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150 Flags |= eClassFlags_Hidden;
2151
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002152 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002153 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002154 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002155 // Instance methods should always be defined.
2156 InstanceMethods.push_back(GetMethodConstant(*i));
2157 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002158 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002159 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002160 // Class methods should always be defined.
2161 ClassMethods.push_back(GetMethodConstant(*i));
2162 }
2163
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002164 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002165 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002166 ObjCPropertyImplDecl *PID = *i;
2167
2168 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2169 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2170
2171 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2172 if (llvm::Constant *C = GetMethodConstant(MD))
2173 InstanceMethods.push_back(C);
2174 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2175 if (llvm::Constant *C = GetMethodConstant(MD))
2176 InstanceMethods.push_back(C);
2177 }
2178 }
2179
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002180 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002181 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002183 // Record a reference to the super class.
2184 LazySymbols.insert(Super->getIdentifier());
2185
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002187 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002188 ObjCTypes.ClassPtrTy);
2189 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002190 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002191 }
2192 Values[ 2] = GetClassName(ID->getIdentifier());
2193 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002194 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2195 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2196 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002197 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002198 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002199 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002200 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002201 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002202 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002203 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002205 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002207 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002209 std::string Name("\01L_OBJC_CLASS_");
2210 Name += ClassName;
2211 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2212 // Check for a forward reference.
2213 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2214 if (GV) {
2215 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2216 "Forward metaclass reference has incorrect type.");
2217 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2218 GV->setInitializer(Init);
2219 GV->setSection(Section);
2220 GV->setAlignment(4);
2221 CGM.AddUsedGlobal(GV);
2222 }
2223 else
2224 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 DefinedClasses.push_back(GV);
2226}
2227
2228llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2229 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002230 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002232 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233
Daniel Dunbar04d40782009-04-14 06:00:08 +00002234 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002237 std::vector<llvm::Constant*> Values(12);
2238 // The isa for the metaclass is the root of the hierarchy.
2239 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2240 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2241 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002242 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002243 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002245 // The super class for the metaclass is emitted as the name of the
2246 // super class. The runtime fixes this up to point to the
2247 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002248 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002249 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002250 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251 ObjCTypes.ClassPtrTy);
2252 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002253 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254 }
2255 Values[ 2] = GetClassName(ID->getIdentifier());
2256 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002257 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2258 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2259 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002260 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002261 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002262 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002263 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002264 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002265 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002266 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 Values[ 9] = Protocols;
2268 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002269 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002271 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002272 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273 Values);
2274
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002275 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002276 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002277
2278 // Check for a forward reference.
2279 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2280 if (GV) {
2281 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2282 "Forward metaclass reference has incorrect type.");
2283 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2284 GV->setInitializer(Init);
2285 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002286 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002287 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002288 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002289 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002291 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002292 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002293
2294 return GV;
2295}
2296
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002297llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002298 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002299
Mike Stumpf5408fe2009-05-16 07:57:57 +00002300 // FIXME: Should we look these up somewhere other than the module. Its a bit
2301 // silly since we only generate these while processing an implementation, so
2302 // exactly one pointer would work if know when we entered/exitted an
2303 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002304
2305 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002306 // Previously, metaclass with internal linkage may have been defined.
2307 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002308 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2309 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002310 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2311 "Forward metaclass reference has incorrect type.");
2312 return GV;
2313 } else {
2314 // Generate as an external reference to keep a consistent
2315 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002316 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002317 llvm::GlobalValue::ExternalLinkage,
2318 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002319 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002320 }
2321}
2322
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002323llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2324 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2325
2326 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2327 true)) {
2328 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2329 "Forward class metadata reference has incorrect type.");
2330 return GV;
2331 } else {
2332 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2333 llvm::GlobalValue::ExternalLinkage,
2334 0,
2335 Name);
2336 }
2337}
2338
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339/*
2340 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341 uint32_t size;
2342 const char *weak_ivar_layout;
2343 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002344 };
2345*/
2346llvm::Constant *
2347CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002348 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002349 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002350
2351 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002352 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002353 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002354 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002355 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002356
2357 // Return null if no extension bits are used.
2358 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002359 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002360
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002361 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002362 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002363 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002365 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002366}
2367
2368/*
2369 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370 char *ivar_name;
2371 char *ivar_type;
2372 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373 };
2374
2375 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002376 int ivar_count;
2377 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002378 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002379*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002380llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002381 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382 std::vector<llvm::Constant*> Ivars, Ivar(3);
2383
2384 // When emitting the root class GCC emits ivar entries for the
2385 // actual class structure. It is not clear if we need to follow this
2386 // behavior; for now lets try and get away with not doing it. If so,
2387 // the cleanest solution would be to make up an ObjCInterfaceDecl
2388 // for the class.
2389 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002390 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002391
2392 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002393 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002394
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002395 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002396 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002398 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2399 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002400 // Ignore unnamed bit-fields.
2401 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002402 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002403 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2404 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002405 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002406 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002407 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408 }
2409
2410 // Return null for empty list.
2411 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002412 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002413
2414 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002415 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002416 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002418 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002419 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002420
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002421 llvm::GlobalVariable *GV;
2422 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002423 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002424 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002425 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002426 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002427 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002428 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002429 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002430 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431}
2432
2433/*
2434 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002435 SEL method_name;
2436 char *method_types;
2437 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002439
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002441 struct objc_method_list *obsolete;
2442 int count;
2443 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 };
2445*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002446
2447/// GetMethodConstant - Return a struct objc_method constant for the
2448/// given method if it has been defined. The result is null if the
2449/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002450llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002451 // FIXME: Use DenseMap::lookup
2452 llvm::Function *Fn = MethodDefinitions[MD];
2453 if (!Fn)
2454 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002455
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002456 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002457 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002458 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002459 ObjCTypes.SelectorPtrTy);
2460 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002461 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002462 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002463}
2464
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002465llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002466 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002467 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002468 // Return null for empty list.
2469 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002470 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471
2472 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002473 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002474 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002475 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002476 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002477 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002478 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002480 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002481 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002482 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002483}
2484
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002485llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002486 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002487 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002488 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002489
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002490 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002491 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002492 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002493 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002494 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002495 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002496 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002497 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002498 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002499
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002500 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002501}
2502
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002503llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002504CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002505 llvm::Constant *Init,
2506 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002507 unsigned Align,
2508 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002509 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002510 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002511 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002512 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002513 if (Section)
2514 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002515 if (Align)
2516 GV->setAlignment(Align);
2517 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002518 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002519 return GV;
2520}
2521
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002522llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002523 // Abuse this interface function as a place to finalize.
2524 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002525 return NULL;
2526}
2527
Chris Lattner74391b42009-03-22 21:03:39 +00002528llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002529 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002530}
2531
Chris Lattner74391b42009-03-22 21:03:39 +00002532llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002533 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002534}
2535
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002536llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2537 return ObjCTypes.getCopyStructFn();
2538}
2539
Chris Lattner74391b42009-03-22 21:03:39 +00002540llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002541 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002542}
2543
John McCallf1549f62010-07-06 01:34:17 +00002544void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2545 return EmitTryOrSynchronizedStmt(CGF, S);
2546}
2547
2548void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2549 const ObjCAtSynchronizedStmt &S) {
2550 return EmitTryOrSynchronizedStmt(CGF, S);
2551}
2552
John McCallcc505292010-07-21 06:59:36 +00002553namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002554 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002555 const Stmt &S;
2556 llvm::Value *SyncArg;
2557 llvm::Value *CallTryExitVar;
2558 llvm::Value *ExceptionData;
2559 ObjCTypesHelper &ObjCTypes;
2560 PerformFragileFinally(const Stmt *S,
2561 llvm::Value *SyncArg,
2562 llvm::Value *CallTryExitVar,
2563 llvm::Value *ExceptionData,
2564 ObjCTypesHelper *ObjCTypes)
2565 : S(*S), SyncArg(SyncArg), CallTryExitVar(CallTryExitVar),
2566 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2567
2568 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2569 // Check whether we need to call objc_exception_try_exit.
2570 // In optimized code, this branch will always be folded.
2571 llvm::BasicBlock *FinallyCallExit =
2572 CGF.createBasicBlock("finally.call_exit");
2573 llvm::BasicBlock *FinallyNoCallExit =
2574 CGF.createBasicBlock("finally.no_call_exit");
2575 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2576 FinallyCallExit, FinallyNoCallExit);
2577
2578 CGF.EmitBlock(FinallyCallExit);
2579 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2580 ->setDoesNotThrow();
2581
2582 CGF.EmitBlock(FinallyNoCallExit);
2583
2584 if (isa<ObjCAtTryStmt>(S)) {
2585 if (const ObjCAtFinallyStmt* FinallyStmt =
2586 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2587 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2588
2589 // Currently, the end of the cleanup must always exist.
2590 CGF.EnsureInsertPoint();
2591 } else {
2592 // Emit objc_sync_exit(expr); as finally's sole statement for
2593 // @synchronized.
2594 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2595 ->setDoesNotThrow();
2596 }
2597 }
2598 };
2599}
2600
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002601/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002602
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002603 Objective-C setjmp-longjmp (sjlj) Exception Handling
2604 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002605
John McCallf1549f62010-07-06 01:34:17 +00002606 A catch buffer is a setjmp buffer plus:
2607 - a pointer to the exception that was caught
2608 - a pointer to the previous exception data buffer
2609 - two pointers of reserved storage
2610 Therefore catch buffers form a stack, with a pointer to the top
2611 of the stack kept in thread-local storage.
2612
2613 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2614 objc_exception_try_exit pops the given catch buffer, which is
2615 required to be the top of the EH stack.
2616 objc_exception_throw pops the top of the EH stack, writes the
2617 thrown exception into the appropriate field, and longjmps
2618 to the setjmp buffer. It crashes the process (with a printf
2619 and an abort()) if there are no catch buffers on the stack.
2620 objc_exception_extract just reads the exception pointer out of the
2621 catch buffer.
2622
2623 There's no reason an implementation couldn't use a light-weight
2624 setjmp here --- something like __builtin_setjmp, but API-compatible
2625 with the heavyweight setjmp. This will be more important if we ever
2626 want to implement correct ObjC/C++ exception interactions for the
2627 fragile ABI.
2628
2629 Note that for this use of setjmp/longjmp to be correct, we may need
2630 to mark some local variables volatile: if a non-volatile local
2631 variable is modified between the setjmp and the longjmp, it has
2632 indeterminate value. For the purposes of LLVM IR, it may be
2633 sufficient to make loads and stores within the @try (to variables
2634 declared outside the @try) volatile. This is necessary for
2635 optimized correctness, but is not currently being done; this is
2636 being tracked as rdar://problem/8160285
2637
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002638 The basic framework for a @try-catch-finally is as follows:
2639 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002640 objc_exception_data d;
2641 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002642 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002643
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002644 objc_exception_try_enter(&d);
2645 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002646 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002647 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002648 // exception path
2649 id _caught = objc_exception_extract(&d);
2650
2651 // enter new try scope for handlers
2652 if (!setjmp(d.jmp_buf)) {
2653 ... match exception and execute catch blocks ...
2654
2655 // fell off end, rethrow.
2656 _rethrow = _caught;
2657 ... jump-through-finally to finally_rethrow ...
2658 } else {
2659 // exception in catch block
2660 _rethrow = objc_exception_extract(&d);
2661 _call_try_exit = false;
2662 ... jump-through-finally to finally_rethrow ...
2663 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002664 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002665 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002666
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002667 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002668 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002669 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002670
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002671 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002672 ... dispatch to finally destination ...
2673
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002674 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002675 objc_exception_throw(_rethrow);
2676
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002677 finally_end:
2678 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002679
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002680 This framework differs slightly from the one gcc uses, in that gcc
2681 uses _rethrow to determine if objc_exception_try_exit should be called
2682 and if the object should be rethrown. This breaks in the face of
2683 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002684
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002685 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002686
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002687 - If there are no catch blocks, then we avoid emitting the second
2688 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002689
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002690 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2691 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002692
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002693 - FIXME: If there is no @finally block we can do a few more
2694 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002695
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002696 Rethrows and Jumps-Through-Finally
2697 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002698
John McCallf1549f62010-07-06 01:34:17 +00002699 '@throw;' is supported by pushing the currently-caught exception
2700 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002701
John McCallf1549f62010-07-06 01:34:17 +00002702 Branches through the @finally block are handled with an ordinary
2703 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2704 exceptions are not compatible with C++ exceptions, and this is
2705 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002706
John McCallf1549f62010-07-06 01:34:17 +00002707 @synchronized(expr) { stmt; } is emitted as if it were:
2708 id synch_value = expr;
2709 objc_sync_enter(synch_value);
2710 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002711*/
2712
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002713void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2714 const Stmt &S) {
2715 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002716
2717 // A destination for the fall-through edges of the catch handlers to
2718 // jump to.
2719 CodeGenFunction::JumpDest FinallyEnd =
2720 CGF.getJumpDestInCurrentScope("finally.end");
2721
2722 // A destination for the rethrow edge of the catch handlers to jump
2723 // to.
2724 CodeGenFunction::JumpDest FinallyRethrow =
2725 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002726
Daniel Dunbar1c566672009-02-24 01:43:46 +00002727 // For @synchronized, call objc_sync_enter(sync.expr). The
2728 // evaluation of the expression must occur before we enter the
2729 // @synchronized. We can safely avoid a temp here because jumps into
2730 // @synchronized are illegal & this will dominate uses.
2731 llvm::Value *SyncArg = 0;
2732 if (!isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002733 SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002734 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2735 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002736 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2737 ->setDoesNotThrow();
Daniel Dunbar1c566672009-02-24 01:43:46 +00002738 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002739
Daniel Dunbar898d5082008-09-30 01:06:03 +00002740 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002741 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2742 "exceptiondata.ptr");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002743 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002744 "_rethrow");
John McCallf1549f62010-07-06 01:34:17 +00002745
2746 // Create a flag indicating whether the cleanup needs to call
2747 // objc_exception_try_exit. This is true except when
2748 // - no catches match and we're branching through the cleanup
2749 // just to rethrow the exception, or
2750 // - a catch matched and we're falling out of the catch handler.
2751 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002752 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002753 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002754 CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002755
John McCallf1549f62010-07-06 01:34:17 +00002756 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002757 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
2758 SyncArg,
2759 CallTryExitVar,
2760 ExceptionData,
2761 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002762
2763 // Enter a try block:
2764 // - Call objc_exception_try_enter to push ExceptionData on top of
2765 // the EH stack.
2766 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2767 ->setDoesNotThrow();
2768
2769 // - Call setjmp on the exception data buffer.
2770 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2771 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2772 llvm::Value *SetJmpBuffer =
2773 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2774 llvm::CallInst *SetJmpResult =
2775 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2776 SetJmpResult->setDoesNotThrow();
2777
2778 // If setjmp returned 0, enter the protected block; otherwise,
2779 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002780 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2781 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002782 llvm::Value *DidCatch =
2783 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2784 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson80f25672008-09-09 17:59:25 +00002785
John McCallf1549f62010-07-06 01:34:17 +00002786 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002787 CGF.EmitBlock(TryBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002788 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002789 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002790 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002791
John McCallf1549f62010-07-06 01:34:17 +00002792 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002793 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002794
2795 // Retrieve the exception object. We may emit multiple blocks but
2796 // nothing can cross this so the value is already in SSA form.
John McCallf1549f62010-07-06 01:34:17 +00002797 llvm::CallInst *Caught =
Chris Lattner34b02a12009-04-22 02:26:14 +00002798 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2799 ExceptionData, "caught");
John McCallf1549f62010-07-06 01:34:17 +00002800 Caught->setDoesNotThrow();
2801
2802 // Remember the exception to rethrow.
2803 CGF.Builder.CreateStore(Caught, RethrowPtr);
2804
2805 // Note: at this point, objc_exception_throw already popped the
2806 // catch handler, so anything that branches to the cleanup needs
2807 // to set CallTryExitVar to false.
2808
2809 // For a @synchronized (or a @try with no catches), just branch
2810 // through the cleanup to the rethrow block.
2811 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2812 // Tell the cleanup not to re-pop the exit.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002813 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002814 CallTryExitVar);
2815
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002816 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002817
2818 // Otherwise, we have to match against the caught exceptions.
2819 } else {
2820 // Push the exception to rethrow onto the EH value stack for the
2821 // benefit of any @throws in the handlers.
2822 CGF.ObjCEHValueStack.push_back(Caught);
2823
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002824 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
2825
Daniel Dunbar55e40722008-09-27 07:03:52 +00002826 // Enter a new exception try block (in case a @catch block throws
John McCallf1549f62010-07-06 01:34:17 +00002827 // an exception). Now CallTryExitVar (currently true) is back in
2828 // synch with reality.
2829 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2830 ->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831
John McCallf1549f62010-07-06 01:34:17 +00002832 llvm::CallInst *SetJmpResult =
2833 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2834 "setjmp.result");
2835 SetJmpResult->setDoesNotThrow();
2836
2837 llvm::Value *Threw =
2838 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
Anders Carlsson80f25672008-09-09 17:59:25 +00002839
Daniel Dunbar55e87422008-11-11 02:29:29 +00002840 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
John McCallf1549f62010-07-06 01:34:17 +00002841 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch_for_catch");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002842 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002843
Anders Carlsson80f25672008-09-09 17:59:25 +00002844 CGF.EmitBlock(CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002845
Daniel Dunbar55e40722008-09-27 07:03:52 +00002846 // Handle catch list. As a special case we check if everything is
2847 // matched and avoid generating code for falling off the end if
2848 // so.
2849 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002850 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
2851 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00002852
Douglas Gregorc00d8e12010-04-26 16:46:50 +00002853 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002854 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002855
Anders Carlsson80f25672008-09-09 17:59:25 +00002856 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002857 if (!CatchParam) {
2858 AllMatched = true;
2859 } else {
John McCall183700f2009-09-21 23:43:11 +00002860 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002861
John McCallf1549f62010-07-06 01:34:17 +00002862 // catch(id e) always matches under this ABI, since only
2863 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002864 // FIXME: For the time being we also match id<X>; this should
2865 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002866 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002867 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002868 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002869
John McCallf1549f62010-07-06 01:34:17 +00002870 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002871 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00002872 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2873
Anders Carlssondde0a942008-09-11 09:15:33 +00002874 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002875 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002876 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00002877
2878 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00002879 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002880 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002881
Anders Carlssondde0a942008-09-11 09:15:33 +00002882 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00002883
2884 // The scope of the catch variable ends right here.
2885 CatchVarCleanups.ForceCleanup();
2886
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002887 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002888 break;
2889 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002890
Steve Naroff14108da2009-07-10 23:34:53 +00002891 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00002892 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00002893
2894 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00002895 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
2896 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00002897
2898 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00002899 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900
John McCallf1549f62010-07-06 01:34:17 +00002901 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00002902 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2903 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00002904 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002905
John McCallf1549f62010-07-06 01:34:17 +00002906 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
2907 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002908
2909 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002910 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911
Anders Carlsson80f25672008-09-09 17:59:25 +00002912 // Emit the @catch block.
2913 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00002914
2915 // Collect any cleanups for the catch variable. The scope lasts until
2916 // the end of the catch body.
2917 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2918
Steve Naroff7ba138a2009-03-03 19:52:17 +00002919 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002920 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002921
John McCallf1549f62010-07-06 01:34:17 +00002922 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002923 llvm::Value *Tmp =
2924 CGF.Builder.CreateBitCast(Caught,
2925 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002926 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002927 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002928
Anders Carlssondde0a942008-09-11 09:15:33 +00002929 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00002930
2931 // We're done with the catch variable.
2932 CatchVarCleanups.ForceCleanup();
2933
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002934 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002935
Anders Carlsson80f25672008-09-09 17:59:25 +00002936 CGF.EmitBlock(NextCatchBlock);
2937 }
2938
John McCallf1549f62010-07-06 01:34:17 +00002939 CGF.ObjCEHValueStack.pop_back();
2940
Daniel Dunbar55e40722008-09-27 07:03:52 +00002941 if (!AllMatched) {
2942 // None of the handlers caught the exception, so store it to be
2943 // rethrown at the end of the @finally block.
2944 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002945 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002946 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002947
Daniel Dunbar55e40722008-09-27 07:03:52 +00002948 // Emit the exception handler for the @catch blocks.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002949 CGF.EmitBlock(CatchHandler);
John McCallf1549f62010-07-06 01:34:17 +00002950
2951 // Rethrow the new exception, not the old one.
2952 Caught = CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2953 ExceptionData);
2954 Caught->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002955 CGF.Builder.CreateStore(Caught, RethrowPtr);
John McCallf1549f62010-07-06 01:34:17 +00002956
2957 // Don't pop the catch handler; the throw already did.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002958 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002959 CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002960 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002961 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002962
John McCallf1549f62010-07-06 01:34:17 +00002963 // Pop the cleanup.
2964 CGF.PopCleanupBlock();
John McCallff8e1152010-07-23 21:56:41 +00002965 CGF.EmitBlock(FinallyEnd.getBlock());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002966
John McCallf1549f62010-07-06 01:34:17 +00002967 // Emit the rethrow block.
2968 CGF.Builder.ClearInsertionPoint();
John McCallff8e1152010-07-23 21:56:41 +00002969 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00002970 if (CGF.HaveInsertPoint()) {
2971 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
2972 CGF.Builder.CreateLoad(RethrowPtr))
2973 ->setDoesNotThrow();
2974 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002975 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002976
John McCallff8e1152010-07-23 21:56:41 +00002977 CGF.Builder.SetInsertPoint(FinallyEnd.getBlock());
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002978}
2979
2980void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002981 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002982 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002983
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002984 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2985 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002986 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002987 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2988 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002989 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002990 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002991 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002992 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002993
John McCallf1549f62010-07-06 01:34:17 +00002994 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
2995 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00002996 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002997
2998 // Clear the insertion point to indicate we are in unreachable code.
2999 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003000}
3001
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003002/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003003/// object: objc_read_weak (id *src)
3004///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003005llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003006 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003007 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003008 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3009 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3010 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003011 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003012 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003013 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003014 return read_weak;
3015}
3016
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003017/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3018/// objc_assign_weak (id src, id *dst)
3019///
3020void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003021 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003022 const llvm::Type * SrcTy = src->getType();
3023 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003024 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003025 assert(Size <= 8 && "does not support size > 8");
3026 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003028 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3029 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003030 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3031 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003032 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003033 src, dst, "weakassign");
3034 return;
3035}
3036
Fariborz Jahanian58626502008-11-19 00:59:10 +00003037/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3038/// objc_assign_global (id src, id *dst)
3039///
3040void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003041 llvm::Value *src, llvm::Value *dst,
3042 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003043 const llvm::Type * SrcTy = src->getType();
3044 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003045 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003046 assert(Size <= 8 && "does not support size > 8");
3047 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003049 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3050 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003051 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3052 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003053 if (!threadlocal)
3054 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3055 src, dst, "globalassign");
3056 else
3057 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3058 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003059 return;
3060}
3061
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003062/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003063/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003064///
3065void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003066 llvm::Value *src, llvm::Value *dst,
3067 llvm::Value *ivarOffset) {
3068 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003069 const llvm::Type * SrcTy = src->getType();
3070 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003071 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003072 assert(Size <= 8 && "does not support size > 8");
3073 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003074 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003075 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3076 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003077 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3078 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003079 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3080 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003081 return;
3082}
3083
Fariborz Jahanian58626502008-11-19 00:59:10 +00003084/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3085/// objc_assign_strongCast (id src, id *dst)
3086///
3087void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003088 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003089 const llvm::Type * SrcTy = src->getType();
3090 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003091 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003092 assert(Size <= 8 && "does not support size > 8");
3093 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003094 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003095 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3096 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003097 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3098 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003099 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003100 src, dst, "weakassign");
3101 return;
3102}
3103
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003104void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105 llvm::Value *DestPtr,
3106 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003107 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003108 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3109 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003110 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003111 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003112 return;
3113}
3114
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003115/// EmitObjCValueForIvar - Code Gen for ivar reference.
3116///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003117LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3118 QualType ObjectTy,
3119 llvm::Value *BaseValue,
3120 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003121 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003122 const ObjCInterfaceDecl *ID =
3123 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003124 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3125 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003126}
3127
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003128llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003129 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003130 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003131 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003132 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003133 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3134 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003135}
3136
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003137/* *** Private Interface *** */
3138
3139/// EmitImageInfo - Emit the image info marker used to encode some module
3140/// level information.
3141///
3142/// See: <rdr://4810609&4810587&4810587>
3143/// struct IMAGE_INFO {
3144/// unsigned version;
3145/// unsigned flags;
3146/// };
3147enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003148 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003149 eImageInfo_GarbageCollected = (1 << 1),
3150 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003151 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3152
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003153 // A flag indicating that the module has no instances of a @synthesize of a
3154 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003155 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003156};
3157
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003158void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003159 unsigned version = 0; // Version is unused?
3160 unsigned flags = 0;
3161
3162 // FIXME: Fix and continue?
3163 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3164 flags |= eImageInfo_GarbageCollected;
3165 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3166 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003167
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003168 // We never allow @synthesize of a superclass property.
3169 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003170
Chris Lattner77b89b82010-06-27 07:15:29 +00003171 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3172
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003173 // Emitted as int[2];
3174 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003175 llvm::ConstantInt::get(Int32Ty, version),
3176 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003177 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003178 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003179
3180 const char *Section;
3181 if (ObjCABI == 1)
3182 Section = "__OBJC, __image_info,regular";
3183 else
3184 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003185 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003186 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003187 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003188 Section,
3189 0,
3190 true);
3191 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003192}
3193
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003194
3195// struct objc_module {
3196// unsigned long version;
3197// unsigned long size;
3198// const char *name;
3199// Symtab symtab;
3200// };
3201
3202// FIXME: Get from somewhere
3203static const int ModuleVersion = 7;
3204
3205void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003206 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003207
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003208 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003209 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3210 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003211 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003212 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003213 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003214 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003215 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003216 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003217 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003218}
3219
3220llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003221 unsigned NumClasses = DefinedClasses.size();
3222 unsigned NumCategories = DefinedCategories.size();
3223
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003224 // Return null if no symbols were defined.
3225 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003226 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003227
3228 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003229 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003230 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003231 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3232 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003233
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003234 // The runtime expects exactly the list of defined classes followed
3235 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003236 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003237 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003238 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003239 ObjCTypes.Int8PtrTy);
3240 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003241 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003242 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003243 ObjCTypes.Int8PtrTy);
3244
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003245 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003246 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003247 NumClasses + NumCategories),
3248 Symbols);
3249
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003250 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003251
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003252 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003253 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3254 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003255 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003256 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003257}
3258
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003259llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003260 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003261 LazySymbols.insert(ID->getIdentifier());
3262
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003263 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003264
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003265 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003266 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003267 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003268 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003270 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3271 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003272 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003273 }
3274
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003275 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003276}
3277
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003278llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3279 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003280 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003282 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003283 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003284 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003285 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003286 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003287 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3288 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003289 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003290 }
3291
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003292 if (lvalue)
3293 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003294 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003295}
3296
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003297llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003298 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003299
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003300 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003301 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003302 llvm::ConstantArray::get(VMContext,
3303 Ident->getNameStart()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003304 ((ObjCABI == 2) ?
3305 "__TEXT,__objc_classname,cstring_literals" :
3306 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003307 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003308
Owen Andersona1cf15f2009-07-14 23:10:40 +00003309 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003310}
3311
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003312/// GetIvarLayoutName - Returns a unique constant for the given
3313/// ivar layout bitmap.
3314llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003315 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003316 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003317}
3318
John McCall0953e762009-09-24 19:53:00 +00003319static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003320 if (FQT.isObjCGCStrong())
John McCall0953e762009-09-24 19:53:00 +00003321 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003322
3323 if (FQT.isObjCGCWeak())
John McCall0953e762009-09-24 19:53:00 +00003324 return Qualifiers::Weak;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003325
Fariborz Jahanian039e6a12009-09-11 17:39:05 +00003326 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003327 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003328
Ted Kremenek6217b802009-07-29 21:53:49 +00003329 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003330 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003331
John McCall0953e762009-09-24 19:53:00 +00003332 return Qualifiers::GCNone;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003333}
3334
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003335void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003337 bool ForStrongLayout,
3338 bool &HasUnion) {
3339 const RecordDecl *RD = RT->getDecl();
3340 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003341 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003342 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003343 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003344 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003345
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003346 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3347 ForStrongLayout, HasUnion);
3348}
3349
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003350void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003351 const llvm::StructLayout *Layout,
3352 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003353 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354 unsigned int BytePos, bool ForStrongLayout,
3355 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003356 bool IsUnion = (RD && RD->isUnion());
3357 uint64_t MaxUnionIvarSize = 0;
3358 uint64_t MaxSkippedUnionIvarSize = 0;
3359 FieldDecl *MaxField = 0;
3360 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003361 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003362 uint64_t MaxFieldOffset = 0;
3363 uint64_t MaxSkippedFieldOffset = 0;
3364 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003365
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003366 if (RecFields.empty())
3367 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003368 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3369 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3370
Chris Lattnerf1690852009-03-31 08:48:01 +00003371 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003372 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003373 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003374 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003375 // Note that 'i' here is actually the field index inside RD of Field,
3376 // although this dependency is hidden.
3377 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3378 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003379 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003380 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003381
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003382 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003383 if (!Field->getIdentifier() || Field->isBitField()) {
3384 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003385 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003386 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003387 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003388
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003389 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003390 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003391 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003392 if (FQT->isUnionType())
3393 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003394
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003395 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003396 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003397 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003398 continue;
3399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003400
Chris Lattnerf1690852009-03-31 08:48:01 +00003401 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003402 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003403 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003404 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003405 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003406 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003407 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3408 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003409 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003410 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003411 FQT = CArray->getElementType();
3412 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003413
3414 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003415 "layout for array of unions not supported");
3416 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003417 int OldIndex = IvarsInfo.size() - 1;
3418 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003419
Ted Kremenek6217b802009-07-29 21:53:49 +00003420 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003421 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003422 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003423
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003424 // Replicate layout information for each array element. Note that
3425 // one element is already done.
3426 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003427 for (int FirstIndex = IvarsInfo.size() - 1,
3428 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003429 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003430 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3431 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3432 IvarsInfo[i].ivar_size));
3433 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3434 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3435 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003436 }
3437 continue;
3438 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003439 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003440 // At this point, we are done with Record/Union and array there of.
3441 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003442 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003443
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003444 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003445 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3446 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003447 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003448 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003449 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003450 MaxUnionIvarSize = UnionIvarSize;
3451 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003452 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003453 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003454 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003455 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003456 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003457 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003458 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003459 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3460 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003461 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003462 // FIXME: Why the asymmetry? We divide by word size in bits on other
3463 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003464 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003465 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003466 MaxSkippedUnionIvarSize = UnionIvarSize;
3467 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003468 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003469 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003470 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003471 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003472 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003473 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003474 }
3475 }
3476 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003477
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003478 if (LastFieldBitfield) {
3479 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003480 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3481 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003482 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003483 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003484 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003485 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3486 + ((BitFieldSize % ByteSizeInBits) != 0);
3487 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003488 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003489
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003490 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003491 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003492 MaxUnionIvarSize));
3493 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003494 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003495 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003496}
3497
3498/// BuildIvarLayout - Builds ivar layout bitmap for the class
3499/// implementation for the __strong or __weak case.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003500/// The layout map displays which words in ivar list must be skipped
3501/// and which must be scanned by GC (see below). String is built of bytes.
3502/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003503/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003504/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003505/// represented by a 0x00 byte which also ends the string.
3506/// 1. when ForStrongLayout is true, following ivars are scanned:
3507/// - id, Class
3508/// - object *
3509/// - __strong anything
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003510///
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003511/// 2. When ForStrongLayout is false, following ivars are scanned:
3512/// - __weak anything
3513///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003514llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003515 const ObjCImplementationDecl *OMD,
3516 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003517 bool hasUnion = false;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003518
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003519 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003520 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003521 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003522 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003523
Chris Lattnerf1690852009-03-31 08:48:01 +00003524 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003525 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003526 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527
Daniel Dunbar37153282009-05-04 04:10:48 +00003528 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003529 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian11062e12010-02-19 00:31:17 +00003530 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003531 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3532 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003533
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003534 if (RecFields.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003535 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536
3537 SkipIvars.clear();
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003538 IvarsInfo.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003539
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003540 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003541 if (IvarsInfo.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003542 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003543
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003544 // Sort on byte position in case we encounterred a union nested in
3545 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003546 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003547 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003548 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003549 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003550
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003551 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003552 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003553 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003554 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003555 if (IvarsInfo[0].ivar_bytepos == 0) {
3556 WordsToSkip = 0;
3557 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003558 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003559 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3560 WordsToScan = IvarsInfo[0].ivar_size;
3561 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003562 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003563 unsigned int TailPrevGCObjC =
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003564 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003565 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003566 // consecutive 'scanned' object pointers.
3567 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003568 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003569 // Skip over 'gc'able object pointer which lay over each other.
3570 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3571 continue;
3572 // Must skip over 1 or more words. We save current skip/scan values
3573 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003574 SKIP_SCAN SkScan;
3575 SkScan.skip = WordsToSkip;
3576 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003577 SkipScanIvars.push_back(SkScan);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003578
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003579 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003580 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3581 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003582 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003583 WordsToSkip = 0;
3584 WordsToScan = IvarsInfo[i].ivar_size;
3585 }
3586 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003587 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003588 SKIP_SCAN SkScan;
3589 SkScan.skip = WordsToSkip;
3590 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003591 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003592 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003593
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003594 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003595 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003596 int LastByteSkipped =
3597 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003598 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599 int LastByteScanned =
3600 IvarsInfo[LastIndex].ivar_bytepos +
3601 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003602 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003603 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003604 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003605 SKIP_SCAN SkScan;
3606 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3607 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003608 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003609 }
3610 }
3611 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3612 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003613 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003614 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003615 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3616 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3617 // 0xM0 followed by 0x0N detected.
3618 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3619 for (int j = i+1; j < SkipScan; j++)
3620 SkipScanIvars[j] = SkipScanIvars[j+1];
3621 --SkipScan;
3622 }
3623 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003624
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003625 // Generate the string.
3626 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003627 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003628 unsigned char byte;
3629 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3630 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3631 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3632 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3633
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003634 // first skip big.
3635 for (unsigned int ix = 0; ix < skip_big; ix++)
3636 BitMap += (unsigned char)(0xf0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003637
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003638 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003639 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003640 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003641 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003642 byte |= 0xf;
3643 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003644 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003645 byte |= scan_small;
3646 scan_small = 0;
3647 }
3648 BitMap += byte;
3649 }
3650 // next scan big
3651 for (unsigned int ix = 0; ix < scan_big; ix++)
3652 BitMap += (unsigned char)(0x0f);
3653 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003654 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003655 byte = scan_small;
3656 BitMap += byte;
3657 }
3658 }
3659 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003660 unsigned char zero = 0;
3661 BitMap += zero;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003662
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003663 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003664 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003665 ForStrongLayout ? "strong" : "weak",
3666 OMD->getClassInterface()->getNameAsCString());
3667 const unsigned char *s = (unsigned char*)BitMap.c_str();
3668 for (unsigned i = 0; i < BitMap.size(); i++)
3669 if (!(s[i] & 0xf0))
3670 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3671 else
3672 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3673 printf("\n");
3674 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003675 llvm::GlobalVariable * Entry =
3676 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003677 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003678 ((ObjCABI == 2) ?
3679 "__TEXT,__objc_classname,cstring_literals" :
3680 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 1, true);
3682 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003683}
3684
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003685llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003686 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3687
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003688 // FIXME: Avoid std::string copying.
3689 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003690 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003691 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003692 ((ObjCABI == 2) ?
3693 "__TEXT,__objc_methname,cstring_literals" :
3694 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003695 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003696
Owen Andersona1cf15f2009-07-14 23:10:40 +00003697 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003698}
3699
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003700// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003701llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003702 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3703}
3704
3705// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003706llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003707 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3708}
3709
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003710llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003711 std::string TypeStr;
3712 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3713
3714 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003715
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003716 if (!Entry)
3717 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003718 llvm::ConstantArray::get(VMContext, TypeStr),
Jim Grosbach35eee092010-07-20 16:20:26 +00003719 ((ObjCABI == 2) ?
3720 "__TEXT,__objc_methtype,cstring_literals" :
3721 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003722 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003723
Owen Andersona1cf15f2009-07-14 23:10:40 +00003724 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003725}
3726
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003727llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003728 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003729 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3730 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003731
3732 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3733
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003734 if (!Entry)
3735 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003736 llvm::ConstantArray::get(VMContext, TypeStr),
Jim Grosbach35eee092010-07-20 16:20:26 +00003737 ((ObjCABI == 2) ?
3738 "__TEXT,__objc_methtype,cstring_literals" :
3739 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003740 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003741
Owen Andersona1cf15f2009-07-14 23:10:40 +00003742 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003743}
3744
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003745// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003746llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003747 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003749 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003750 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003751 llvm::ConstantArray::get(VMContext,
3752 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003753 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003754 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003755
Owen Andersona1cf15f2009-07-14 23:10:40 +00003756 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003757}
3758
3759// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003760// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003761llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003762CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3763 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003764 std::string TypeStr;
3765 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003766 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3767}
3768
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003769void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003770 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003771 llvm::SmallVectorImpl<char> &Name) {
3772 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003773 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003774 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3775 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003776 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003777 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003778 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003779 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003780}
3781
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003782void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003783 EmitModuleInfo();
3784
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003785 // Emit the dummy bodies for any protocols which were referenced but
3786 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003787 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003788 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3789 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003790 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003791
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003792 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003793 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003794 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003795 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003796 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003797 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003798 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003799 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003800 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003801 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003802 }
3803
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003804 // Add assembler directives to add lazy undefined symbol references
3805 // for classes which are referenced but not defined. This is
3806 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00003807 //
3808 // FIXME: It would be nice if we had an LLVM construct for this.
3809 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3810 llvm::SmallString<256> Asm;
3811 Asm += CGM.getModule().getModuleInlineAsm();
3812 if (!Asm.empty() && Asm.back() != '\n')
3813 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003814
Daniel Dunbar33063492009-09-07 00:20:42 +00003815 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00003816 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3817 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003818 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3819 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003820 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00003821 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003822 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00003823 }
3824
3825 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
3826 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
3827 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
3828 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003829
Daniel Dunbar33063492009-09-07 00:20:42 +00003830 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003831 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003832}
3833
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003834CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003835 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00003836 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003837 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003838 ObjCABI = 2;
3839}
3840
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003841/* *** */
3842
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003843ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003844 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003845 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3846 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003847
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003848 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003849 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003850 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003851 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003852 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003853
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003854 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003855 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003856 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003857
Mike Stumpf5408fe2009-05-16 07:57:57 +00003858 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3859 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003860 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003861 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003862
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003863 // I'm not sure I like this. The implicit coordination is a bit
3864 // gross. We should solve this in a reasonable fashion because this
3865 // is a pretty common task (match some runtime data structure with
3866 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003867
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003868 // FIXME: This is leaked.
3869 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003870
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003871 // struct _objc_super {
3872 // id self;
3873 // Class cls;
3874 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003875 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00003876 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003877 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003878 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003879 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003880 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003881 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003882 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00003883 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003884
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003885 SuperCTy = Ctx.getTagDeclType(RD);
3886 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003887
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003888 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003889 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3890
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003891 // struct _prop_t {
3892 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003893 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003894 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003895 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003896 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003897 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003898
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003899 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003900 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003901 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003902 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003903 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003904 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003905 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003906 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003907 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003908 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003909 PropertyListTy);
3910 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003911 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003912
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003913 // struct _objc_method {
3914 // SEL _cmd;
3915 // char *method_type;
3916 // char *_imp;
3917 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003918 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003919 Int8PtrTy,
3920 Int8PtrTy,
3921 NULL);
3922 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003923
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003924 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003925 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003926 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003927 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003928}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003930ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003931 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003932 // struct _objc_method_description {
3933 // SEL name;
3934 // char *types;
3935 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003936 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003937 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003938 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003939 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003940 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003941 MethodDescriptionTy);
3942
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003943 // struct _objc_method_description_list {
3944 // int count;
3945 // struct _objc_method_description[1];
3946 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003947 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003948 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003949 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003950 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003951 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003952 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003953
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003954 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003956 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003957
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003958 // Protocol description structures
3959
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003960 // struct _objc_protocol_extension {
3961 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3962 // struct _objc_method_description_list *optional_instance_methods;
3963 // struct _objc_method_description_list *optional_class_methods;
3964 // struct _objc_property_list *instance_properties;
3965 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003966 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003967 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003968 MethodDescriptionListPtrTy,
3969 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003970 PropertyListPtrTy,
3971 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003972 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003973 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003974
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003975 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003976 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003977
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003978 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003979
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003980 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
3981 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003982
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003983 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00003984 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00003985 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003986 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003987 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003988 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003989 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3990
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003991 // struct _objc_protocol {
3992 // struct _objc_protocol_extension *isa;
3993 // char *protocol_name;
3994 // struct _objc_protocol **_objc_protocol_list;
3995 // struct _objc_method_description_list *instance_methods;
3996 // struct _objc_method_description_list *class_methods;
3997 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003998 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003999 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004000 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004001 MethodDescriptionListPtrTy,
4002 MethodDescriptionListPtrTy,
4003 NULL);
4004 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4005
4006 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004007 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004008 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004009 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004010 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004011
4012 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004013 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004014 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004015
4016 // Class description structures
4017
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004018 // struct _objc_ivar {
4019 // char *ivar_name;
4020 // char *ivar_type;
4021 // int ivar_offset;
4022 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004023 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004024 Int8PtrTy,
4025 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004026 NULL);
4027 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4028
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004029 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004030 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004031 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004032 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004033
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004034 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004035 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004036 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004037 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004038
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004039 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004040 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004041 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004042 Int8PtrTy,
4043 PropertyListPtrTy,
4044 NULL);
4045 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004046 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004047
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004048 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004049
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004050 // struct _objc_class {
4051 // Class isa;
4052 // Class super_class;
4053 // char *name;
4054 // long version;
4055 // long info;
4056 // long instance_size;
4057 // struct _objc_ivar_list *ivars;
4058 // struct _objc_method_list *methods;
4059 // struct _objc_cache *cache;
4060 // struct _objc_protocol_list *protocols;
4061 // char *ivar_layout;
4062 // struct _objc_class_ext *ext;
4063 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004064 T = llvm::StructType::get(VMContext,
4065 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004066 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004067 Int8PtrTy,
4068 LongTy,
4069 LongTy,
4070 LongTy,
4071 IvarListPtrTy,
4072 MethodListPtrTy,
4073 CachePtrTy,
4074 ProtocolListPtrTy,
4075 Int8PtrTy,
4076 ClassExtensionPtrTy,
4077 NULL);
4078 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004079
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004080 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4081 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004082 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004083
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004084 // struct _objc_category {
4085 // char *category_name;
4086 // char *class_name;
4087 // struct _objc_method_list *instance_method;
4088 // struct _objc_method_list *class_method;
4089 // uint32_t size; // sizeof(struct _objc_category)
4090 // struct _objc_property_list *instance_properties;// category's @property
4091 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004092 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004093 Int8PtrTy,
4094 MethodListPtrTy,
4095 MethodListPtrTy,
4096 ProtocolListPtrTy,
4097 IntTy,
4098 PropertyListPtrTy,
4099 NULL);
4100 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4101
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004102 // Global metadata structures
4103
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004104 // struct _objc_symtab {
4105 // long sel_ref_cnt;
4106 // SEL *refs;
4107 // short cls_def_cnt;
4108 // short cat_def_cnt;
4109 // char *defs[cls_def_cnt + cat_def_cnt];
4110 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004111 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004112 SelectorPtrTy,
4113 ShortTy,
4114 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004115 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004116 NULL);
4117 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004118 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004119
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004120 // struct _objc_module {
4121 // long version;
4122 // long size; // sizeof(struct _objc_module)
4123 // char *name;
4124 // struct _objc_symtab* symtab;
4125 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004126 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004127 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004128 LongTy,
4129 Int8PtrTy,
4130 SymtabPtrTy,
4131 NULL);
4132 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004133
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004134
Mike Stumpf5408fe2009-05-16 07:57:57 +00004135 // FIXME: This is the size of the setjmp buffer and should be target
4136 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004137 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004138
Anders Carlsson124526b2008-09-09 10:10:21 +00004139 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004140 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004141 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004142
4143 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004144 llvm::StructType::get(VMContext,
4145 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4146 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004147 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004148 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004149 ExceptionDataTy);
4150
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004151}
4152
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004153ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004154 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004155 // struct _method_list_t {
4156 // uint32_t entsize; // sizeof(struct _objc_method)
4157 // uint32_t method_count;
4158 // struct _objc_method method_list[method_count];
4159 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004160 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004161 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004162 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004163 NULL);
4164 CGM.getModule().addTypeName("struct.__method_list_t",
4165 MethodListnfABITy);
4166 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004167 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004168
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004169 // struct _protocol_t {
4170 // id isa; // NULL
4171 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004172 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004173 // const struct method_list_t * const instance_methods;
4174 // const struct method_list_t * const class_methods;
4175 // const struct method_list_t *optionalInstanceMethods;
4176 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004177 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004178 // const uint32_t size; // sizeof(struct _protocol_t)
4179 // const uint32_t flags; // = 0
4180 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004181
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004182 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004183 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004184
Owen Anderson47a434f2009-08-05 23:18:46 +00004185 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004186 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004187 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004188 ProtocolListTyHolder),
4189 MethodListnfABIPtrTy,
4190 MethodListnfABIPtrTy,
4191 MethodListnfABIPtrTy,
4192 MethodListnfABIPtrTy,
4193 PropertyListPtrTy,
4194 IntTy,
4195 IntTy,
4196 NULL);
4197 CGM.getModule().addTypeName("struct._protocol_t",
4198 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004199
4200 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004202
Fariborz Jahanianda320092009-01-29 19:24:30 +00004203 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004204 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004205 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004206 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004207 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004208 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004209 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004210 NULL);
4211 CGM.getModule().addTypeName("struct._objc_protocol_list",
4212 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004213 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214 ProtocolListnfABITy);
4215
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004216 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004217 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004219 // struct _ivar_t {
4220 // unsigned long int *offset; // pointer to ivar offset location
4221 // char *name;
4222 // char *type;
4223 // uint32_t alignment;
4224 // uint32_t size;
4225 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004226 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004227 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004228 Int8PtrTy,
4229 Int8PtrTy,
4230 IntTy,
4231 IntTy,
4232 NULL);
4233 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004234
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004235 // struct _ivar_list_t {
4236 // uint32 entsize; // sizeof(struct _ivar_t)
4237 // uint32 count;
4238 // struct _iver_t list[count];
4239 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004240 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004241 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004242 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004243 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004244 NULL);
4245 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004246
Owen Anderson96e0fc72009-07-29 22:16:19 +00004247 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004248
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004249 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004250 // uint32_t const flags;
4251 // uint32_t const instanceStart;
4252 // uint32_t const instanceSize;
4253 // uint32_t const reserved; // only when building for 64bit targets
4254 // const uint8_t * const ivarLayout;
4255 // const char *const name;
4256 // const struct _method_list_t * const baseMethods;
4257 // const struct _objc_protocol_list *const baseProtocols;
4258 // const struct _ivar_list_t *const ivars;
4259 // const uint8_t * const weakIvarLayout;
4260 // const struct _prop_list_t * const properties;
4261 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004262
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004263 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004264 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004265 IntTy,
4266 IntTy,
4267 Int8PtrTy,
4268 Int8PtrTy,
4269 MethodListnfABIPtrTy,
4270 ProtocolListnfABIPtrTy,
4271 IvarListnfABIPtrTy,
4272 Int8PtrTy,
4273 PropertyListPtrTy,
4274 NULL);
4275 CGM.getModule().addTypeName("struct._class_ro_t",
4276 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004277
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004278 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4279 std::vector<const llvm::Type*> Params;
4280 Params.push_back(ObjectPtrTy);
4281 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004282 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004283 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4284
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004285 // struct _class_t {
4286 // struct _class_t *isa;
4287 // struct _class_t * const superclass;
4288 // void *cache;
4289 // IMP *vtable;
4290 // struct class_ro_t *ro;
4291 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004292
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004293 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004294 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004295 llvm::StructType::get(VMContext,
4296 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004297 llvm::PointerType::getUnqual(ClassTyHolder),
4298 CachePtrTy,
4299 llvm::PointerType::getUnqual(ImpnfABITy),
4300 llvm::PointerType::getUnqual(ClassRonfABITy),
4301 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004302 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4303
4304 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004305 ClassnfABITy);
4306
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004307 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004308 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004309
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004310 // struct _category_t {
4311 // const char * const name;
4312 // struct _class_t *const cls;
4313 // const struct _method_list_t * const instance_methods;
4314 // const struct _method_list_t * const class_methods;
4315 // const struct _protocol_list_t * const protocols;
4316 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004317 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004318 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004319 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004320 MethodListnfABIPtrTy,
4321 MethodListnfABIPtrTy,
4322 ProtocolListnfABIPtrTy,
4323 PropertyListPtrTy,
4324 NULL);
4325 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004326
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004327 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004328 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4329 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004331 // MessageRefTy - LLVM for:
4332 // struct _message_ref_t {
4333 // IMP messenger;
4334 // SEL name;
4335 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004336
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004337 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004338 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004339 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004340 SourceLocation(),
4341 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004342 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004343 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004344 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004345 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004346 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004347
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004348 MessageRefCTy = Ctx.getTagDeclType(RD);
4349 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4350 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004351
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004352 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004353 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004354
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004355 // SuperMessageRefTy - LLVM for:
4356 // struct _super_message_ref_t {
4357 // SUPER_IMP messenger;
4358 // SEL name;
4359 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004360 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004361 SelectorPtrTy,
4362 NULL);
4363 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004364
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004365 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004366 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4367
Daniel Dunbare588b992009-03-01 04:46:24 +00004368
4369 // struct objc_typeinfo {
4370 // const void** vtable; // objc_ehtype_vtable + 2
4371 // const char* name; // c++ typeinfo string
4372 // Class cls;
4373 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004374 EHTypeTy = llvm::StructType::get(VMContext,
4375 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004376 Int8PtrTy,
4377 ClassnfABIPtrTy,
4378 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004379 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004380 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004381}
4382
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004383llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004384 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004386 return NULL;
4387}
4388
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004389void CGObjCNonFragileABIMac::AddModuleClassList(const
4390 std::vector<llvm::GlobalValue*>
4391 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004392 const char *SymbolName,
4393 const char *SectionName) {
4394 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004395
Daniel Dunbar463b8762009-05-15 21:48:48 +00004396 if (!NumClasses)
4397 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004398
Daniel Dunbar463b8762009-05-15 21:48:48 +00004399 std::vector<llvm::Constant*> Symbols(NumClasses);
4400 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004401 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004402 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004404 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004405 NumClasses),
4406 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004407
Daniel Dunbar463b8762009-05-15 21:48:48 +00004408 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004409 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004410 llvm::GlobalValue::InternalLinkage,
4411 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004412 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004413 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004414 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004415 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004416}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004417
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004418void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4419 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004420
Daniel Dunbar463b8762009-05-15 21:48:48 +00004421 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004422 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004424 "\01L_OBJC_LABEL_CLASS_$",
4425 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004426
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004427 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4428 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4429 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4430 continue;
4431 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004432 }
4433
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004434 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4435 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4436 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4437 continue;
4438 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4439 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004440
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004441 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004442 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4443 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004445 // Build list of all implemented category addresses in array
4446 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004447 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004448 "\01L_OBJC_LABEL_CATEGORY_$",
4449 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004451 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4452 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004453
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004454 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004455}
4456
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004457/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004458/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004459/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004460/// message dispatch call for all the rest.
4461///
4462bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004463 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4464 default:
4465 assert(0 && "Invalid dispatch method!");
4466 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004467 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004468 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004469 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004470 case CodeGenOptions::Mixed:
4471 break;
4472 }
4473
4474 // If so, see whether this selector is in the white-list of things which must
4475 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004476 if (NonLegacyDispatchMethods.empty()) {
4477 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4478 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4479 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4480 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4481 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4482 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4483 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4484 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4485 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4486 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004487
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004488 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4489 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4490 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4491 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4492 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4493 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4494 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4495 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004497 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4499 &CGM.getContext().Idents.get("objects"),
4500 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004501 };
4502 NonLegacyDispatchMethods.insert(
4503 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004504 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004505
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004506 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004507}
4508
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004509// Metadata flags
4510enum MetaDataDlags {
4511 CLS = 0x0,
4512 CLS_META = 0x1,
4513 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004514 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004515 CLS_EXCEPTION = 0x20
4516};
4517/// BuildClassRoTInitializer - generate meta-data for:
4518/// struct _class_ro_t {
4519/// uint32_t const flags;
4520/// uint32_t const instanceStart;
4521/// uint32_t const instanceSize;
4522/// uint32_t const reserved; // only when building for 64bit targets
4523/// const uint8_t * const ivarLayout;
4524/// const char *const name;
4525/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004526/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004527/// const struct _ivar_list_t *const ivars;
4528/// const uint8_t * const weakIvarLayout;
4529/// const struct _prop_list_t * const properties;
4530/// }
4531///
4532llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004533 unsigned flags,
4534 unsigned InstanceStart,
4535 unsigned InstanceSize,
4536 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004537 std::string ClassName = ID->getNameAsString();
4538 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004539 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4540 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4541 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004542 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004543 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4544 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004545 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004546 // const struct _method_list_t * const baseMethods;
4547 std::vector<llvm::Constant*> Methods;
4548 std::string MethodListName("\01l_OBJC_$_");
4549 if (flags & CLS_META) {
4550 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004552 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004553 // Class methods should always be defined.
4554 Methods.push_back(GetMethodConstant(*i));
4555 }
4556 } else {
4557 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004558 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004559 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004560 // Instance methods should always be defined.
4561 Methods.push_back(GetMethodConstant(*i));
4562 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004563 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004564 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004565 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004567 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4568 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004569
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004570 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4571 if (llvm::Constant *C = GetMethodConstant(MD))
4572 Methods.push_back(C);
4573 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4574 if (llvm::Constant *C = GetMethodConstant(MD))
4575 Methods.push_back(C);
4576 }
4577 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004579 Values[ 5] = EmitMethodList(MethodListName,
4580 "__DATA, __objc_const", Methods);
4581
Fariborz Jahanianda320092009-01-29 19:24:30 +00004582 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4583 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004585 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004586 OID->protocol_begin(),
4587 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004588
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004589 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004590 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004591 else
4592 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004593 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4594 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004595 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004596 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004597 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004598 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4599 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004600 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004601 Values);
4602 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004603 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4604 llvm::GlobalValue::InternalLinkage,
4605 Init,
4606 (flags & CLS_META) ?
4607 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4608 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004609 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004610 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004611 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004612 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004613
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004614}
4615
4616/// BuildClassMetaData - This routine defines that to-level meta-data
4617/// for the given ClassName for:
4618/// struct _class_t {
4619/// struct _class_t *isa;
4620/// struct _class_t * const superclass;
4621/// void *cache;
4622/// IMP *vtable;
4623/// struct class_ro_t *ro;
4624/// }
4625///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004626llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004627 std::string &ClassName,
4628 llvm::Constant *IsAGV,
4629 llvm::Constant *SuperClassGV,
4630 llvm::Constant *ClassRoGV,
4631 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004632 std::vector<llvm::Constant*> Values(5);
4633 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004634 Values[1] = SuperClassGV;
4635 if (!Values[1])
4636 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004637 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4638 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4639 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004640 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004641 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004642 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4643 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004644 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004645 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004646 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004647 if (HiddenVisibility)
4648 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004649 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004650}
4651
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004652bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004653CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004654 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004655}
4656
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004657void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004658 uint32_t &InstanceStart,
4659 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004660 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004661 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004662
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004663 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004664 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004665
4666 // If there are no fields, the start is the same as the end.
4667 if (!RL.getFieldCount())
4668 InstanceStart = InstanceSize;
4669 else
4670 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004671}
4672
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004673void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4674 std::string ClassName = ID->getNameAsString();
4675 if (!ObjCEmptyCacheVar) {
4676 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004677 CGM.getModule(),
4678 ObjCTypes.CacheTy,
4679 false,
4680 llvm::GlobalValue::ExternalLinkage,
4681 0,
4682 "_objc_empty_cache");
4683
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004684 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004685 CGM.getModule(),
4686 ObjCTypes.ImpnfABITy,
4687 false,
4688 llvm::GlobalValue::ExternalLinkage,
4689 0,
4690 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004691 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004693 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004694 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004695 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004696 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004697 uint32_t InstanceSize = InstanceStart;
4698 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004699 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4700 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004702 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004703
4704 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004705 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004706 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004707 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004708 if (ID->getNumIvarInitializers())
4709 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004710 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004711 // class is root
4712 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004713 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004714 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004715 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004716 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004717 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4718 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4719 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004720 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004721 if (Root->hasAttr<WeakImportAttr>())
4722 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004723 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004724 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004725 ObjCMetaClassName +
4726 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004727 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004728 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4729 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004730 }
4731 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4732 InstanceStart,
4733 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004734 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004736 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4737 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004738 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004739
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004740 // Metadata for the class
4741 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004742 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004743 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004744 if (ID->getNumIvarInitializers())
4745 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004746
Douglas Gregor68584ed2009-06-18 16:11:24 +00004747 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004748 flags |= CLS_EXCEPTION;
4749
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004750 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004751 flags |= CLS_ROOT;
4752 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004753 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004754 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004755 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004756 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004757 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004758 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4759 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004760 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004761 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004762 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004763 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004765 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004766
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004767 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004768 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004769 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4770 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004771 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004772
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004773 // Determine if this class is also "non-lazy".
4774 if (ImplementationIsNonLazy(ID))
4775 DefinedNonLazyClasses.push_back(ClassMD);
4776
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004777 // Force the definition of the EHType if necessary.
4778 if (flags & CLS_EXCEPTION)
4779 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004780}
4781
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004782/// GenerateProtocolRef - This routine is called to generate code for
4783/// a protocol reference expression; as in:
4784/// @code
4785/// @protocol(Proto1);
4786/// @endcode
4787/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4788/// which will hold address of the protocol meta-data.
4789///
4790llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004791 const ObjCProtocolDecl *PD) {
4792
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004793 // This routine is called for @protocol only. So, we must build definition
4794 // of protocol's meta-data (not a reference to it!)
4795 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004796 llvm::Constant *Init =
4797 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4798 ObjCTypes.ExternalProtocolPtrTy);
4799
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004800 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4801 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004802
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004803 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4804 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004805 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004806 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004807 CGM.getModule(),
4808 Init->getType(), false,
4809 llvm::GlobalValue::WeakAnyLinkage,
4810 Init,
4811 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004812 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4813 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004814 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004815 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004816}
4817
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004818/// GenerateCategory - Build metadata for a category implementation.
4819/// struct _category_t {
4820/// const char * const name;
4821/// struct _class_t *const cls;
4822/// const struct _method_list_t * const instance_methods;
4823/// const struct _method_list_t * const class_methods;
4824/// const struct _protocol_list_t * const protocols;
4825/// const struct _prop_list_t * const properties;
4826/// }
4827///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004828void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004829 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004830 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004831 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4832 "_$_" + OCD->getNameAsString());
4833 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004834 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004835
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004836 std::vector<llvm::Constant*> Values(6);
4837 Values[0] = GetClassName(OCD->getIdentifier());
4838 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004839 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00004840 if (Interface->hasAttr<WeakImportAttr>())
4841 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4842
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004843 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004844 std::vector<llvm::Constant*> Methods;
4845 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004846 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004847 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848
4849 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004850 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004851 // Instance methods should always be defined.
4852 Methods.push_back(GetMethodConstant(*i));
4853 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004854
4855 Values[2] = EmitMethodList(MethodListName,
4856 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004857 Methods);
4858
4859 MethodListName = Prefix;
4860 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4861 OCD->getNameAsString();
4862 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004863 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004864 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004865 // Class methods should always be defined.
4866 Methods.push_back(GetMethodConstant(*i));
4867 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868
4869 Values[3] = EmitMethodList(MethodListName,
4870 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004871 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004873 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004874 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004875 llvm::SmallString<256> ExtName;
4876 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
4877 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004878 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004879 + Interface->getName() + "_$_"
4880 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004881 Category->protocol_begin(),
4882 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004883 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
4884 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00004885 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004886 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4887 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004888 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004889
4890 llvm::Constant *Init =
4891 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004892 Values);
4893 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004894 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004895 false,
4896 llvm::GlobalValue::InternalLinkage,
4897 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004898 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004899 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004900 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004901 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004902 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004903 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004904
4905 // Determine if this category is also "non-lazy".
4906 if (ImplementationIsNonLazy(OCD))
4907 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004908}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004909
4910/// GetMethodConstant - Return a struct objc_method constant for the
4911/// given method if it has been defined. The result is null if the
4912/// method has not been defined. The return value has type MethodPtrTy.
4913llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004914 const ObjCMethodDecl *MD) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004915 // FIXME: Use DenseMap::lookup
4916 llvm::Function *Fn = MethodDefinitions[MD];
4917 if (!Fn)
4918 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004919
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004920 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004921 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004922 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004924 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004925 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004926 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004927}
4928
4929/// EmitMethodList - Build meta-data for method declarations
4930/// struct _method_list_t {
4931/// uint32_t entsize; // sizeof(struct _objc_method)
4932/// uint32_t method_count;
4933/// struct _objc_method method_list[method_count];
4934/// }
4935///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004936llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
4937 const char *Section,
4938 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004939 // Return null for empty list.
4940 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00004941 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004942
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004943 std::vector<llvm::Constant*> Values(3);
4944 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004945 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004946 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004947 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004948 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004949 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004950 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004951 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00004952 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004953
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004954 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004955 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004956 llvm::GlobalValue::InternalLinkage,
4957 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004958 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004959 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004960 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004961 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004962 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004963 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004964 ObjCTypes.MethodListnfABIPtrTy);
4965}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004966
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004967/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4968/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00004969llvm::GlobalVariable *
4970CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4971 const ObjCIvarDecl *Ivar) {
4972 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00004973 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004974 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004976 CGM.getModule().getGlobalVariable(Name);
4977 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004978 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004979 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004980 false,
4981 llvm::GlobalValue::ExternalLinkage,
4982 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004983 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004984 return IvarOffsetGV;
4985}
4986
Daniel Dunbare83be122010-04-02 21:14:02 +00004987llvm::Constant *
4988CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
4989 const ObjCIvarDecl *Ivar,
4990 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004991 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004992 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004993 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004994 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004995 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004996
Mike Stumpf5408fe2009-05-16 07:57:57 +00004997 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4998 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004999 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5000 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5001 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005002 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005003 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005004 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005005 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005006 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005007}
5008
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005009/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005010/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005011/// IvarListnfABIPtrTy.
5012/// struct _ivar_t {
5013/// unsigned long int *offset; // pointer to ivar offset location
5014/// char *name;
5015/// char *type;
5016/// uint32_t alignment;
5017/// uint32_t size;
5018/// }
5019/// struct _ivar_list_t {
5020/// uint32 entsize; // sizeof(struct _ivar_t)
5021/// uint32 count;
5022/// struct _iver_t list[count];
5023/// }
5024///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005025
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005026llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005027 const ObjCImplementationDecl *ID) {
5028
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005029 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005030
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005031 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5032 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005033
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005034 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005035
Daniel Dunbar91636d62009-04-20 00:33:43 +00005036 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005037 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005038 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005039
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005040 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5041 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005042 // Ignore unnamed bit-fields.
5043 if (!IVD->getDeclName())
5044 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005045 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005046 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005047 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5048 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005049 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005050 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005051 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005052 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005054 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005055 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005056 // NOTE. Size of a bitfield does not match gcc's, because of the
5057 // way bitfields are treated special in each. But I am told that
5058 // 'size' for bitfield ivars is ignored by the runtime so it does
5059 // not matter. If it matters, there is enough info to get the
5060 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005061 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005062 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005063 }
5064 // Return null for empty list.
5065 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005066 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005067 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005068 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005069 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5070 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005071 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005072 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005073 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005074 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005075 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5076 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005077 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005078 llvm::GlobalValue::InternalLinkage,
5079 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005080 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005081 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005082 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005083 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005084
Chris Lattnerad64e022009-07-17 23:57:13 +00005085 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005086 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005087}
5088
5089llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005090 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005091 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005092
Fariborz Jahanianda320092009-01-29 19:24:30 +00005093 if (!Entry) {
5094 // We use the initializer as a marker of whether this is a forward
5095 // reference or not. At module finalization we add the empty
5096 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005097 Entry =
5098 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5099 llvm::GlobalValue::ExternalLinkage,
5100 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005101 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005102 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005103 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005104
Fariborz Jahanianda320092009-01-29 19:24:30 +00005105 return Entry;
5106}
5107
5108/// GetOrEmitProtocol - Generate the protocol meta-data:
5109/// @code
5110/// struct _protocol_t {
5111/// id isa; // NULL
5112/// const char * const protocol_name;
5113/// const struct _protocol_list_t * protocol_list; // super protocols
5114/// const struct method_list_t * const instance_methods;
5115/// const struct method_list_t * const class_methods;
5116/// const struct method_list_t *optionalInstanceMethods;
5117/// const struct method_list_t *optionalClassMethods;
5118/// const struct _prop_list_t * properties;
5119/// const uint32_t size; // sizeof(struct _protocol_t)
5120/// const uint32_t flags; // = 0
5121/// }
5122/// @endcode
5123///
5124
5125llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005126 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005127 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005128
Fariborz Jahanianda320092009-01-29 19:24:30 +00005129 // Early exit if a defining object has already been generated.
5130 if (Entry && Entry->hasInitializer())
5131 return Entry;
5132
Fariborz Jahanianda320092009-01-29 19:24:30 +00005133 // Construct method lists.
5134 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5135 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005137 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005138 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005139 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005140 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5141 OptInstanceMethods.push_back(C);
5142 } else {
5143 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146
5147 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005148 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005149 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005150 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005151 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5152 OptClassMethods.push_back(C);
5153 } else {
5154 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005155 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005156 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005157
Fariborz Jahanianda320092009-01-29 19:24:30 +00005158 std::vector<llvm::Constant*> Values(10);
5159 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005160 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005161 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005162 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5163 PD->protocol_begin(),
5164 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005165
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005166 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005167 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005168 "__DATA, __objc_const",
5169 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005170 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005171 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005172 "__DATA, __objc_const",
5173 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005174 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005175 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005176 "__DATA, __objc_const",
5177 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005178 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005179 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005180 "__DATA, __objc_const",
5181 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005182 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005183 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005184 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005185 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005186 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005187 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005188 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005189 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005190
Fariborz Jahanianda320092009-01-29 19:24:30 +00005191 if (Entry) {
5192 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005193 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005194 Entry->setInitializer(Init);
5195 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005196 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005197 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5198 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5199 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005200 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005201 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005202 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005203 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005204 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005205 CGM.AddUsedGlobal(Entry);
5206
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005207 // Use this protocol meta-data to build protocol list table in section
5208 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005209 llvm::GlobalVariable *PTGV =
5210 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5211 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5212 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005213 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005214 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005215 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005216 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005217 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005218 return Entry;
5219}
5220
5221/// EmitProtocolList - Generate protocol list meta-data:
5222/// @code
5223/// struct _protocol_list_t {
5224/// long protocol_count; // Note, this is 32/64 bit
5225/// struct _protocol_t[protocol_count];
5226/// }
5227/// @endcode
5228///
5229llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005230CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5231 ObjCProtocolDecl::protocol_iterator begin,
5232 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005233 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005234
Fariborz Jahanianda320092009-01-29 19:24:30 +00005235 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005236 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005237 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238
Daniel Dunbar948e2582009-02-15 07:36:20 +00005239 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005240 llvm::SmallString<256> TmpName;
5241 Name.toVector(TmpName);
5242 llvm::GlobalVariable *GV =
5243 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005244 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005245 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005246
Daniel Dunbar948e2582009-02-15 07:36:20 +00005247 for (; begin != end; ++begin)
5248 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5249
Fariborz Jahanianda320092009-01-29 19:24:30 +00005250 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005251 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252 ObjCTypes.ProtocolnfABIPtrTy));
5253
Fariborz Jahanianda320092009-01-29 19:24:30 +00005254 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005255 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005256 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005257 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005258 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005259 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005260 ProtocolRefs.size()),
5261 ProtocolRefs);
5262
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005263 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005264 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005265 llvm::GlobalValue::InternalLinkage,
5266 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005267 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005268 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005269 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005270 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005271 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005272 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005273 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005274}
5275
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005276/// GetMethodDescriptionConstant - This routine build following meta-data:
5277/// struct _objc_method {
5278/// SEL _cmd;
5279/// char *method_type;
5280/// char *_imp;
5281/// }
5282
5283llvm::Constant *
5284CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5285 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005286 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005287 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5288 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005289 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005290 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005291 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005292 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005293}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005294
5295/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5296/// This code gen. amounts to generating code for:
5297/// @code
5298/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5299/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005300///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005301LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005302 CodeGen::CodeGenFunction &CGF,
5303 QualType ObjectTy,
5304 llvm::Value *BaseValue,
5305 const ObjCIvarDecl *Ivar,
5306 unsigned CVRQualifiers) {
5307 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005308 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5309 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005310}
5311
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005312llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005313 CodeGen::CodeGenFunction &CGF,
5314 const ObjCInterfaceDecl *Interface,
5315 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005316 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005317}
5318
Fariborz Jahanian46551122009-02-04 00:22:57 +00005319CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005320 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005321 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322 QualType ResultType,
5323 Selector Sel,
5324 llvm::Value *Receiver,
5325 QualType Arg0Ty,
5326 bool IsSuper,
5327 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005328 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5329 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005330 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005331 llvm::Value *Arg0 = Receiver;
5332 if (!IsSuper)
5333 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005335 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005336 // FIXME. This is too much work to get the ABI-specific result type needed to
5337 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005338 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005339 = Types.getFunctionInfo(ResultType, CallArgList(),
5340 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005341 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005342 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005343 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005344#if 0
5345 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005346 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005347 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005348 // FIXME. Is there a better way of getting these names.
5349 // They are available in RuntimeFunctions vector pair.
5350 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005351 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005352#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005353 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005354 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005355 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005356 } else {
5357 Fn = ObjCTypes.getMessageSendStretFixupFn();
5358 Name += "objc_msgSend_stret_fixup";
5359 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005360 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5361 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5362 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005363 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005364#if 0
5365// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005366 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005367 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005368 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005370#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005371 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005372 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005373 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374 } else {
5375 Fn = ObjCTypes.getMessageSendFixupFn();
5376 Name += "objc_msgSend_fixup";
5377 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005378 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005379 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005380 Name += '_';
5381 std::string SelName(Sel.getAsString());
5382 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005383 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005384 if (SelName[i] == ':')
5385 SelName[i] = '_';
5386 Name += SelName;
5387 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5388 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005389 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005390 std::vector<llvm::Constant*> Values(2);
5391 Values[0] = Fn;
5392 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005393 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005394 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005395 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005396 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005397 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005398 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005399 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005400 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005401 }
5402 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005404 CallArgList ActualArgs;
5405 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005406 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005407 ObjCTypes.MessageRefCPtrTy));
5408 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005409 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005410 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005411 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5412 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005413 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005414 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005415 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005416 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005417}
5418
5419/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005420CodeGen::RValue
5421CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005422 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005423 QualType ResultType,
5424 Selector Sel,
5425 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005426 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005427 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005428 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005429 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005430 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5431 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005433 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005434 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435 Receiver, CGF.getContext().getObjCIdType(),
5436 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005437}
5438
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005439llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005440CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005441 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5442
Daniel Dunbardfff2302009-03-02 05:18:14 +00005443 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005445 false, llvm::GlobalValue::ExternalLinkage,
5446 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005447 }
5448
5449 return GV;
5450}
5451
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005452llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5453 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005454 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005456 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005457 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005458 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 Entry =
5460 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005461 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005463 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005464 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005465 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005466 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005467 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005468 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005469 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005470
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005471 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005472}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005473
Daniel Dunbar11394522009-04-18 08:51:00 +00005474llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005475CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005476 const ObjCInterfaceDecl *ID) {
5477 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478
Daniel Dunbar11394522009-04-18 08:51:00 +00005479 if (!Entry) {
5480 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5481 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005482 Entry =
5483 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005484 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005485 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005486 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005487 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005488 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005489 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005490 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005491 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005492 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005493
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005494 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005495}
5496
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005497/// EmitMetaClassRef - Return a Value * of the address of _class_t
5498/// meta-data
5499///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005500llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5501 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005502 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5503 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005504 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005505
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005506 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005507 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005508 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005509 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005510 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005512 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005513 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005514 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005515 ObjCTypes.ClassnfABIPtrTy));
5516
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005517 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005518 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005519
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005520 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005521}
5522
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005523/// GetClass - Return a reference to the class for the given interface
5524/// decl.
5525llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5526 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005527 if (ID->hasAttr<WeakImportAttr>()) {
5528 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5529 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5530 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5531 }
5532
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005533 return EmitClassRef(Builder, ID);
5534}
5535
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005536/// Generates a message send where the super is the receiver. This is
5537/// a message send to self with special delivery semantics indicating
5538/// which class's method should be called.
5539CodeGen::RValue
5540CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005541 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005542 QualType ResultType,
5543 Selector Sel,
5544 const ObjCInterfaceDecl *Class,
5545 bool isCategoryImpl,
5546 llvm::Value *Receiver,
5547 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005548 const CodeGen::CallArgList &CallArgs,
5549 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005550 // ...
5551 // Create and init a super structure; this is a (receiver, class)
5552 // pair we will pass to objc_msgSendSuper.
5553 llvm::Value *ObjCSuper =
5554 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005555
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005556 llvm::Value *ReceiverAsObject =
5557 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5558 CGF.Builder.CreateStore(ReceiverAsObject,
5559 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005561 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005562 llvm::Value *Target;
5563 if (IsClassMessage) {
5564 if (isCategoryImpl) {
5565 // Message sent to "super' in a class method defined in
5566 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005567 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005568 Target = CGF.Builder.CreateStructGEP(Target, 0);
5569 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005570 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005571 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005572 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005573 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005574
Mike Stumpf5408fe2009-05-16 07:57:57 +00005575 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5576 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005577 const llvm::Type *ClassTy =
5578 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5579 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5580 CGF.Builder.CreateStore(Target,
5581 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005583 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005584 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5585 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005586 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005587 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005588 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589 ObjCSuper, ObjCTypes.SuperPtrCTy,
5590 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005591}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005592
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005593llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005594 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005595 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005596
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005597 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005598 llvm::Constant *Casted =
5599 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5600 ObjCTypes.SelectorPtrTy);
5601 Entry =
5602 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5603 llvm::GlobalValue::InternalLinkage,
5604 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005605 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005606 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005607 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005608
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005609 if (lval)
5610 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005611 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005612}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005613/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005614/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005615///
5616void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005617 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005618 llvm::Value *dst,
5619 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005620 const llvm::Type * SrcTy = src->getType();
5621 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005622 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005623 assert(Size <= 8 && "does not support size > 8");
5624 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5625 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005626 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5627 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005628 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5629 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005630 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5631 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005632 return;
5633}
5634
5635/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5636/// objc_assign_strongCast (id src, id *dst)
5637///
5638void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005640 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005641 const llvm::Type * SrcTy = src->getType();
5642 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005643 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005644 assert(Size <= 8 && "does not support size > 8");
5645 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005646 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005647 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5648 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005649 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5650 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005651 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005652 src, dst, "weakassign");
5653 return;
5654}
5655
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005656void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657 CodeGen::CodeGenFunction &CGF,
5658 llvm::Value *DestPtr,
5659 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005660 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005661 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5662 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005663 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005664 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005665 return;
5666}
5667
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005668/// EmitObjCWeakRead - Code gen for loading value of a __weak
5669/// object: objc_read_weak (id *src)
5670///
5671llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005672 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005673 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005674 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5676 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005677 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005678 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005679 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005680 return read_weak;
5681}
5682
5683/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5684/// objc_assign_weak (id src, id *dst)
5685///
5686void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005687 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005688 const llvm::Type * SrcTy = src->getType();
5689 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005690 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005691 assert(Size <= 8 && "does not support size > 8");
5692 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5693 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005694 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5695 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005696 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5697 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005698 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005699 src, dst, "weakassign");
5700 return;
5701}
5702
5703/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5704/// objc_assign_global (id src, id *dst)
5705///
5706void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005707 llvm::Value *src, llvm::Value *dst,
5708 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005709 const llvm::Type * SrcTy = src->getType();
5710 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005711 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005712 assert(Size <= 8 && "does not support size > 8");
5713 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5714 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005715 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5716 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005717 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5718 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005719 if (!threadlocal)
5720 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5721 src, dst, "globalassign");
5722 else
5723 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5724 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005725 return;
5726}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005727
John McCall740e8072010-07-21 00:41:47 +00005728namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005729 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005730 llvm::Value *SyncExitFn;
5731 llvm::Value *SyncArg;
5732 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5733 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5734
5735 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5736 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5737 }
5738 };
5739}
5740
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741void
John McCallf1549f62010-07-06 01:34:17 +00005742CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5743 const ObjCAtSynchronizedStmt &S) {
5744 // Evaluate the lock operand. This should dominate the cleanup.
5745 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005746
John McCallf1549f62010-07-06 01:34:17 +00005747 // Acquire the lock.
5748 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5749 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5750 ->setDoesNotThrow();
5751
5752 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005753 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5754 ObjCTypes.getSyncExitFn(),
5755 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005756
John McCallf1549f62010-07-06 01:34:17 +00005757 // Emit the body of the statement.
5758 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005759
John McCallf1549f62010-07-06 01:34:17 +00005760 // Pop the lock-release cleanup.
5761 CGF.PopCleanupBlock();
5762}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005763
John McCallf1549f62010-07-06 01:34:17 +00005764namespace {
5765 struct CatchHandler {
5766 const VarDecl *Variable;
5767 const Stmt *Body;
5768 llvm::BasicBlock *Block;
5769 llvm::Value *TypeInfo;
5770 };
John McCall8e3f8612010-07-13 22:12:14 +00005771
John McCall1f0fca52010-07-21 07:22:38 +00005772 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005773 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5774 MightThrow(MightThrow), Fn(Fn) {}
5775 bool MightThrow;
5776 llvm::Value *Fn;
5777
5778 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5779 if (!MightThrow) {
5780 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5781 return;
5782 }
5783
5784 CGF.EmitCallOrInvoke(Fn, 0, 0);
5785 }
5786 };
John McCallf1549f62010-07-06 01:34:17 +00005787}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788
John McCall5a180392010-07-24 00:37:23 +00005789llvm::Constant *
5790CGObjCNonFragileABIMac::GetEHType(QualType T) {
5791 // There's a particular fixed type info for 'id'.
5792 if (T->isObjCIdType() ||
5793 T->isObjCQualifiedIdType()) {
5794 llvm::Constant *IDEHType =
5795 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5796 if (!IDEHType)
5797 IDEHType =
5798 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5799 false,
5800 llvm::GlobalValue::ExternalLinkage,
5801 0, "OBJC_EHTYPE_id");
5802 return IDEHType;
5803 }
5804
5805 // All other types should be Objective-C interface pointer types.
5806 const ObjCObjectPointerType *PT =
5807 T->getAs<ObjCObjectPointerType>();
5808 assert(PT && "Invalid @catch type.");
5809 const ObjCInterfaceType *IT = PT->getInterfaceType();
5810 assert(IT && "Invalid @catch type.");
5811 return GetInterfaceEHType(IT->getDecl(), false);
5812}
5813
John McCallf1549f62010-07-06 01:34:17 +00005814void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5815 const ObjCAtTryStmt &S) {
5816 // Jump destination for falling out of catch bodies.
5817 CodeGenFunction::JumpDest Cont;
5818 if (S.getNumCatchStmts())
5819 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005820
John McCallf1549f62010-07-06 01:34:17 +00005821 CodeGenFunction::FinallyInfo FinallyInfo;
5822 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
5823 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
5824 ObjCTypes.getObjCBeginCatchFn(),
5825 ObjCTypes.getObjCEndCatchFn(),
5826 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005827
John McCallf1549f62010-07-06 01:34:17 +00005828 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005829
John McCallf1549f62010-07-06 01:34:17 +00005830 // Enter the catch, if there is one.
5831 if (S.getNumCatchStmts()) {
5832 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
5833 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00005834 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005835
John McCallf1549f62010-07-06 01:34:17 +00005836 Handlers.push_back(CatchHandler());
5837 CatchHandler &Handler = Handlers.back();
5838 Handler.Variable = CatchDecl;
5839 Handler.Body = CatchStmt->getCatchBody();
5840 Handler.Block = CGF.createBasicBlock("catch");
5841
5842 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005843 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00005844 Handler.TypeInfo = 0; // catch-all
5845 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005846 break;
5847 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005848
John McCall5a180392010-07-24 00:37:23 +00005849 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005850 }
John McCallf1549f62010-07-06 01:34:17 +00005851
5852 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
5853 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
5854 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005855 }
John McCallf1549f62010-07-06 01:34:17 +00005856
5857 // Emit the try body.
5858 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005859
John McCallf1549f62010-07-06 01:34:17 +00005860 // Leave the try.
5861 if (S.getNumCatchStmts())
5862 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005863
John McCallf1549f62010-07-06 01:34:17 +00005864 // Remember where we were.
5865 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005866
John McCallf1549f62010-07-06 01:34:17 +00005867 // Emit the handlers.
5868 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
5869 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005870
John McCallf1549f62010-07-06 01:34:17 +00005871 CGF.EmitBlock(Handler.Block);
5872 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005873
John McCallf1549f62010-07-06 01:34:17 +00005874 // Enter the catch.
5875 llvm::CallInst *Exn =
5876 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
5877 "exn.adjusted");
5878 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005879
John McCallf1549f62010-07-06 01:34:17 +00005880 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00005881 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00005882 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
5883 EndCatchMightThrow,
5884 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005885
John McCallf1549f62010-07-06 01:34:17 +00005886 // Bind the catch parameter if it exists.
5887 if (const VarDecl *CatchParam = Handler.Variable) {
5888 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
5889 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005890
John McCallf1549f62010-07-06 01:34:17 +00005891 CGF.EmitLocalBlockVarDecl(*CatchParam);
5892 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005893 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005894
John McCallf1549f62010-07-06 01:34:17 +00005895 CGF.ObjCEHValueStack.push_back(Exn);
5896 CGF.EmitStmt(Handler.Body);
5897 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005898
John McCallf1549f62010-07-06 01:34:17 +00005899 // Leave the earlier cleanup.
5900 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005901
John McCallf1549f62010-07-06 01:34:17 +00005902 CGF.EmitBranchThroughCleanup(Cont);
5903 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005904
John McCallf1549f62010-07-06 01:34:17 +00005905 // Go back to the try-statement fallthrough.
5906 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005907
John McCallf1549f62010-07-06 01:34:17 +00005908 // Pop out of the normal cleanup on the finally.
5909 if (S.getFinallyStmt())
5910 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005911
John McCallff8e1152010-07-23 21:56:41 +00005912 if (Cont.isValid())
5913 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005914}
5915
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005916/// EmitThrowStmt - Generate code for a throw statement.
5917void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5918 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005919 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005920 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005921 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005922 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005923 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005924 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005925 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005926 "Unexpected rethrow outside @catch block.");
5927 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005928 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005929 }
5930
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005931 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005932 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5933 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5934 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005935 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00005936 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005937 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00005938 } else {
5939 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
5940 ->setDoesNotReturn();
5941 CGF.Builder.CreateUnreachable();
5942 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005943
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005944 // Clear the insertion point to indicate we are in unreachable code.
5945 CGF.Builder.ClearInsertionPoint();
5946}
Daniel Dunbare588b992009-03-01 04:46:24 +00005947
John McCall5a180392010-07-24 00:37:23 +00005948llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005950 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005951 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005952
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005953 // If we don't need a definition, return the entry if found or check
5954 // if we use an external reference.
5955 if (!ForDefinition) {
5956 if (Entry)
5957 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005958
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005959 // If this type (or a super class) has the __objc_exception__
5960 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005961 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005962 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005963 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005964 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005965 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005966 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005967 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005968 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005969
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005970 // Otherwise we need to either make a new entry or fill in the
5971 // initializer.
5972 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005973 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005974 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005975 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00005976 CGM.getModule().getGlobalVariable(VTableName);
5977 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005978 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5979 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005980 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005981 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005982
Chris Lattner77b89b82010-06-27 07:15:29 +00005983 llvm::Value *VTableIdx =
5984 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005985
5986 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005987 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005988 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005989 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005990 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005991 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005992
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005993 if (Entry) {
5994 Entry->setInitializer(Init);
5995 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005996 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005997 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005998 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005999 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006000 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006001 }
6002
Daniel Dunbar04d40782009-04-14 06:00:08 +00006003 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006004 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006005 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6006 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006007
6008 if (ForDefinition) {
6009 Entry->setSection("__DATA,__objc_const");
6010 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6011 } else {
6012 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6013 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006014
6015 return Entry;
6016}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006017
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006018/* *** */
6019
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006020CodeGen::CGObjCRuntime *
6021CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006022 return new CGObjCMac(CGM);
6023}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006024
6025CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006026CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006027 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006028}