blob: 73074b6bbe5285085e8db03c601bd203450ca6de [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallf1549f62010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000027
John McCall87bb5822010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000043
Daniel Dunbar97776872009-04-22 07:32:20 +000044// Common CGObjCRuntime functions, these don't belong here, but they
45// don't belong in CGObjCRuntime either so we will live with it for
46// now.
47
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000053
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000054 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
55 // in here; it should never be necessary because that should be the lexical
56 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000057
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 // If we know have an implementation (and the ivar is in it) then
59 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 if (ID && ID->getClassInterface() == Container)
62 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
63 else
64 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000065
66 // Compute field index.
67 //
68 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
69 // implemented. This should be fixed to get the information from the layout
70 // directly.
71 unsigned Index = 0;
72 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
73 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
74 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
75 if (Ivar == Ivars[k])
76 break;
77 ++Index;
78 }
79 assert(Index != Ivars.size() && "Ivar is not inside container!");
80
Daniel Dunbar532d4da2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000109
Daniel Dunbar6d5eb762010-08-21 03:44:13 +0000110 if (!Ivar->isBitField()) {
111 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
112 LV.getQuals().addCVRQualifiers(CVRQualifiers);
113 return LV;
114 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000115
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000116 // We need to compute an access strategy for this bit-field. We are given the
117 // offset to the first byte in the bit-field, the sub-byte offset is taken
118 // from the original layout. We reuse the normal bit-field access strategy by
119 // treating this as an access to a struct where the bit-field is in byte 0,
120 // and adjust the containing type size as appropriate.
121 //
122 // FIXME: Note that currently we make a very conservative estimate of the
123 // alignment of the bit-field, because (a) it is not clear what guarantees the
124 // runtime makes us, and (b) we don't have a way to specify that the struct is
125 // at an alignment plus offset.
126 //
127 // Note, there is a subtle invariant here: we can only call this routine on
128 // non-synthesized ivars but we may be called for synthesized ivars. However,
129 // a synthesized ivar can never be a bit-field, so this is safe.
130 const ASTRecordLayout &RL =
131 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
132 uint64_t TypeSizeInBits = RL.getSize();
133 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
134 uint64_t BitOffset = FieldBitOffset % 8;
135 uint64_t ContainingTypeAlign = 8;
136 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbar56229f52010-04-05 16:20:33 +0000137 uint64_t BitFieldSize =
138 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000139
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000140 // Allocate a new CGBitFieldInfo object to describe this access.
141 //
142 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
143 // layout object. However, this is blocked on other cleanups to the
144 // Objective-C code, so for now we just live with allocating a bunch of these
145 // objects.
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000146 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
147 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
148 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000149
Daniel Dunbar76695ca2010-08-21 04:05:54 +0000150 return LValue::MakeBitfield(V, *Info,
151 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000152}
153
154///
155
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000156namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000157
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000158typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000159
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160// FIXME: We should find a nicer way to make the labels for metadata, string
161// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000162
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000163class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000164protected:
165 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000166
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000167private:
168 llvm::Constant *getMessageSendFn() const {
169 // id objc_msgSend (id, SEL, ...)
170 std::vector<const llvm::Type*> Params;
171 Params.push_back(ObjectPtrTy);
172 Params.push_back(SelectorPtrTy);
173 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
175 Params, true),
176 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000177 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000179 llvm::Constant *getMessageSendStretFn() const {
180 // id objc_msgSend_stret (id, SEL, ...)
181 std::vector<const llvm::Type*> Params;
182 Params.push_back(ObjectPtrTy);
183 Params.push_back(SelectorPtrTy);
184 return
Owen Anderson0032b272009-08-13 21:57:51 +0000185 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000186 Params, true),
187 "objc_msgSend_stret");
188
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000189 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000190
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000191 llvm::Constant *getMessageSendFpretFn() const {
192 // FIXME: This should be long double on x86_64?
193 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
194 std::vector<const llvm::Type*> Params;
195 Params.push_back(ObjectPtrTy);
196 Params.push_back(SelectorPtrTy);
197 return
Owen Anderson0032b272009-08-13 21:57:51 +0000198 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
199 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000200 Params,
201 true),
202 "objc_msgSend_fpret");
203
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000204 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000205
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000206 llvm::Constant *getMessageSendSuperFn() const {
207 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
208 const char *SuperName = "objc_msgSendSuper";
209 std::vector<const llvm::Type*> Params;
210 Params.push_back(SuperPtrTy);
211 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000212 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000213 Params, true),
214 SuperName);
215 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000216
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000217 llvm::Constant *getMessageSendSuperFn2() const {
218 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
219 const char *SuperName = "objc_msgSendSuper2";
220 std::vector<const llvm::Type*> Params;
221 Params.push_back(SuperPtrTy);
222 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000223 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000224 Params, true),
225 SuperName);
226 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000228 llvm::Constant *getMessageSendSuperStretFn() const {
229 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
230 // SEL op, ...)
231 std::vector<const llvm::Type*> Params;
232 Params.push_back(Int8PtrTy);
233 Params.push_back(SuperPtrTy);
234 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000235 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000236 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000237 Params, true),
238 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000239 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000240
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000241 llvm::Constant *getMessageSendSuperStretFn2() const {
242 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
243 // SEL op, ...)
244 std::vector<const llvm::Type*> Params;
245 Params.push_back(Int8PtrTy);
246 Params.push_back(SuperPtrTy);
247 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000248 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000249 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250 Params, true),
251 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000252 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000253
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000254 llvm::Constant *getMessageSendSuperFpretFn() const {
255 // There is no objc_msgSendSuper_fpret? How can that work?
256 return getMessageSendSuperFn();
257 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000258
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000259 llvm::Constant *getMessageSendSuperFpretFn2() const {
260 // There is no objc_msgSendSuper_fpret? How can that work?
261 return getMessageSendSuperFn2();
262 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000263
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000264protected:
265 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000267public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000268 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000269 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000271 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
272 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000273
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000274 /// PtrObjectPtrTy - LLVM type for id *
275 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000276
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000277 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000278 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 /// ProtocolPtrTy - LLVM type for external protocol handles
280 /// (typeof(Protocol))
281 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000282
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000283 // SuperCTy - clang type for struct objc_super.
284 QualType SuperCTy;
285 // SuperPtrCTy - clang type for struct objc_super *.
286 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000287
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000288 /// SuperTy - LLVM type for struct objc_super.
289 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000290 /// SuperPtrTy - LLVM type for struct objc_super *.
291 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000292
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000293 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
294 /// in GCC parlance).
295 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000296
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000297 /// PropertyListTy - LLVM type for struct objc_property_list
298 /// (_prop_list_t in GCC parlance).
299 const llvm::StructType *PropertyListTy;
300 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
301 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000302
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000303 // MethodTy - LLVM type for struct objc_method.
304 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000305
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000306 /// CacheTy - LLVM type for struct objc_cache.
307 const llvm::Type *CacheTy;
308 /// CachePtrTy - LLVM type for struct objc_cache *.
309 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000310
Chris Lattner72db6c32009-04-22 02:44:54 +0000311 llvm::Constant *getGetPropertyFn() {
312 CodeGen::CodeGenTypes &Types = CGM.getTypes();
313 ASTContext &Ctx = CGM.getContext();
314 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000315 llvm::SmallVector<CanQualType,4> Params;
316 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
317 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000318 Params.push_back(IdType);
319 Params.push_back(SelType);
320 Params.push_back(Ctx.LongTy);
321 Params.push_back(Ctx.BoolTy);
322 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000323 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000324 FunctionType::ExtInfo()),
325 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
327 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000328
Chris Lattner72db6c32009-04-22 02:44:54 +0000329 llvm::Constant *getSetPropertyFn() {
330 CodeGen::CodeGenTypes &Types = CGM.getTypes();
331 ASTContext &Ctx = CGM.getContext();
332 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000333 llvm::SmallVector<CanQualType,6> Params;
334 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
335 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000336 Params.push_back(IdType);
337 Params.push_back(SelType);
338 Params.push_back(Ctx.LongTy);
339 Params.push_back(IdType);
340 Params.push_back(Ctx.BoolTy);
341 Params.push_back(Ctx.BoolTy);
342 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000343 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000344 FunctionType::ExtInfo()),
345 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000346 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
347 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000348
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000349
350 llvm::Constant *getCopyStructFn() {
351 CodeGen::CodeGenTypes &Types = CGM.getTypes();
352 ASTContext &Ctx = CGM.getContext();
353 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
354 llvm::SmallVector<CanQualType,5> Params;
355 Params.push_back(Ctx.VoidPtrTy);
356 Params.push_back(Ctx.VoidPtrTy);
357 Params.push_back(Ctx.LongTy);
358 Params.push_back(Ctx.BoolTy);
359 Params.push_back(Ctx.BoolTy);
360 const llvm::FunctionType *FTy =
361 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
362 FunctionType::ExtInfo()),
363 false);
364 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
365 }
366
Chris Lattner72db6c32009-04-22 02:44:54 +0000367 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000368 CodeGen::CodeGenTypes &Types = CGM.getTypes();
369 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000370 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000371 llvm::SmallVector<CanQualType,1> Params;
372 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000373 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000374 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000375 FunctionType::ExtInfo()),
376 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000379
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000380 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000381 llvm::Constant *getGcReadWeakFn() {
382 // id objc_read_weak (id *)
383 std::vector<const llvm::Type*> Args;
384 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000385 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000386 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000387 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000388 }
389
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000390 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000391 llvm::Constant *getGcAssignWeakFn() {
392 // id objc_assign_weak (id, id *)
393 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
394 Args.push_back(ObjectPtrTy->getPointerTo());
395 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000396 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000397 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
398 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000399
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000400 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000401 llvm::Constant *getGcAssignGlobalFn() {
402 // id objc_assign_global(id, id *)
403 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
404 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000405 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000406 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000410 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
411 llvm::Constant *getGcAssignThreadLocalFn() {
412 // id objc_assign_threadlocal(id src, id * dest)
413 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
414 Args.push_back(ObjectPtrTy->getPointerTo());
415 llvm::FunctionType *FTy =
416 llvm::FunctionType::get(ObjectPtrTy, Args, false);
417 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
418 }
419
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000420 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000421 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000422 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
424 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000425 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000426 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000427 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000428 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
429 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000430
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000431 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
432 llvm::Constant *GcMemmoveCollectableFn() {
433 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
434 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
435 Args.push_back(Int8PtrTy);
436 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000437 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000438 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
439 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000440
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000441 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000442 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000443 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
445 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000446 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000447 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
449 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000450
451 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000452 llvm::Constant *getExceptionThrowFn() {
453 // void objc_exception_throw(id)
454 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
455 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000456 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000457 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
458 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000459
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000460 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
461 llvm::Constant *getExceptionRethrowFn() {
462 // void objc_exception_rethrow(void)
463 std::vector<const llvm::Type*> Args;
464 llvm::FunctionType *FTy =
465 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
466 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
467 }
468
Daniel Dunbar1c566672009-02-24 01:43:46 +0000469 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000470 llvm::Constant *getSyncEnterFn() {
471 // void objc_sync_enter (id)
472 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
473 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000474 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000475 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
476 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000477
Daniel Dunbar1c566672009-02-24 01:43:46 +0000478 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000479 llvm::Constant *getSyncExitFn() {
480 // void objc_sync_exit (id)
481 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
482 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000483 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000484 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000486
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
489 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000490
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendFn2(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
493 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000494
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
497 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000498
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendStretFn2(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
501 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
505 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000506
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000507 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
508 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
509 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000510
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000511 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
512 ~ObjCCommonTypesHelper(){}
513};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000514
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000515/// ObjCTypesHelper - Helper class that encapsulates lazy
516/// construction of varies types used during ObjC generation.
517class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000518public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000519 /// SymtabTy - LLVM type for struct objc_symtab.
520 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000521 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
522 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000523 /// ModuleTy - LLVM type for struct objc_module.
524 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000525
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000526 /// ProtocolTy - LLVM type for struct objc_protocol.
527 const llvm::StructType *ProtocolTy;
528 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
529 const llvm::Type *ProtocolPtrTy;
530 /// ProtocolExtensionTy - LLVM type for struct
531 /// objc_protocol_extension.
532 const llvm::StructType *ProtocolExtensionTy;
533 /// ProtocolExtensionTy - LLVM type for struct
534 /// objc_protocol_extension *.
535 const llvm::Type *ProtocolExtensionPtrTy;
536 /// MethodDescriptionTy - LLVM type for struct
537 /// objc_method_description.
538 const llvm::StructType *MethodDescriptionTy;
539 /// MethodDescriptionListTy - LLVM type for struct
540 /// objc_method_description_list.
541 const llvm::StructType *MethodDescriptionListTy;
542 /// MethodDescriptionListPtrTy - LLVM type for struct
543 /// objc_method_description_list *.
544 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000545 /// ProtocolListTy - LLVM type for struct objc_property_list.
546 const llvm::Type *ProtocolListTy;
547 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
548 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000549 /// CategoryTy - LLVM type for struct objc_category.
550 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000551 /// ClassTy - LLVM type for struct objc_class.
552 const llvm::StructType *ClassTy;
553 /// ClassPtrTy - LLVM type for struct objc_class *.
554 const llvm::Type *ClassPtrTy;
555 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
556 const llvm::StructType *ClassExtensionTy;
557 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
558 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000559 // IvarTy - LLVM type for struct objc_ivar.
560 const llvm::StructType *IvarTy;
561 /// IvarListTy - LLVM type for struct objc_ivar_list.
562 const llvm::Type *IvarListTy;
563 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
564 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000565 /// MethodListTy - LLVM type for struct objc_method_list.
566 const llvm::Type *MethodListTy;
567 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
568 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000569
Anders Carlsson124526b2008-09-09 10:10:21 +0000570 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
571 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572
Anders Carlsson124526b2008-09-09 10:10:21 +0000573 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000574 llvm::Constant *getExceptionTryEnterFn() {
575 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000576 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000577 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000578 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579 Params, false),
580 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000582
583 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000584 llvm::Constant *getExceptionTryExitFn() {
585 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000586 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000587 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000588 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000589 Params, false),
590 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000591 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000592
593 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000594 llvm::Constant *getExceptionExtractFn() {
595 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000596 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
597 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000598 Params, false),
599 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Chris Lattner34b02a12009-04-22 02:26:14 +0000601 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Anders Carlsson124526b2008-09-09 10:10:21 +0000603 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000604 llvm::Constant *getExceptionMatchFn() {
605 std::vector<const llvm::Type*> Params;
606 Params.push_back(ClassPtrTy);
607 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000608 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000609 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610 Params, false),
611 "objc_exception_match");
612
Chris Lattner34b02a12009-04-22 02:26:14 +0000613 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000614
Anders Carlsson124526b2008-09-09 10:10:21 +0000615 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000616 llvm::Constant *getSetJmpFn() {
617 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000618 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000619 return
Owen Anderson0032b272009-08-13 21:57:51 +0000620 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000621 Params, false),
622 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000623
Chris Lattner34b02a12009-04-22 02:26:14 +0000624 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000626public:
627 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000628 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000629};
630
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000631/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000632/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000633class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000634public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000635
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000636 // MethodListnfABITy - LLVM for struct _method_list_t
637 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000639 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
640 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000641
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000642 // ProtocolnfABITy = LLVM for struct _protocol_t
643 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Daniel Dunbar948e2582009-02-15 07:36:20 +0000645 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
646 const llvm::Type *ProtocolnfABIPtrTy;
647
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000648 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
649 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000650
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000651 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
652 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000654 // ClassnfABITy - LLVM for struct _class_t
655 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000657 // ClassnfABIPtrTy - LLVM for struct _class_t*
658 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000659
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000660 // IvarnfABITy - LLVM for struct _ivar_t
661 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000663 // IvarListnfABITy - LLVM for struct _ivar_list_t
664 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000666 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
667 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000669 // ClassRonfABITy - LLVM for struct _class_ro_t
670 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000671
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000672 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
673 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000675 // CategorynfABITy - LLVM for struct _category_t
676 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000677
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000678 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000680 // MessageRefTy - LLVM for:
681 // struct _message_ref_t {
682 // IMP messenger;
683 // SEL name;
684 // };
685 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000686 // MessageRefCTy - clang type for struct _message_ref_t
687 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000688
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000689 // MessageRefPtrTy - LLVM for struct _message_ref_t*
690 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000691 // MessageRefCPtrTy - clang type for struct _message_ref_t*
692 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000693
Fariborz Jahanianef163782009-02-05 01:13:09 +0000694 // MessengerTy - Type of the messenger (shown as IMP above)
695 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000696
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000697 // SuperMessageRefTy - LLVM for:
698 // struct _super_message_ref_t {
699 // SUPER_IMP messenger;
700 // SEL name;
701 // };
702 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000703
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000704 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
705 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000706
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 llvm::Constant *getMessageSendFixupFn() {
708 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
709 std::vector<const llvm::Type*> Params;
710 Params.push_back(ObjectPtrTy);
711 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000712 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000713 Params, true),
714 "objc_msgSend_fixup");
715 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000716
Chris Lattner1c02f862009-04-22 02:53:24 +0000717 llvm::Constant *getMessageSendFpretFixupFn() {
718 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
719 std::vector<const llvm::Type*> Params;
720 Params.push_back(ObjectPtrTy);
721 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000723 Params, true),
724 "objc_msgSend_fpret_fixup");
725 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000726
Chris Lattner1c02f862009-04-22 02:53:24 +0000727 llvm::Constant *getMessageSendStretFixupFn() {
728 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
729 std::vector<const llvm::Type*> Params;
730 Params.push_back(ObjectPtrTy);
731 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000732 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000733 Params, true),
734 "objc_msgSend_stret_fixup");
735 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000736
Chris Lattner1c02f862009-04-22 02:53:24 +0000737 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000738 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000739 // struct _super_message_ref_t*, ...)
740 std::vector<const llvm::Type*> Params;
741 Params.push_back(SuperPtrTy);
742 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000743 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000744 Params, true),
745 "objc_msgSendSuper2_fixup");
746 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000747
Chris Lattner1c02f862009-04-22 02:53:24 +0000748 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000750 // struct _super_message_ref_t*, ...)
751 std::vector<const llvm::Type*> Params;
752 Params.push_back(SuperPtrTy);
753 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000754 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000755 Params, true),
756 "objc_msgSendSuper2_stret_fixup");
757 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000758
Chris Lattner8a569112009-04-22 02:15:23 +0000759 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000760 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000761 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000762 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000763
Chris Lattner8a569112009-04-22 02:15:23 +0000764 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000765
Chris Lattner8a569112009-04-22 02:15:23 +0000766 llvm::Constant *getObjCBeginCatchFn() {
767 std::vector<const llvm::Type*> Params;
768 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000770 Params, false),
771 "objc_begin_catch");
772 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000773
774 const llvm::StructType *EHTypeTy;
775 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000776
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000777 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
778 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000779};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000780
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000782public:
783 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000784 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000785 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000786 unsigned ivar_bytepos;
787 unsigned ivar_size;
788 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000789 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000790
791 // Allow sorting based on byte pos.
792 bool operator<(const GC_IVAR &b) const {
793 return ivar_bytepos < b.ivar_bytepos;
794 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000795 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000796
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000797 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000798 public:
799 unsigned skip;
800 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000802 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000803 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000804
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000805protected:
806 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000807 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000808 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000809 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000810
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000811 // gc ivar layout bitmap calculation helper caches.
812 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
813 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000814
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000815 /// LazySymbols - Symbols to generate a lazy reference for. See
816 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000817 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000818
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000819 /// DefinedSymbols - External symbols which are defined by this
820 /// module. The symbols in this list and LazySymbols are used to add
821 /// special linker symbols which ensure that Objective-C modules are
822 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000823 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000824
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000825 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000826 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000827
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000828 /// MethodVarNames - uniqued method variable names.
829 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000830
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000831 /// DefinedCategoryNames - list of category names in form Class_Category.
832 llvm::SetVector<std::string> DefinedCategoryNames;
833
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000834 /// MethodVarTypes - uniqued method type signatures. We have to use
835 /// a StringMap here because have no other unique reference.
836 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000837
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000838 /// MethodDefinitions - map of methods which have been defined in
839 /// this translation unit.
840 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000842 /// PropertyNames - uniqued method variable names.
843 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000845 /// ClassReferences - uniqued class references.
846 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000848 /// SelectorReferences - uniqued selector references.
849 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000851 /// Protocols - Protocols for which an objc_protocol structure has
852 /// been emitted. Forward declarations are handled by creating an
853 /// empty structure whose initializer is filled in when/if defined.
854 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000855
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000856 /// DefinedProtocols - Protocols which have actually been
857 /// defined. We should not need this, see FIXME in GenerateProtocol.
858 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000859
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000860 /// DefinedClasses - List of defined classes.
861 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000862
863 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
864 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000865
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000866 /// DefinedCategories - List of defined categories.
867 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000869 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
870 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000871
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000872 /// GetNameForMethod - Return a name for the given method.
873 /// \param[out] NameOut - The return value.
874 void GetNameForMethod(const ObjCMethodDecl *OMD,
875 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000876 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000878 /// GetMethodVarName - Return a unique constant for the given
879 /// selector's name. The return value has type char *.
880 llvm::Constant *GetMethodVarName(Selector Sel);
881 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000882
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000883 /// GetMethodVarType - Return a unique constant for the given
884 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000885
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000886 // FIXME: This is a horrible name.
887 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000888 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000889
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000890 /// GetPropertyName - Return a unique constant for the given
891 /// name. The return value has type char *.
892 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000894 // FIXME: This can be dropped once string functions are unified.
895 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
896 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000897
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000898 /// GetClassName - Return a unique constant for the given selector's
899 /// name. The return value has type char *.
900 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000901
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000902 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
903
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000904 /// BuildIvarLayout - Builds ivar layout bitmap for the class
905 /// implementation for the __strong or __weak case.
906 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000907 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
908 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000909
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000910 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000911
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000912 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913 unsigned int BytePos, bool ForStrongLayout,
914 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000915 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000916 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000917 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000918 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000919 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000920 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000921
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000922 /// GetIvarLayoutName - Returns a unique constant for the given
923 /// ivar layout bitmap.
924 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
925 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000926
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000927 /// EmitPropertyList - Emit the given property list. The return
928 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000929 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000930 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000931 const ObjCContainerDecl *OCD,
932 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000933
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000934 /// PushProtocolProperties - Push protocol's property on the input stack.
935 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
936 std::vector<llvm::Constant*> &Properties,
937 const Decl *Container,
938 const ObjCProtocolDecl *PROTO,
939 const ObjCCommonTypesHelper &ObjCTypes);
940
Fariborz Jahanianda320092009-01-29 19:24:30 +0000941 /// GetProtocolRef - Return a reference to the internal protocol
942 /// description, creating an empty one if it has not been
943 /// defined. The return value has type ProtocolPtrTy.
944 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000945
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000946 /// CreateMetadataVar - Create a global variable with internal
947 /// linkage for use by the Objective-C runtime.
948 ///
949 /// This is a convenience wrapper which not only creates the
950 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000951 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000952 ///
953 /// \param Name - The variable name.
954 /// \param Init - The variable initializer; this is also used to
955 /// define the type of the variable.
956 /// \param Section - The section the variable should go into, or 0.
957 /// \param Align - The alignment for the variable, or 0.
958 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000959 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000960 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000961 llvm::Constant *Init,
962 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000963 unsigned Align,
964 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000965
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000966 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000967 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000968 QualType ResultType,
969 llvm::Value *Sel,
970 llvm::Value *Arg0,
971 QualType Arg0Ty,
972 bool IsSuper,
973 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000974 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000975 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000976
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000977 /// EmitImageInfo - Emit the image info marker used to encode some module
978 /// level information.
979 void EmitImageInfo();
980
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000981public:
Owen Anderson69243822009-07-13 04:10:07 +0000982 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000983 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000984
David Chisnall0d13f6f2010-01-23 02:40:42 +0000985 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000986
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000987 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
988 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000989
Fariborz Jahanianda320092009-01-29 19:24:30 +0000990 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000991
Fariborz Jahanianda320092009-01-29 19:24:30 +0000992 /// GetOrEmitProtocol - Get the protocol object for the given
993 /// declaration, emitting it if necessary. The return value has type
994 /// ProtocolPtrTy.
995 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000996
Fariborz Jahanianda320092009-01-29 19:24:30 +0000997 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
998 /// object for the given declaration, emitting it if needed. These
999 /// forward references will be filled in with empty bodies if no
1000 /// definition is seen. The return value has type ProtocolPtrTy.
1001 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001002 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001003 const llvm::SmallVectorImpl<const Expr *> &);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001004
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001005};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001006
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001007class CGObjCMac : public CGObjCCommonMac {
1008private:
1009 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001010
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001011 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001012 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001013 void EmitModuleInfo();
1014
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001015 /// EmitModuleSymols - Emit module symbols, the list of defined
1016 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001017 llvm::Constant *EmitModuleSymbols();
1018
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001019 /// FinishModule - Write out global data structures at the end of
1020 /// processing a translation unit.
1021 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001023 /// EmitClassExtension - Generate the class extension structure used
1024 /// to store the weak ivar layout and properties. The return value
1025 /// has type ClassExtensionPtrTy.
1026 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1027
1028 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1029 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001030 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001031 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001032
1033 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1034 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001035
1036 /// EmitIvarList - Emit the ivar list for the given
1037 /// implementation. If ForClass is true the list of class ivars
1038 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1039 /// interface ivars will be emitted. The return value has type
1040 /// IvarListPtrTy.
1041 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001042 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001043
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001044 /// EmitMetaClass - Emit a forward reference to the class structure
1045 /// for the metaclass of the given interface. The return value has
1046 /// type ClassPtrTy.
1047 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1048
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001049 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001050 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001051 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1052 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001053 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001054
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001055 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001056
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001057 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001058
1059 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001060 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001061 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001062 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001063 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001064
1065 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001066 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001067 /// - TypeName: The name for the type containing the methods.
1068 /// - IsProtocol: True iff these methods are for a protocol.
1069 /// - ClassMethds: True iff these are class methods.
1070 /// - Required: When true, only "required" methods are
1071 /// listed. Similarly, when false only "optional" methods are
1072 /// listed. For classes this should always be true.
1073 /// - begin, end: The method list to output.
1074 ///
1075 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001076 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001077 const char *Section,
1078 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001079
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001080 /// GetOrEmitProtocol - Get the protocol object for the given
1081 /// declaration, emitting it if necessary. The return value has type
1082 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001083 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001084
1085 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1086 /// object for the given declaration, emitting it if needed. These
1087 /// forward references will be filled in with empty bodies if no
1088 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001089 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001090
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001091 /// EmitProtocolExtension - Generate the protocol extension
1092 /// structure used to store optional instance and class methods, and
1093 /// protocol properties. The return value has type
1094 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001095 llvm::Constant *
1096 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1097 const ConstantVector &OptInstanceMethods,
1098 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001099
1100 /// EmitProtocolList - Generate the list of referenced
1101 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001102 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001103 ObjCProtocolDecl::protocol_iterator begin,
1104 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001105
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001106 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1107 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001108 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1109 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001110
1111public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001112 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001113
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001114 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001115
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001116 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001117 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001118 QualType ResultType,
1119 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001120 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001121 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001122 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001123 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001124
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001125 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001126 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001127 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001128 QualType ResultType,
1129 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001130 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001131 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001132 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001133 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001134 const CallArgList &CallArgs,
1135 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001136
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001137 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001138 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001139
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001140 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1141 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001142
1143 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1144 /// untyped one.
1145 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1146 const ObjCMethodDecl *Method);
1147
John McCall5a180392010-07-24 00:37:23 +00001148 virtual llvm::Constant *GetEHType(QualType T);
1149
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001150 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001151
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001152 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001153
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001154 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001155 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001156
Chris Lattner74391b42009-03-22 21:03:39 +00001157 virtual llvm::Constant *GetPropertyGetFunction();
1158 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001159 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001160 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001161
John McCallf1549f62010-07-06 01:34:17 +00001162 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1163 const ObjCAtTryStmt &S);
1164 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1165 const ObjCAtSynchronizedStmt &S);
1166 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001167 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1168 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001169 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001170 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001171 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001172 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001173 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001174 llvm::Value *src, llvm::Value *dest,
1175 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001176 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001177 llvm::Value *src, llvm::Value *dest,
1178 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001179 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1180 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001181 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1182 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001183 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001184
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001185 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1186 QualType ObjectTy,
1187 llvm::Value *BaseValue,
1188 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001189 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001190 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001191 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001192 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001193};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001194
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001195class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001196private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001197 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001198 llvm::GlobalVariable* ObjCEmptyCacheVar;
1199 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
Daniel Dunbar11394522009-04-18 08:51:00 +00001201 /// SuperClassReferences - uniqued super class references.
1202 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001203
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001204 /// MetaClassReferences - uniqued meta class references.
1205 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001206
1207 /// EHTypeReferences - uniqued class ehtype references.
1208 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001209
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001210 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001211 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001212 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001213
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001214 /// DefinedMetaClasses - List of defined meta-classes.
1215 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1216
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001217 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001218 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001219 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001220
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001221 /// FinishNonFragileABIModule - Write out global data structures at the end of
1222 /// processing a translation unit.
1223 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001224
Daniel Dunbar463b8762009-05-15 21:48:48 +00001225 /// AddModuleClassList - Add the given list of class pointers to the
1226 /// module with the provided symbol and section names.
1227 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1228 const char *SymbolName,
1229 const char *SectionName);
1230
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001231 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1232 unsigned InstanceStart,
1233 unsigned InstanceSize,
1234 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001235 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001236 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001237 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001238 llvm::Constant *ClassRoGV,
1239 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001240
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001241 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001243 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001245 /// EmitMethodList - Emit the method list for the given
1246 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001247 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001248 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001249 const ConstantVector &Methods);
1250 /// EmitIvarList - Emit the ivar list for the given
1251 /// implementation. If ForClass is true the list of class ivars
1252 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1253 /// interface ivars will be emitted. The return value has type
1254 /// IvarListnfABIPtrTy.
1255 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001257 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001258 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001259 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001260
Fariborz Jahanianda320092009-01-29 19:24:30 +00001261 /// GetOrEmitProtocol - Get the protocol object for the given
1262 /// declaration, emitting it if necessary. The return value has type
1263 /// ProtocolPtrTy.
1264 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001265
Fariborz Jahanianda320092009-01-29 19:24:30 +00001266 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1267 /// object for the given declaration, emitting it if needed. These
1268 /// forward references will be filled in with empty bodies if no
1269 /// definition is seen. The return value has type ProtocolPtrTy.
1270 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001271
Fariborz Jahanianda320092009-01-29 19:24:30 +00001272 /// EmitProtocolList - Generate the list of referenced
1273 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001274 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001275 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001276 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
Fariborz Jahanian46551122009-02-04 00:22:57 +00001278 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001279 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001280 QualType ResultType,
1281 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001282 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001283 QualType Arg0Ty,
1284 bool IsSuper,
1285 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001286
1287 /// GetClassGlobal - Return the global variable for the Objective-C
1288 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001289 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001290
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001291 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001292 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001293 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001294 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001295
Daniel Dunbar11394522009-04-18 08:51:00 +00001296 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1297 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001298 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1299 const ObjCInterfaceDecl *ID);
1300
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001301 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1302 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001303 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001304 const ObjCInterfaceDecl *ID);
1305
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001306 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1307 /// the given ivar.
1308 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001309 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310 const ObjCInterfaceDecl *ID,
1311 const ObjCIvarDecl *Ivar);
1312
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001313 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1314 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001315 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1316 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001317
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001318 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001319 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001320 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001321 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001322
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001323 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001324 return "OBJC_METACLASS_$_";
1325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001326
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001327 const char *getClassSymbolPrefix() const {
1328 return "OBJC_CLASS_$_";
1329 }
1330
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001331 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001332 uint32_t &InstanceStart,
1333 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001334
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001335 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001336 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001337 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1338 return CGM.getContext().Selectors.getSelector(0, &II);
1339 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001340
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001341 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001342 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1343 return CGM.getContext().Selectors.getSelector(1, &II);
1344 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001345
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001346 /// ImplementationIsNonLazy - Check whether the given category or
1347 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001348 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001349
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001350public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001351 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001352 // FIXME. All stubs for now!
1353 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001354
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001356 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 QualType ResultType,
1358 Selector Sel,
1359 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001360 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001361 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001362 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001363
1364 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001365 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001366 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001367 QualType ResultType,
1368 Selector Sel,
1369 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001370 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001371 llvm::Value *Receiver,
1372 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001373 const CallArgList &CallArgs,
1374 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001375
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001376 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001377 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001379 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1380 bool lvalue = false)
1381 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001382
1383 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1384 /// untyped one.
1385 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1386 const ObjCMethodDecl *Method)
1387 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001388
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001389 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001390
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001391 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001392 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001393 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001394
John McCall5a180392010-07-24 00:37:23 +00001395 virtual llvm::Constant *GetEHType(QualType T);
1396
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001398 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001400 virtual llvm::Constant *GetPropertySetFunction() {
1401 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001402 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001403
1404 virtual llvm::Constant *GetCopyStructFunction() {
1405 return ObjCTypes.getCopyStructFn();
1406 }
1407
Chris Lattner74391b42009-03-22 21:03:39 +00001408 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001409 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001411
John McCallf1549f62010-07-06 01:34:17 +00001412 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1413 const ObjCAtTryStmt &S);
1414 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1415 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001416 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001417 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001419 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001420 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001421 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001423 llvm::Value *src, llvm::Value *dest,
1424 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001426 llvm::Value *src, llvm::Value *dest,
1427 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001429 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001430 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1431 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001432 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001433 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1434 QualType ObjectTy,
1435 llvm::Value *BaseValue,
1436 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001437 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001438 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001439 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001440 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001441};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001442
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001443} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001444
1445/* *** Helper Functions *** */
1446
1447/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001448static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001450 unsigned idx0,
1451 unsigned idx1) {
1452 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001453 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1454 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001455 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001456 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001457}
1458
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001459/// hasObjCExceptionAttribute - Return true if this class or any super
1460/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001461static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001462 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001463 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001464 return true;
1465 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001466 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001467 return false;
1468}
1469
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001470/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001471
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001472CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001473 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001474 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001475 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001476}
1477
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001478/// GetClass - Return a reference to the class for the given interface
1479/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001480llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001481 const ObjCInterfaceDecl *ID) {
1482 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001483}
1484
1485/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001486llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1487 bool lval) {
1488 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001489}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001490llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001491 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001492 return EmitSelector(Builder, Method->getSelector());
1493}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494
John McCall5a180392010-07-24 00:37:23 +00001495llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1496 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1497 return 0;
1498}
1499
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001500/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001501/*
1502 struct __builtin_CFString {
1503 const int *isa; // point to __CFConstantStringClassReference
1504 int flags;
1505 const char *str;
1506 long length;
1507 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001508*/
1509
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001510/// or Generate a constant NSString object.
1511/*
1512 struct __builtin_NSString {
1513 const int *isa; // point to __NSConstantStringClassReference
1514 const char *str;
1515 unsigned int length;
1516 };
1517*/
1518
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001519llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001520 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001521 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1522 CGM.GetAddrOfConstantCFString(SL) :
1523 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001524}
1525
1526/// Generates a message send where the super is the receiver. This is
1527/// a message send to self with special delivery semantics indicating
1528/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001529CodeGen::RValue
1530CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001531 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001532 QualType ResultType,
1533 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001534 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001535 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001536 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001537 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001538 const CodeGen::CallArgList &CallArgs,
1539 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001540 // Create and init a super structure; this is a (receiver, class)
1541 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001542 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001543 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001544 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001545 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001546 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001547 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001548
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001549 // If this is a class message the metaclass is passed as the target.
1550 llvm::Value *Target;
1551 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001552 if (isCategoryImpl) {
1553 // Message sent to 'super' in a class method defined in a category
1554 // implementation requires an odd treatment.
1555 // If we are in a class method, we must retrieve the
1556 // _metaclass_ for the current class, pointed at by
1557 // the class's "isa" pointer. The following assumes that
1558 // isa" is the first ivar in a class (which it must be).
1559 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1560 Target = CGF.Builder.CreateStructGEP(Target, 0);
1561 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001562 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001563 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1564 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1565 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1566 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001567 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001568 }
1569 else if (isCategoryImpl)
1570 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1571 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001572 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1573 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1574 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001575 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001576 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1577 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001578 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001579 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001580 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001581 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001582 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001583 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001584 EmitSelector(CGF.Builder, Sel),
1585 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001586 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001587}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001588
1589/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001590CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001591 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001592 QualType ResultType,
1593 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001594 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001595 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001596 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001597 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001598 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001599 EmitSelector(CGF.Builder, Sel),
1600 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001601 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001602}
1603
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001604CodeGen::RValue
1605CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001606 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001607 QualType ResultType,
1608 llvm::Value *Sel,
1609 llvm::Value *Arg0,
1610 QualType Arg0Ty,
1611 bool IsSuper,
1612 const CallArgList &CallArgs,
1613 const ObjCMethodDecl *Method,
1614 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001615 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001616 if (!IsSuper)
1617 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001618 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001619 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001620 CGF.getContext().getObjCSelType()));
1621 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001622
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001623 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001624 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001625 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001626 const llvm::FunctionType *FTy =
1627 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001628
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001629 if (Method)
1630 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1631 CGM.getContext().getCanonicalType(ResultType) &&
1632 "Result type mismatch!");
1633
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001634 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001635 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001636 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001637 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001638 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1639 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1640 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001641 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001642 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001643 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001644 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001645 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001646 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001647}
1648
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001649static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1650 if (FQT.isObjCGCStrong())
1651 return Qualifiers::Strong;
1652
1653 if (FQT.isObjCGCWeak())
1654 return Qualifiers::Weak;
1655
1656 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1657 return Qualifiers::Strong;
1658
1659 if (const PointerType *PT = FQT->getAs<PointerType>())
1660 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1661
1662 return Qualifiers::GCNone;
1663}
1664
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001665llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001666 const llvm::SmallVectorImpl<const Expr *> &BlockLayout) {
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001667 llvm::Constant *NullPtr =
1668 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian5af12352010-08-05 15:52:12 +00001669 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001670 BlockLayout.empty())
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001671 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001672 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001673 SkipIvars.clear();
1674 IvarsInfo.clear();
1675 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1676 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1677
Fariborz Jahanian81979822010-09-09 00:21:45 +00001678 // __isa is the first field in block descriptor and must assume by runtime's
1679 // convention that it is GC'able.
1680 IvarsInfo.push_back(GC_IVAR(0, 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001681 for (size_t i = 0; i < BlockLayout.size(); ++i) {
1682 const Expr *E = BlockLayout[i];
1683 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
1684 if (!BDRE)
1685 continue;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001686 const ValueDecl *VD = BDRE->getDecl();
1687 CharUnits Offset = CGF.BlockDecls[VD];
1688 uint64_t FieldOffset = Offset.getQuantity();
1689 QualType Ty = VD->getType();
1690 assert(!Ty->isArrayType() &&
1691 "Array block variable should have been caught");
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001692 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001693 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1694 FieldOffset,
1695 true,
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001696 hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001697 continue;
1698 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001699
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001700 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1701 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1702 // __block variables are passed by their descriptior address. So, size
1703 // must reflect this.
1704 if (BDRE->isByRef())
1705 FieldSize = WordSizeInBits;
1706 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1707 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1708 FieldSize / WordSizeInBits));
1709 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1710 SkipIvars.push_back(GC_IVAR(FieldOffset,
1711 FieldSize / ByteSizeInBits));
1712 }
1713
1714 if (IvarsInfo.empty())
1715 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001716 // Sort on byte position in case we encounterred a union nested in
1717 // block variable type's aggregate type.
1718 if (hasUnion && !IvarsInfo.empty())
1719 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1720 if (hasUnion && !SkipIvars.empty())
1721 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001722
1723 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001724 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001725 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1726 printf("\n block variable layout for block: ");
1727 const unsigned char *s = (unsigned char*)BitMap.c_str();
1728 for (unsigned i = 0; i < BitMap.size(); i++)
1729 if (!(s[i] & 0xf0))
1730 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1731 else
1732 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1733 printf("\n");
1734 }
1735
1736 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001737}
1738
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001740 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001741 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001742 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001743 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1744
Owen Anderson3c4972d2009-07-29 18:54:39 +00001745 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001746 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001747}
1748
Fariborz Jahanianda320092009-01-29 19:24:30 +00001749void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001750 // FIXME: We shouldn't need this, the protocol decl should contain enough
1751 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001752 DefinedProtocols.insert(PD->getIdentifier());
1753
1754 // If we have generated a forward reference to this protocol, emit
1755 // it now. Otherwise do nothing, the protocol objects are lazily
1756 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001757 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001758 GetOrEmitProtocol(PD);
1759}
1760
Fariborz Jahanianda320092009-01-29 19:24:30 +00001761llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001762 if (DefinedProtocols.count(PD->getIdentifier()))
1763 return GetOrEmitProtocol(PD);
1764 return GetOrEmitProtocolRef(PD);
1765}
1766
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001767/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001768// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1769struct _objc_protocol {
1770struct _objc_protocol_extension *isa;
1771char *protocol_name;
1772struct _objc_protocol_list *protocol_list;
1773struct _objc__method_prototype_list *instance_methods;
1774struct _objc__method_prototype_list *class_methods
1775};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001777See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001778*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001779llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1780 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1781
1782 // Early exit if a defining object has already been generated.
1783 if (Entry && Entry->hasInitializer())
1784 return Entry;
1785
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001786 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001787 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001788 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1789
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001790 // Construct method lists.
1791 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1792 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001793 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001794 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001795 ObjCMethodDecl *MD = *i;
1796 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1797 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1798 OptInstanceMethods.push_back(C);
1799 } else {
1800 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001801 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001802 }
1803
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001804 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001805 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001806 ObjCMethodDecl *MD = *i;
1807 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1808 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1809 OptClassMethods.push_back(C);
1810 } else {
1811 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001812 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001813 }
1814
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001815 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001816 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001817 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001818 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001819 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001820 PD->protocol_begin(),
1821 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001822 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001823 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001824 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1825 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001826 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001827 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001828 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1829 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001830 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001831 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001832
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001833 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001834 // Already created, fix the linkage and update the initializer.
1835 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001836 Entry->setInitializer(Init);
1837 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001839 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001840 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001841 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001842 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001843 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001844 // FIXME: Is this necessary? Why only for protocol?
1845 Entry->setAlignment(4);
1846 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001847 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001848
1849 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001850}
1851
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001852llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1854
1855 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001856 // We use the initializer as a marker of whether this is a forward
1857 // reference or not. At module finalization we add the empty
1858 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001860 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001861 llvm::GlobalValue::ExternalLinkage,
1862 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001863 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001864 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865 // FIXME: Is this necessary? Why only for protocol?
1866 Entry->setAlignment(4);
1867 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001868
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869 return Entry;
1870}
1871
1872/*
1873 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001874 uint32_t size;
1875 struct objc_method_description_list *optional_instance_methods;
1876 struct objc_method_description_list *optional_class_methods;
1877 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001878 };
1879*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001880llvm::Constant *
1881CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1882 const ConstantVector &OptInstanceMethods,
1883 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001884 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001885 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001886 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001887 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888 Values[1] =
1889 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001890 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001891 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1892 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001893 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001894 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001895 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1896 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001897 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001898 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001899
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001900 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001901 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001902 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001903 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001904
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001905 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001906 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001907
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001908 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001909 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001910 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001911 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001912}
1913
1914/*
1915 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001916 struct objc_protocol_list *next;
1917 long count;
1918 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919 };
1920*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001921llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001922CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001923 ObjCProtocolDecl::protocol_iterator begin,
1924 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001925 std::vector<llvm::Constant*> ProtocolRefs;
1926
Daniel Dunbardbc93372008-08-21 21:57:41 +00001927 for (; begin != end; ++begin)
1928 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001929
1930 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001931 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001932 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001933
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001934 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001935 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001936
1937 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001938 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001939 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001940 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001941 ProtocolRefs.size() - 1);
1942 Values[2] =
1943 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1944 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001946
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001947 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001948 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001949 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001950 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001951 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001952}
1953
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001954void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1955 std::vector<llvm::Constant*> &Properties,
1956 const Decl *Container,
1957 const ObjCProtocolDecl *PROTO,
1958 const ObjCCommonTypesHelper &ObjCTypes) {
1959 std::vector<llvm::Constant*> Prop(2);
1960 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1961 E = PROTO->protocol_end(); P != E; ++P)
1962 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1963 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1964 E = PROTO->prop_end(); I != E; ++I) {
1965 const ObjCPropertyDecl *PD = *I;
1966 if (!PropertySet.insert(PD->getIdentifier()))
1967 continue;
1968 Prop[0] = GetPropertyName(PD->getIdentifier());
1969 Prop[1] = GetPropertyTypeString(PD, Container);
1970 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1971 }
1972}
1973
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001974/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001975 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001976 const char * const name;
1977 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001978 };
1979
1980 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001981 uint32_t entsize; // sizeof (struct _objc_property)
1982 uint32_t prop_count;
1983 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001984 };
1985*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001986llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001987 const Decl *Container,
1988 const ObjCContainerDecl *OCD,
1989 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001990 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001991 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001992 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1993 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001994 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001995 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001996 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001997 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001998 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001999 Prop));
2000 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002001 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002002 for (ObjCInterfaceDecl::all_protocol_iterator
2003 P = OID->all_referenced_protocol_begin(),
2004 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002005 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2006 ObjCTypes);
2007 }
2008 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2009 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2010 E = CD->protocol_end(); P != E; ++P)
2011 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2012 ObjCTypes);
2013 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002014
2015 // Return null for empty list.
2016 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002017 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002018
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002019 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002020 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002021 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002022 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2023 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002024 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002025 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002026 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002027 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002028
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002029 llvm::GlobalVariable *GV =
2030 CreateMetadataVar(Name, Init,
2031 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002032 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002033 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002034 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002035 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002036}
2037
2038/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002039 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002040 int count;
2041 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002042 };
2043*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002044llvm::Constant *
2045CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2046 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002047 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002048 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2049 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002050 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002051 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002052 Desc);
2053}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002054
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002055llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002056 const char *Section,
2057 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002058 // Return null for empty list.
2059 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002060 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002061
2062 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002063 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002064 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002065 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002066 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002067 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002068
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002069 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002070 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002071 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002072}
2073
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002074/*
2075 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002076 char *category_name;
2077 char *class_name;
2078 struct _objc_method_list *instance_methods;
2079 struct _objc_method_list *class_methods;
2080 struct _objc_protocol_list *protocols;
2081 uint32_t size; // <rdar://4585769>
2082 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002083 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002085void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002086 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002087
Mike Stumpf5408fe2009-05-16 07:57:57 +00002088 // FIXME: This is poor design, the OCD should have a pointer to the category
2089 // decl. Additionally, note that Category can be null for the @implementation
2090 // w/o an @interface case. Sema should just create one for us as it does for
2091 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002092 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002093 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002094 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002095
2096 llvm::SmallString<256> ExtName;
2097 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2098 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002099
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002100 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002101 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002102 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002103 // Instance methods should always be defined.
2104 InstanceMethods.push_back(GetMethodConstant(*i));
2105 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002107 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002108 // Class methods should always be defined.
2109 ClassMethods.push_back(GetMethodConstant(*i));
2110 }
2111
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002112 std::vector<llvm::Constant*> Values(7);
2113 Values[0] = GetClassName(OCD->getIdentifier());
2114 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002115 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002116 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002117 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002118 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002119 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002121 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002122 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002123 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002124 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002125 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002126 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002127 Category->protocol_begin(),
2128 Category->protocol_end());
2129 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002130 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002131 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002132 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002133
2134 // If there is no category @interface then there can be no properties.
2135 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002136 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002137 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002138 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002139 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002140 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002141
Owen Anderson08e25242009-07-27 22:29:56 +00002142 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002143 Values);
2144
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002146 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002147 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002148 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002149 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002150 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002151}
2152
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153// FIXME: Get from somewhere?
2154enum ClassFlags {
2155 eClassFlags_Factory = 0x00001,
2156 eClassFlags_Meta = 0x00002,
2157 // <rdr://5142207>
2158 eClassFlags_HasCXXStructors = 0x02000,
2159 eClassFlags_Hidden = 0x20000,
2160 eClassFlags_ABI2_Hidden = 0x00010,
2161 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2162};
2163
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164/*
2165 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002166 Class isa;
2167 Class super_class;
2168 const char *name;
2169 long version;
2170 long info;
2171 long instance_size;
2172 struct _objc_ivar_list *ivars;
2173 struct _objc_method_list *methods;
2174 struct _objc_cache *cache;
2175 struct _objc_protocol_list *protocols;
2176 // Objective-C 1.0 extensions (<rdr://4585769>)
2177 const char *ivar_layout;
2178 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 };
2180
2181 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002182*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002183void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002184 DefinedSymbols.insert(ID->getIdentifier());
2185
Chris Lattner8ec03f52008-11-24 03:54:41 +00002186 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002187 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002188 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002189 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002190 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002191 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002192 Interface->all_referenced_protocol_begin(),
2193 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002194 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002195 if (ID->getNumIvarInitializers())
2196 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002197 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002198 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199
2200 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002201 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002202 Flags |= eClassFlags_Hidden;
2203
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002204 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002205 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002206 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002207 // Instance methods should always be defined.
2208 InstanceMethods.push_back(GetMethodConstant(*i));
2209 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002210 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002211 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002212 // Class methods should always be defined.
2213 ClassMethods.push_back(GetMethodConstant(*i));
2214 }
2215
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002216 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002217 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002218 ObjCPropertyImplDecl *PID = *i;
2219
2220 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2221 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2222
2223 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2224 if (llvm::Constant *C = GetMethodConstant(MD))
2225 InstanceMethods.push_back(C);
2226 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2227 if (llvm::Constant *C = GetMethodConstant(MD))
2228 InstanceMethods.push_back(C);
2229 }
2230 }
2231
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002232 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002233 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002235 // Record a reference to the super class.
2236 LazySymbols.insert(Super->getIdentifier());
2237
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002238 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002239 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002240 ObjCTypes.ClassPtrTy);
2241 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002242 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243 }
2244 Values[ 2] = GetClassName(ID->getIdentifier());
2245 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002246 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2247 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2248 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002249 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002250 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002251 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002252 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002253 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002255 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002257 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002259 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002260 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002261 std::string Name("\01L_OBJC_CLASS_");
2262 Name += ClassName;
2263 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2264 // Check for a forward reference.
2265 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2266 if (GV) {
2267 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2268 "Forward metaclass reference has incorrect type.");
2269 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2270 GV->setInitializer(Init);
2271 GV->setSection(Section);
2272 GV->setAlignment(4);
2273 CGM.AddUsedGlobal(GV);
2274 }
2275 else
2276 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 DefinedClasses.push_back(GV);
2278}
2279
2280llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2281 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002282 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002284 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002285
Daniel Dunbar04d40782009-04-14 06:00:08 +00002286 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002288
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002289 std::vector<llvm::Constant*> Values(12);
2290 // The isa for the metaclass is the root of the hierarchy.
2291 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2292 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2293 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002294 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002295 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002297 // The super class for the metaclass is emitted as the name of the
2298 // super class. The runtime fixes this up to point to the
2299 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002301 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002302 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 ObjCTypes.ClassPtrTy);
2304 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002305 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 }
2307 Values[ 2] = GetClassName(ID->getIdentifier());
2308 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002309 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2310 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2311 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002312 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002313 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002314 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002315 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002316 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002318 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002319 Values[ 9] = Protocols;
2320 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002321 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002322 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002323 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002324 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325 Values);
2326
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002327 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002328 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002329
2330 // Check for a forward reference.
2331 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2332 if (GV) {
2333 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2334 "Forward metaclass reference has incorrect type.");
2335 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2336 GV->setInitializer(Init);
2337 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002338 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002339 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002340 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002341 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002343 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002344 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345
2346 return GV;
2347}
2348
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002349llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002350 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002351
Mike Stumpf5408fe2009-05-16 07:57:57 +00002352 // FIXME: Should we look these up somewhere other than the module. Its a bit
2353 // silly since we only generate these while processing an implementation, so
2354 // exactly one pointer would work if know when we entered/exitted an
2355 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002356
2357 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002358 // Previously, metaclass with internal linkage may have been defined.
2359 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002360 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2361 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002362 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2363 "Forward metaclass reference has incorrect type.");
2364 return GV;
2365 } else {
2366 // Generate as an external reference to keep a consistent
2367 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002368 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002369 llvm::GlobalValue::ExternalLinkage,
2370 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002371 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002372 }
2373}
2374
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002375llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2376 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2377
2378 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2379 true)) {
2380 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2381 "Forward class metadata reference has incorrect type.");
2382 return GV;
2383 } else {
2384 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2385 llvm::GlobalValue::ExternalLinkage,
2386 0,
2387 Name);
2388 }
2389}
2390
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391/*
2392 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002393 uint32_t size;
2394 const char *weak_ivar_layout;
2395 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396 };
2397*/
2398llvm::Constant *
2399CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002400 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002401 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002402
2403 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002404 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002405 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002406 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002407 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408
2409 // Return null if no extension bits are used.
2410 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002411 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002412
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002413 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002414 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002415 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002416 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002417 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002418}
2419
2420/*
2421 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002422 char *ivar_name;
2423 char *ivar_type;
2424 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002425 };
2426
2427 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002428 int ivar_count;
2429 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002431*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002433 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 std::vector<llvm::Constant*> Ivars, Ivar(3);
2435
2436 // When emitting the root class GCC emits ivar entries for the
2437 // actual class structure. It is not clear if we need to follow this
2438 // behavior; for now lets try and get away with not doing it. If so,
2439 // the cleanest solution would be to make up an ObjCInterfaceDecl
2440 // for the class.
2441 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002442 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002443
2444 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002445 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002446
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002447 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002448 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002449
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002450 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2451 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002452 // Ignore unnamed bit-fields.
2453 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002454 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002455 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2456 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002457 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002458 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002459 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002460 }
2461
2462 // Return null for empty list.
2463 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002464 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002465
2466 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002467 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002468 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002469 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002470 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002471 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002472
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002473 llvm::GlobalVariable *GV;
2474 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002475 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002476 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002477 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002478 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002479 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002480 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002481 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002482 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002483}
2484
2485/*
2486 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002487 SEL method_name;
2488 char *method_types;
2489 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002490 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002491
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002493 struct objc_method_list *obsolete;
2494 int count;
2495 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002496 };
2497*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002498
2499/// GetMethodConstant - Return a struct objc_method constant for the
2500/// given method if it has been defined. The result is null if the
2501/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002502llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002503 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002504 if (!Fn)
2505 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002506
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002507 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002508 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002509 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002510 ObjCTypes.SelectorPtrTy);
2511 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002512 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002513 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002514}
2515
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002516llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002517 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002518 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002519 // Return null for empty list.
2520 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002521 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002522
2523 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002524 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002525 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002526 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002527 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002528 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002529 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002531 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002532 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002533 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002534}
2535
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002536llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002537 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002538 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002539 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002540
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002541 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002542 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002543 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002544 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002545 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002546 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002547 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002548 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002549 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002550
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002551 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002552}
2553
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002554llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002555CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002556 llvm::Constant *Init,
2557 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002558 unsigned Align,
2559 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002560 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002561 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002562 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002563 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002564 if (Section)
2565 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002566 if (Align)
2567 GV->setAlignment(Align);
2568 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002569 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002570 return GV;
2571}
2572
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002573llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002574 // Abuse this interface function as a place to finalize.
2575 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002576 return NULL;
2577}
2578
Chris Lattner74391b42009-03-22 21:03:39 +00002579llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002580 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002581}
2582
Chris Lattner74391b42009-03-22 21:03:39 +00002583llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002584 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002585}
2586
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002587llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2588 return ObjCTypes.getCopyStructFn();
2589}
2590
Chris Lattner74391b42009-03-22 21:03:39 +00002591llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002592 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002593}
2594
John McCallf1549f62010-07-06 01:34:17 +00002595void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2596 return EmitTryOrSynchronizedStmt(CGF, S);
2597}
2598
2599void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2600 const ObjCAtSynchronizedStmt &S) {
2601 return EmitTryOrSynchronizedStmt(CGF, S);
2602}
2603
John McCallcc505292010-07-21 06:59:36 +00002604namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002605 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002606 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002607 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002608 llvm::Value *CallTryExitVar;
2609 llvm::Value *ExceptionData;
2610 ObjCTypesHelper &ObjCTypes;
2611 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002612 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002613 llvm::Value *CallTryExitVar,
2614 llvm::Value *ExceptionData,
2615 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002616 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002617 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2618
2619 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2620 // Check whether we need to call objc_exception_try_exit.
2621 // In optimized code, this branch will always be folded.
2622 llvm::BasicBlock *FinallyCallExit =
2623 CGF.createBasicBlock("finally.call_exit");
2624 llvm::BasicBlock *FinallyNoCallExit =
2625 CGF.createBasicBlock("finally.no_call_exit");
2626 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2627 FinallyCallExit, FinallyNoCallExit);
2628
2629 CGF.EmitBlock(FinallyCallExit);
2630 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2631 ->setDoesNotThrow();
2632
2633 CGF.EmitBlock(FinallyNoCallExit);
2634
2635 if (isa<ObjCAtTryStmt>(S)) {
2636 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002637 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2638 // Save the current cleanup destination in case there's
2639 // control flow inside the finally statement.
2640 llvm::Value *CurCleanupDest =
2641 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2642
John McCallcc505292010-07-21 06:59:36 +00002643 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2644
John McCalld96a8e72010-08-11 00:16:14 +00002645 if (CGF.HaveInsertPoint()) {
2646 CGF.Builder.CreateStore(CurCleanupDest,
2647 CGF.getNormalCleanupDestSlot());
2648 } else {
2649 // Currently, the end of the cleanup must always exist.
2650 CGF.EnsureInsertPoint();
2651 }
2652 }
John McCallcc505292010-07-21 06:59:36 +00002653 } else {
2654 // Emit objc_sync_exit(expr); as finally's sole statement for
2655 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002656 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002657 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2658 ->setDoesNotThrow();
2659 }
2660 }
2661 };
John McCall87bb5822010-07-31 23:20:56 +00002662
2663 class FragileHazards {
2664 CodeGenFunction &CGF;
2665 llvm::SmallVector<llvm::Value*, 20> Locals;
2666 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2667
2668 llvm::InlineAsm *ReadHazard;
2669 llvm::InlineAsm *WriteHazard;
2670
2671 llvm::FunctionType *GetAsmFnType();
2672
2673 void collectLocals();
2674 void emitReadHazard(CGBuilderTy &Builder);
2675
2676 public:
2677 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002678
John McCall87bb5822010-07-31 23:20:56 +00002679 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002680 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002681 };
2682}
2683
2684/// Create the fragile-ABI read and write hazards based on the current
2685/// state of the function, which is presumed to be immediately prior
2686/// to a @try block. These hazards are used to maintain correct
2687/// semantics in the face of optimization and the fragile ABI's
2688/// cavalier use of setjmp/longjmp.
2689FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2690 collectLocals();
2691
2692 if (Locals.empty()) return;
2693
2694 // Collect all the blocks in the function.
2695 for (llvm::Function::iterator
2696 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2697 BlocksBeforeTry.insert(&*I);
2698
2699 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2700
2701 // Create a read hazard for the allocas. This inhibits dead-store
2702 // optimizations and forces the values to memory. This hazard is
2703 // inserted before any 'throwing' calls in the protected scope to
2704 // reflect the possibility that the variables might be read from the
2705 // catch block if the call throws.
2706 {
2707 std::string Constraint;
2708 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2709 if (I) Constraint += ',';
2710 Constraint += "*m";
2711 }
2712
2713 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2714 }
2715
2716 // Create a write hazard for the allocas. This inhibits folding
2717 // loads across the hazard. This hazard is inserted at the
2718 // beginning of the catch path to reflect the possibility that the
2719 // variables might have been written within the protected scope.
2720 {
2721 std::string Constraint;
2722 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2723 if (I) Constraint += ',';
2724 Constraint += "=*m";
2725 }
2726
2727 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2728 }
2729}
2730
2731/// Emit a write hazard at the current location.
2732void FragileHazards::emitWriteHazard() {
2733 if (Locals.empty()) return;
2734
2735 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2736 ->setDoesNotThrow();
2737}
2738
John McCall87bb5822010-07-31 23:20:56 +00002739void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2740 assert(!Locals.empty());
2741 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2742 ->setDoesNotThrow();
2743}
2744
2745/// Emit read hazards in all the protected blocks, i.e. all the blocks
2746/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002747void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002748 if (Locals.empty()) return;
2749
2750 CGBuilderTy Builder(CGF.getLLVMContext());
2751
2752 // Iterate through all blocks, skipping those prior to the try.
2753 for (llvm::Function::iterator
2754 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2755 llvm::BasicBlock &BB = *FI;
2756 if (BlocksBeforeTry.count(&BB)) continue;
2757
2758 // Walk through all the calls in the block.
2759 for (llvm::BasicBlock::iterator
2760 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2761 llvm::Instruction &I = *BI;
2762
2763 // Ignore instructions that aren't non-intrinsic calls.
2764 // These are the only calls that can possibly call longjmp.
2765 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2766 if (isa<llvm::IntrinsicInst>(I))
2767 continue;
2768
2769 // Ignore call sites marked nounwind. This may be questionable,
2770 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2771 llvm::CallSite CS(&I);
2772 if (CS.doesNotThrow()) continue;
2773
John McCall0b251722010-08-04 05:59:32 +00002774 // Insert a read hazard before the call. This will ensure that
2775 // any writes to the locals are performed before making the
2776 // call. If the call throws, then this is sufficient to
2777 // guarantee correctness as long as it doesn't also write to any
2778 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002779 Builder.SetInsertPoint(&BB, BI);
2780 emitReadHazard(Builder);
2781 }
2782 }
2783}
2784
2785static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2786 if (V) S.insert(V);
2787}
2788
2789void FragileHazards::collectLocals() {
2790 // Compute a set of allocas to ignore.
2791 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2792 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2793 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2794 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2795
2796 // Collect all the allocas currently in the function. This is
2797 // probably way too aggressive.
2798 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2799 for (llvm::BasicBlock::iterator
2800 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2801 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2802 Locals.push_back(&*I);
2803}
2804
2805llvm::FunctionType *FragileHazards::GetAsmFnType() {
2806 std::vector<const llvm::Type *> Tys(Locals.size());
2807 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2808 Tys[I] = Locals[I]->getType();
2809 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002810}
2811
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002812/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002813
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002814 Objective-C setjmp-longjmp (sjlj) Exception Handling
2815 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002816
John McCallf1549f62010-07-06 01:34:17 +00002817 A catch buffer is a setjmp buffer plus:
2818 - a pointer to the exception that was caught
2819 - a pointer to the previous exception data buffer
2820 - two pointers of reserved storage
2821 Therefore catch buffers form a stack, with a pointer to the top
2822 of the stack kept in thread-local storage.
2823
2824 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2825 objc_exception_try_exit pops the given catch buffer, which is
2826 required to be the top of the EH stack.
2827 objc_exception_throw pops the top of the EH stack, writes the
2828 thrown exception into the appropriate field, and longjmps
2829 to the setjmp buffer. It crashes the process (with a printf
2830 and an abort()) if there are no catch buffers on the stack.
2831 objc_exception_extract just reads the exception pointer out of the
2832 catch buffer.
2833
2834 There's no reason an implementation couldn't use a light-weight
2835 setjmp here --- something like __builtin_setjmp, but API-compatible
2836 with the heavyweight setjmp. This will be more important if we ever
2837 want to implement correct ObjC/C++ exception interactions for the
2838 fragile ABI.
2839
2840 Note that for this use of setjmp/longjmp to be correct, we may need
2841 to mark some local variables volatile: if a non-volatile local
2842 variable is modified between the setjmp and the longjmp, it has
2843 indeterminate value. For the purposes of LLVM IR, it may be
2844 sufficient to make loads and stores within the @try (to variables
2845 declared outside the @try) volatile. This is necessary for
2846 optimized correctness, but is not currently being done; this is
2847 being tracked as rdar://problem/8160285
2848
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002849 The basic framework for a @try-catch-finally is as follows:
2850 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002851 objc_exception_data d;
2852 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002853 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002854
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002855 objc_exception_try_enter(&d);
2856 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002857 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002858 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002859 // exception path
2860 id _caught = objc_exception_extract(&d);
2861
2862 // enter new try scope for handlers
2863 if (!setjmp(d.jmp_buf)) {
2864 ... match exception and execute catch blocks ...
2865
2866 // fell off end, rethrow.
2867 _rethrow = _caught;
2868 ... jump-through-finally to finally_rethrow ...
2869 } else {
2870 // exception in catch block
2871 _rethrow = objc_exception_extract(&d);
2872 _call_try_exit = false;
2873 ... jump-through-finally to finally_rethrow ...
2874 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002875 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002876 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002877
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002878 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002879 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002880 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002881
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002882 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002883 ... dispatch to finally destination ...
2884
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002885 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002886 objc_exception_throw(_rethrow);
2887
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002888 finally_end:
2889 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002890
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002891 This framework differs slightly from the one gcc uses, in that gcc
2892 uses _rethrow to determine if objc_exception_try_exit should be called
2893 and if the object should be rethrown. This breaks in the face of
2894 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002895
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002896 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002897
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002898 - If there are no catch blocks, then we avoid emitting the second
2899 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002900
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002901 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2902 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002903
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002904 - FIXME: If there is no @finally block we can do a few more
2905 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002906
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002907 Rethrows and Jumps-Through-Finally
2908 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002909
John McCallf1549f62010-07-06 01:34:17 +00002910 '@throw;' is supported by pushing the currently-caught exception
2911 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002912
John McCallf1549f62010-07-06 01:34:17 +00002913 Branches through the @finally block are handled with an ordinary
2914 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2915 exceptions are not compatible with C++ exceptions, and this is
2916 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002917
John McCallf1549f62010-07-06 01:34:17 +00002918 @synchronized(expr) { stmt; } is emitted as if it were:
2919 id synch_value = expr;
2920 objc_sync_enter(synch_value);
2921 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002922*/
2923
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002924void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2925 const Stmt &S) {
2926 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002927
2928 // A destination for the fall-through edges of the catch handlers to
2929 // jump to.
2930 CodeGenFunction::JumpDest FinallyEnd =
2931 CGF.getJumpDestInCurrentScope("finally.end");
2932
2933 // A destination for the rethrow edge of the catch handlers to jump
2934 // to.
2935 CodeGenFunction::JumpDest FinallyRethrow =
2936 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002937
Daniel Dunbar1c566672009-02-24 01:43:46 +00002938 // For @synchronized, call objc_sync_enter(sync.expr). The
2939 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002940 // @synchronized. We can't avoid a temp here because we need the
2941 // value to be preserved. If the backend ever does liveness
2942 // correctly after setjmp, this will be unnecessary.
2943 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002944 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002945 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002946 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2947 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002948 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2949 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002950
2951 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2952 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002953 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002954
John McCall0b251722010-08-04 05:59:32 +00002955 // Allocate memory for the setjmp buffer. This needs to be kept
2956 // live throughout the try and catch blocks.
2957 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2958 "exceptiondata.ptr");
2959
John McCall87bb5822010-07-31 23:20:56 +00002960 // Create the fragile hazards. Note that this will not capture any
2961 // of the allocas required for exception processing, but will
2962 // capture the current basic block (which extends all the way to the
2963 // setjmp call) as "before the @try".
2964 FragileHazards Hazards(CGF);
2965
John McCallf1549f62010-07-06 01:34:17 +00002966 // Create a flag indicating whether the cleanup needs to call
2967 // objc_exception_try_exit. This is true except when
2968 // - no catches match and we're branching through the cleanup
2969 // just to rethrow the exception, or
2970 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002971 // The setjmp-safety rule here is that we should always store to this
2972 // variable in a place that dominates the branch through the cleanup
2973 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002974 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002975 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002976
John McCallf1549f62010-07-06 01:34:17 +00002977 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002978 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002979 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002980 CallTryExitVar,
2981 ExceptionData,
2982 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002983
2984 // Enter a try block:
2985 // - Call objc_exception_try_enter to push ExceptionData on top of
2986 // the EH stack.
2987 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2988 ->setDoesNotThrow();
2989
2990 // - Call setjmp on the exception data buffer.
2991 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2992 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2993 llvm::Value *SetJmpBuffer =
2994 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2995 llvm::CallInst *SetJmpResult =
2996 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2997 SetJmpResult->setDoesNotThrow();
2998
2999 // If setjmp returned 0, enter the protected block; otherwise,
3000 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003001 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3002 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003003 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003004 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3005 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003006
John McCallf1549f62010-07-06 01:34:17 +00003007 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003008 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003009 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003010 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003011 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003012
3013 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003014
John McCallf1549f62010-07-06 01:34:17 +00003015 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003016 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003017
John McCall87bb5822010-07-31 23:20:56 +00003018 // Don't optimize loads of the in-scope locals across this point.
3019 Hazards.emitWriteHazard();
3020
John McCallf1549f62010-07-06 01:34:17 +00003021 // For a @synchronized (or a @try with no catches), just branch
3022 // through the cleanup to the rethrow block.
3023 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3024 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003025 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003026 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003027
3028 // Otherwise, we have to match against the caught exceptions.
3029 } else {
John McCall0b251722010-08-04 05:59:32 +00003030 // Retrieve the exception object. We may emit multiple blocks but
3031 // nothing can cross this so the value is already in SSA form.
3032 llvm::CallInst *Caught =
3033 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3034 ExceptionData, "caught");
3035 Caught->setDoesNotThrow();
3036
John McCallf1549f62010-07-06 01:34:17 +00003037 // Push the exception to rethrow onto the EH value stack for the
3038 // benefit of any @throws in the handlers.
3039 CGF.ObjCEHValueStack.push_back(Caught);
3040
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003041 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003042
John McCall0b251722010-08-04 05:59:32 +00003043 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003044
John McCall0b251722010-08-04 05:59:32 +00003045 llvm::BasicBlock *CatchBlock = 0;
3046 llvm::BasicBlock *CatchHandler = 0;
3047 if (HasFinally) {
3048 // Enter a new exception try block (in case a @catch block
3049 // throws an exception).
3050 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3051 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003052
John McCall0b251722010-08-04 05:59:32 +00003053 llvm::CallInst *SetJmpResult =
3054 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3055 "setjmp.result");
3056 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003057
John McCall0b251722010-08-04 05:59:32 +00003058 llvm::Value *Threw =
3059 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3060
3061 CatchBlock = CGF.createBasicBlock("catch");
3062 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3063 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3064
3065 CGF.EmitBlock(CatchBlock);
3066 }
3067
3068 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
Daniel Dunbar55e40722008-09-27 07:03:52 +00003070 // Handle catch list. As a special case we check if everything is
3071 // matched and avoid generating code for falling off the end if
3072 // so.
3073 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003074 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3075 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003076
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003077 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003078 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003079
Anders Carlsson80f25672008-09-09 17:59:25 +00003080 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003081 if (!CatchParam) {
3082 AllMatched = true;
3083 } else {
John McCall183700f2009-09-21 23:43:11 +00003084 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085
John McCallf1549f62010-07-06 01:34:17 +00003086 // catch(id e) always matches under this ABI, since only
3087 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003088 // FIXME: For the time being we also match id<X>; this should
3089 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003090 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003091 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003092 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093
John McCallf1549f62010-07-06 01:34:17 +00003094 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003096 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3097
Anders Carlssondde0a942008-09-11 09:15:33 +00003098 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003099 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003100 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003101
3102 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003103 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003104 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105
Anders Carlssondde0a942008-09-11 09:15:33 +00003106 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003107
3108 // The scope of the catch variable ends right here.
3109 CatchVarCleanups.ForceCleanup();
3110
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003111 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003112 break;
3113 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003114
Steve Naroff14108da2009-07-10 23:34:53 +00003115 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003116 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003117
3118 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003119 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3120 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003121
3122 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003123 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003124
John McCallf1549f62010-07-06 01:34:17 +00003125 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003126 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3127 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003128 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003129
John McCallf1549f62010-07-06 01:34:17 +00003130 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3131 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003132
3133 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003134 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003135
Anders Carlsson80f25672008-09-09 17:59:25 +00003136 // Emit the @catch block.
3137 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003138
3139 // Collect any cleanups for the catch variable. The scope lasts until
3140 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003141 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003142
Steve Naroff7ba138a2009-03-03 19:52:17 +00003143 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003144 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003145
John McCallf1549f62010-07-06 01:34:17 +00003146 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003147 llvm::Value *Tmp =
3148 CGF.Builder.CreateBitCast(Caught,
3149 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003150 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003151 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003152
Anders Carlssondde0a942008-09-11 09:15:33 +00003153 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003154
3155 // We're done with the catch variable.
3156 CatchVarCleanups.ForceCleanup();
3157
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003158 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003159
Anders Carlsson80f25672008-09-09 17:59:25 +00003160 CGF.EmitBlock(NextCatchBlock);
3161 }
3162
John McCallf1549f62010-07-06 01:34:17 +00003163 CGF.ObjCEHValueStack.pop_back();
3164
John McCall0b251722010-08-04 05:59:32 +00003165 // If nothing wanted anything to do with the caught exception,
3166 // kill the extract call.
3167 if (Caught->use_empty())
3168 Caught->eraseFromParent();
3169
3170 if (!AllMatched)
3171 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3172
3173 if (HasFinally) {
3174 // Emit the exception handler for the @catch blocks.
3175 CGF.EmitBlock(CatchHandler);
3176
3177 // In theory we might now need a write hazard, but actually it's
3178 // unnecessary because there's no local-accessing code between
3179 // the try's write hazard and here.
3180 //Hazards.emitWriteHazard();
3181
3182 // Don't pop the catch handler; the throw already did.
3183 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003184 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003185 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003186 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003187
John McCall87bb5822010-07-31 23:20:56 +00003188 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003189 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003190
John McCallf1549f62010-07-06 01:34:17 +00003191 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003192 CGF.Builder.restoreIP(TryFallthroughIP);
3193 if (CGF.HaveInsertPoint())
3194 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003195 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003196 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003197
John McCallf1549f62010-07-06 01:34:17 +00003198 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003199 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003200 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003201 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003202 // Just look in the buffer for the exception to throw.
3203 llvm::CallInst *Caught =
3204 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3205 ExceptionData);
3206 Caught->setDoesNotThrow();
3207
3208 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003209 ->setDoesNotThrow();
3210 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003211 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003212
John McCall87bb5822010-07-31 23:20:56 +00003213 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003214}
3215
3216void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003217 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003218 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003219
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003220 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3221 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003222 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003223 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3224 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003225 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003226 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003227 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003229
John McCallf1549f62010-07-06 01:34:17 +00003230 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3231 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003232 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003233
3234 // Clear the insertion point to indicate we are in unreachable code.
3235 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003236}
3237
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003238/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003239/// object: objc_read_weak (id *src)
3240///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003241llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003242 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003243 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003244 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3245 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3246 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003247 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003248 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003249 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003250 return read_weak;
3251}
3252
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003253/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3254/// objc_assign_weak (id src, id *dst)
3255///
3256void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003257 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003258 const llvm::Type * SrcTy = src->getType();
3259 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003260 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003261 assert(Size <= 8 && "does not support size > 8");
3262 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003263 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003264 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3265 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003266 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3267 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003268 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003269 src, dst, "weakassign");
3270 return;
3271}
3272
Fariborz Jahanian58626502008-11-19 00:59:10 +00003273/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3274/// objc_assign_global (id src, id *dst)
3275///
3276void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003277 llvm::Value *src, llvm::Value *dst,
3278 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003279 const llvm::Type * SrcTy = src->getType();
3280 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003281 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003282 assert(Size <= 8 && "does not support size > 8");
3283 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003285 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3286 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003287 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3288 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003289 if (!threadlocal)
3290 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3291 src, dst, "globalassign");
3292 else
3293 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3294 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003295 return;
3296}
3297
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003298/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003299/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003300///
3301void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003302 llvm::Value *src, llvm::Value *dst,
3303 llvm::Value *ivarOffset) {
3304 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003305 const llvm::Type * SrcTy = src->getType();
3306 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003307 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003308 assert(Size <= 8 && "does not support size > 8");
3309 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003310 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003311 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3312 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003313 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3314 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003315 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3316 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003317 return;
3318}
3319
Fariborz Jahanian58626502008-11-19 00:59:10 +00003320/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3321/// objc_assign_strongCast (id src, id *dst)
3322///
3323void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003324 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003325 const llvm::Type * SrcTy = src->getType();
3326 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003327 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003328 assert(Size <= 8 && "does not support size > 8");
3329 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003330 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003331 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3332 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003333 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3334 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003335 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003336 src, dst, "weakassign");
3337 return;
3338}
3339
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003340void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003341 llvm::Value *DestPtr,
3342 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003343 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003344 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3345 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003346 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003347 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003348 return;
3349}
3350
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003351/// EmitObjCValueForIvar - Code Gen for ivar reference.
3352///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003353LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3354 QualType ObjectTy,
3355 llvm::Value *BaseValue,
3356 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003357 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003358 const ObjCInterfaceDecl *ID =
3359 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003360 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3361 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003362}
3363
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003364llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003365 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003366 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003367 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003368 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003369 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3370 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003371}
3372
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003373/* *** Private Interface *** */
3374
3375/// EmitImageInfo - Emit the image info marker used to encode some module
3376/// level information.
3377///
3378/// See: <rdr://4810609&4810587&4810587>
3379/// struct IMAGE_INFO {
3380/// unsigned version;
3381/// unsigned flags;
3382/// };
3383enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003384 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003385 eImageInfo_GarbageCollected = (1 << 1),
3386 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003387 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3388
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003389 // A flag indicating that the module has no instances of a @synthesize of a
3390 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003391 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003392};
3393
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003394void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003395 unsigned version = 0; // Version is unused?
3396 unsigned flags = 0;
3397
3398 // FIXME: Fix and continue?
3399 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3400 flags |= eImageInfo_GarbageCollected;
3401 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3402 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003403
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003404 // We never allow @synthesize of a superclass property.
3405 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003406
Chris Lattner77b89b82010-06-27 07:15:29 +00003407 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3408
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003409 // Emitted as int[2];
3410 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003411 llvm::ConstantInt::get(Int32Ty, version),
3412 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003413 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003414 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003415
3416 const char *Section;
3417 if (ObjCABI == 1)
3418 Section = "__OBJC, __image_info,regular";
3419 else
3420 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003421 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003422 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003423 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003424 Section,
3425 0,
3426 true);
3427 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003428}
3429
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003430
3431// struct objc_module {
3432// unsigned long version;
3433// unsigned long size;
3434// const char *name;
3435// Symtab symtab;
3436// };
3437
3438// FIXME: Get from somewhere
3439static const int ModuleVersion = 7;
3440
3441void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003442 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003443
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003444 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003445 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3446 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003447 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003448 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003449 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003450 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003451 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003452 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003453 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003454}
3455
3456llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003457 unsigned NumClasses = DefinedClasses.size();
3458 unsigned NumCategories = DefinedCategories.size();
3459
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003460 // Return null if no symbols were defined.
3461 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003462 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003463
3464 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003465 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003466 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003467 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3468 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003469
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003470 // The runtime expects exactly the list of defined classes followed
3471 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003472 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003473 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003474 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003475 ObjCTypes.Int8PtrTy);
3476 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003477 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003478 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003479 ObjCTypes.Int8PtrTy);
3480
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003481 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003482 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003483 NumClasses + NumCategories),
3484 Symbols);
3485
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003486 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003487
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003488 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003489 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3490 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003491 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003492 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003493}
3494
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003495llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003496 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003497 LazySymbols.insert(ID->getIdentifier());
3498
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003499 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003500
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003501 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003502 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003503 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003504 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003505 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003506 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3507 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003508 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003509 }
3510
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003511 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003512}
3513
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003514llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3515 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003516 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003517
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003518 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003519 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003520 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003521 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003522 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003523 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3524 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003525 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003526 }
3527
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003528 if (lvalue)
3529 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003530 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003531}
3532
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003533llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003534 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003535
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003536 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003537 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003538 llvm::ConstantArray::get(VMContext,
3539 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003540 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003541 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003542
Owen Andersona1cf15f2009-07-14 23:10:40 +00003543 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003544}
3545
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003546llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3547 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3548 I = MethodDefinitions.find(MD);
3549 if (I != MethodDefinitions.end())
3550 return I->second;
3551
3552 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3553 // MD isn't emitted yet because it comes from PCH.
3554 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3555 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3556 return MethodDefinitions[MD];
3557 }
3558
3559 return NULL;
3560}
3561
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003562/// GetIvarLayoutName - Returns a unique constant for the given
3563/// ivar layout bitmap.
3564llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003565 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003566 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003567}
3568
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003569void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003570 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003571 bool ForStrongLayout,
3572 bool &HasUnion) {
3573 const RecordDecl *RD = RT->getDecl();
3574 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003575 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003576 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003577 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003578 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003579
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003580 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3581 ForStrongLayout, HasUnion);
3582}
3583
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003584void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585 const llvm::StructLayout *Layout,
3586 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003587 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003588 unsigned int BytePos, bool ForStrongLayout,
3589 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003590 bool IsUnion = (RD && RD->isUnion());
3591 uint64_t MaxUnionIvarSize = 0;
3592 uint64_t MaxSkippedUnionIvarSize = 0;
3593 FieldDecl *MaxField = 0;
3594 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003595 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003596 uint64_t MaxFieldOffset = 0;
3597 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003598 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003600 if (RecFields.empty())
3601 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003602 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3603 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3604
Chris Lattnerf1690852009-03-31 08:48:01 +00003605 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003606 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003607 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003608 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003609 // Note that 'i' here is actually the field index inside RD of Field,
3610 // although this dependency is hidden.
3611 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3612 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003613 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003614 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003615
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003616 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003617 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003618 LastFieldBitfieldOrUnnamed = Field;
3619 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003620 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003621 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003622
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003623 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003624 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003625 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003626 if (FQT->isUnionType())
3627 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003629 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003630 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003631 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003632 continue;
3633 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003634
Chris Lattnerf1690852009-03-31 08:48:01 +00003635 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003636 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003637 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003638 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003639 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003641 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3642 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003643 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003644 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003645 FQT = CArray->getElementType();
3646 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003647
3648 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003649 "layout for array of unions not supported");
3650 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003651 int OldIndex = IvarsInfo.size() - 1;
3652 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003653
Ted Kremenek6217b802009-07-29 21:53:49 +00003654 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003655 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003656 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003657
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003658 // Replicate layout information for each array element. Note that
3659 // one element is already done.
3660 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003661 for (int FirstIndex = IvarsInfo.size() - 1,
3662 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003663 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003664 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3665 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3666 IvarsInfo[i].ivar_size));
3667 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3668 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3669 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003670 }
3671 continue;
3672 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003673 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003674 // At this point, we are done with Record/Union and array there of.
3675 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003676 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003677
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003678 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003679 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3680 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003681 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003682 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003683 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003684 MaxUnionIvarSize = UnionIvarSize;
3685 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003686 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003687 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003688 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003689 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003690 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003691 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003692 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003693 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3694 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003695 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003696 // FIXME: Why the asymmetry? We divide by word size in bits on other
3697 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003698 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003699 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003700 MaxSkippedUnionIvarSize = UnionIvarSize;
3701 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003702 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003703 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003704 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003705 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003706 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003707 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003708 }
3709 }
3710 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003711
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003712 if (LastFieldBitfieldOrUnnamed) {
3713 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3714 // Last field was a bitfield. Must update skip info.
3715 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3716 uint64_t BitFieldSize =
3717 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3718 GC_IVAR skivar;
3719 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3720 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3721 + ((BitFieldSize % ByteSizeInBits) != 0);
3722 SkipIvars.push_back(skivar);
3723 } else {
3724 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3725 // Last field was unnamed. Must update skip info.
3726 unsigned FieldSize
3727 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3728 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3729 FieldSize / ByteSizeInBits));
3730 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003731 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003732
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003733 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003734 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003735 MaxUnionIvarSize));
3736 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003737 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003738 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003739}
3740
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003741/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3742/// the computations and returning the layout bitmap (for ivar or blocks) in
3743/// the given argument BitMap string container. Routine reads
3744/// two containers, IvarsInfo and SkipIvars which are assumed to be
3745/// filled already by the caller.
3746llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003747 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003748 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003749
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003750 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003751 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003752 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003753 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003754 if (IvarsInfo[0].ivar_bytepos == 0) {
3755 WordsToSkip = 0;
3756 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003757 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003758 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3759 WordsToScan = IvarsInfo[0].ivar_size;
3760 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003761 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003762 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003763 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003764 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003765 // consecutive 'scanned' object pointers.
3766 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003767 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003768 // Skip over 'gc'able object pointer which lay over each other.
3769 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3770 continue;
3771 // Must skip over 1 or more words. We save current skip/scan values
3772 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003773 SKIP_SCAN SkScan;
3774 SkScan.skip = WordsToSkip;
3775 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003776 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003777
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003778 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003779 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3780 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003781 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003782 WordsToSkip = 0;
3783 WordsToScan = IvarsInfo[i].ivar_size;
3784 }
3785 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003786 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003787 SKIP_SCAN SkScan;
3788 SkScan.skip = WordsToSkip;
3789 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003790 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003791 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003792
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003793 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003794 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003795 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003796 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003797 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003798 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003799 IvarsInfo[LastIndex].ivar_bytepos +
3800 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003801 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003802 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003803 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003804 SKIP_SCAN SkScan;
3805 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3806 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003807 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003808 }
3809 }
3810 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3811 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003812 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003813 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003814 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3815 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3816 // 0xM0 followed by 0x0N detected.
3817 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3818 for (int j = i+1; j < SkipScan; j++)
3819 SkipScanIvars[j] = SkipScanIvars[j+1];
3820 --SkipScan;
3821 }
3822 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003823
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003824 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003825 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003826 unsigned char byte;
3827 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3828 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3829 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3830 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003831
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003832 // first skip big.
3833 for (unsigned int ix = 0; ix < skip_big; ix++)
3834 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003835
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003836 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003837 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003838 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003839 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003840 byte |= 0xf;
3841 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003842 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003843 byte |= scan_small;
3844 scan_small = 0;
3845 }
3846 BitMap += byte;
3847 }
3848 // next scan big
3849 for (unsigned int ix = 0; ix < scan_big; ix++)
3850 BitMap += (unsigned char)(0x0f);
3851 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003852 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003853 byte = scan_small;
3854 BitMap += byte;
3855 }
3856 }
3857 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003858 unsigned char zero = 0;
3859 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003860
3861 llvm::GlobalVariable * Entry =
3862 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3863 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3864 "__TEXT,__cstring,cstring_literals",
3865 1, true);
3866 return getConstantGEP(VMContext, Entry, 0, 0);
3867}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003868
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003869/// BuildIvarLayout - Builds ivar layout bitmap for the class
3870/// implementation for the __strong or __weak case.
3871/// The layout map displays which words in ivar list must be skipped
3872/// and which must be scanned by GC (see below). String is built of bytes.
3873/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3874/// of words to skip and right nibble is count of words to scan. So, each
3875/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3876/// represented by a 0x00 byte which also ends the string.
3877/// 1. when ForStrongLayout is true, following ivars are scanned:
3878/// - id, Class
3879/// - object *
3880/// - __strong anything
3881///
3882/// 2. When ForStrongLayout is false, following ivars are scanned:
3883/// - __weak anything
3884///
3885llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3886 const ObjCImplementationDecl *OMD,
3887 bool ForStrongLayout) {
3888 bool hasUnion = false;
3889
3890 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3891 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3892 return llvm::Constant::getNullValue(PtrTy);
3893
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003894 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003895 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003896 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003897
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003898 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003899 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3900 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3901
3902 if (RecFields.empty())
3903 return llvm::Constant::getNullValue(PtrTy);
3904
3905 SkipIvars.clear();
3906 IvarsInfo.clear();
3907
3908 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3909 if (IvarsInfo.empty())
3910 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003911 // Sort on byte position in case we encounterred a union nested in
3912 // the ivar list.
3913 if (hasUnion && !IvarsInfo.empty())
3914 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3915 if (hasUnion && !SkipIvars.empty())
3916 std::sort(SkipIvars.begin(), SkipIvars.end());
3917
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003918 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003919 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003920
3921 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003922 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003923 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003924 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003925 const unsigned char *s = (unsigned char*)BitMap.c_str();
3926 for (unsigned i = 0; i < BitMap.size(); i++)
3927 if (!(s[i] & 0xf0))
3928 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3929 else
3930 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3931 printf("\n");
3932 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003933 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003934}
3935
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003936llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003937 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3938
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003939 // FIXME: Avoid std::string copying.
3940 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003941 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003942 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003943 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003944 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003945
Owen Andersona1cf15f2009-07-14 23:10:40 +00003946 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003947}
3948
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003949// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003950llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003951 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3952}
3953
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003954llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003955 std::string TypeStr;
3956 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3957
3958 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003959
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003960 if (!Entry)
3961 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003962 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003963 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003964 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003965
Owen Andersona1cf15f2009-07-14 23:10:40 +00003966 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003967}
3968
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003969llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003970 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003971 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3972 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003973
3974 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3975
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003976 if (!Entry)
3977 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003978 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003979 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003980 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003981
Owen Andersona1cf15f2009-07-14 23:10:40 +00003982 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003983}
3984
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003985// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003986llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003987 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003988
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003989 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003990 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003991 llvm::ConstantArray::get(VMContext,
3992 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003993 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003994 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003995
Owen Andersona1cf15f2009-07-14 23:10:40 +00003996 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003997}
3998
3999// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004000// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004001llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004002CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4003 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004004 std::string TypeStr;
4005 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004006 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4007}
4008
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004009void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004010 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004011 llvm::SmallVectorImpl<char> &Name) {
4012 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004013 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004014 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4015 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004016 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004017 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004018 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004019 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004020}
4021
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004022void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004023 EmitModuleInfo();
4024
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004025 // Emit the dummy bodies for any protocols which were referenced but
4026 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004027 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004028 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4029 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004030 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004031
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004032 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004033 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004034 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004035 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004036 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004037 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004038 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004039 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004040 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004041 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004042 }
4043
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004044 // Add assembler directives to add lazy undefined symbol references
4045 // for classes which are referenced but not defined. This is
4046 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004047 //
4048 // FIXME: It would be nice if we had an LLVM construct for this.
4049 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4050 llvm::SmallString<256> Asm;
4051 Asm += CGM.getModule().getModuleInlineAsm();
4052 if (!Asm.empty() && Asm.back() != '\n')
4053 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004054
Daniel Dunbar33063492009-09-07 00:20:42 +00004055 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004056 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4057 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004058 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4059 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004060 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004061 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004062 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004063 }
4064
4065 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4066 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4067 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4068 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004069
Daniel Dunbar33063492009-09-07 00:20:42 +00004070 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004071 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004072}
4073
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004074CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004075 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004076 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004077 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004078 ObjCABI = 2;
4079}
4080
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004081/* *** */
4082
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004083ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004084 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004085 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4086 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004087
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004088 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004089 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004090 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004091 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004092 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004093
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004094 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004095 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004096 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004097
Mike Stumpf5408fe2009-05-16 07:57:57 +00004098 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4099 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004100 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004101 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004102
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004103 // I'm not sure I like this. The implicit coordination is a bit
4104 // gross. We should solve this in a reasonable fashion because this
4105 // is a pretty common task (match some runtime data structure with
4106 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004107
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004108 // FIXME: This is leaked.
4109 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004110
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004111 // struct _objc_super {
4112 // id self;
4113 // Class cls;
4114 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004115 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004116 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004117 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004118 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004119 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004120 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004122 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004123 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004125 SuperCTy = Ctx.getTagDeclType(RD);
4126 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004128 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004129 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4130
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004131 // struct _prop_t {
4132 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004133 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004134 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004135 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004136 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004137 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004138
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004139 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004140 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004141 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004142 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004143 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004144 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004145 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004146 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004147 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004148 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004149 PropertyListTy);
4150 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004151 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004153 // struct _objc_method {
4154 // SEL _cmd;
4155 // char *method_type;
4156 // char *_imp;
4157 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004158 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004159 Int8PtrTy,
4160 Int8PtrTy,
4161 NULL);
4162 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004164 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004165 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004166 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004167 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004168}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004169
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004170ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004171 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004172 // struct _objc_method_description {
4173 // SEL name;
4174 // char *types;
4175 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004177 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004178 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004179 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004180 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004181 MethodDescriptionTy);
4182
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004183 // struct _objc_method_description_list {
4184 // int count;
4185 // struct _objc_method_description[1];
4186 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004188 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004189 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004190 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004191 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004192 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004193
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004194 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004195 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004196 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004197
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004198 // Protocol description structures
4199
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004200 // struct _objc_protocol_extension {
4201 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4202 // struct _objc_method_description_list *optional_instance_methods;
4203 // struct _objc_method_description_list *optional_class_methods;
4204 // struct _objc_property_list *instance_properties;
4205 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004206 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004207 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004208 MethodDescriptionListPtrTy,
4209 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004210 PropertyListPtrTy,
4211 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004213 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004215 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004216 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004217
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004218 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004219
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004220 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4221 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004222
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004223 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004224 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004225 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004226 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004227 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004228 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004229 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4230
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004231 // struct _objc_protocol {
4232 // struct _objc_protocol_extension *isa;
4233 // char *protocol_name;
4234 // struct _objc_protocol **_objc_protocol_list;
4235 // struct _objc_method_description_list *instance_methods;
4236 // struct _objc_method_description_list *class_methods;
4237 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004238 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004239 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004240 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004241 MethodDescriptionListPtrTy,
4242 MethodDescriptionListPtrTy,
4243 NULL);
4244 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4245
4246 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004247 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004248 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004249 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004250 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004251
4252 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004253 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004254 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004255
4256 // Class description structures
4257
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004258 // struct _objc_ivar {
4259 // char *ivar_name;
4260 // char *ivar_type;
4261 // int ivar_offset;
4262 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004263 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264 Int8PtrTy,
4265 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004266 NULL);
4267 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4268
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004269 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004270 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004271 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004272 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004273
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004274 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004275 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004276 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004277 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004278
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004279 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004280 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004281 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004282 Int8PtrTy,
4283 PropertyListPtrTy,
4284 NULL);
4285 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004286 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004287
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004288 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004290 // struct _objc_class {
4291 // Class isa;
4292 // Class super_class;
4293 // char *name;
4294 // long version;
4295 // long info;
4296 // long instance_size;
4297 // struct _objc_ivar_list *ivars;
4298 // struct _objc_method_list *methods;
4299 // struct _objc_cache *cache;
4300 // struct _objc_protocol_list *protocols;
4301 // char *ivar_layout;
4302 // struct _objc_class_ext *ext;
4303 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004304 T = llvm::StructType::get(VMContext,
4305 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004306 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004307 Int8PtrTy,
4308 LongTy,
4309 LongTy,
4310 LongTy,
4311 IvarListPtrTy,
4312 MethodListPtrTy,
4313 CachePtrTy,
4314 ProtocolListPtrTy,
4315 Int8PtrTy,
4316 ClassExtensionPtrTy,
4317 NULL);
4318 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004319
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004320 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4321 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004322 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004324 // struct _objc_category {
4325 // char *category_name;
4326 // char *class_name;
4327 // struct _objc_method_list *instance_method;
4328 // struct _objc_method_list *class_method;
4329 // uint32_t size; // sizeof(struct _objc_category)
4330 // struct _objc_property_list *instance_properties;// category's @property
4331 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004332 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004333 Int8PtrTy,
4334 MethodListPtrTy,
4335 MethodListPtrTy,
4336 ProtocolListPtrTy,
4337 IntTy,
4338 PropertyListPtrTy,
4339 NULL);
4340 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4341
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004342 // Global metadata structures
4343
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004344 // struct _objc_symtab {
4345 // long sel_ref_cnt;
4346 // SEL *refs;
4347 // short cls_def_cnt;
4348 // short cat_def_cnt;
4349 // char *defs[cls_def_cnt + cat_def_cnt];
4350 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004351 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004352 SelectorPtrTy,
4353 ShortTy,
4354 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004355 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004356 NULL);
4357 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004358 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004359
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004360 // struct _objc_module {
4361 // long version;
4362 // long size; // sizeof(struct _objc_module)
4363 // char *name;
4364 // struct _objc_symtab* symtab;
4365 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004366 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004367 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004368 LongTy,
4369 Int8PtrTy,
4370 SymtabPtrTy,
4371 NULL);
4372 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004373
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374
Mike Stumpf5408fe2009-05-16 07:57:57 +00004375 // FIXME: This is the size of the setjmp buffer and should be target
4376 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004377 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004378
Anders Carlsson124526b2008-09-09 10:10:21 +00004379 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004380 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004381 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
4383 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004384 llvm::StructType::get(VMContext,
4385 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4386 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004387 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004388 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004389 ExceptionDataTy);
4390
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004391}
4392
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004393ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004394 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395 // struct _method_list_t {
4396 // uint32_t entsize; // sizeof(struct _objc_method)
4397 // uint32_t method_count;
4398 // struct _objc_method method_list[method_count];
4399 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004400 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004401 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004402 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004403 NULL);
4404 CGM.getModule().addTypeName("struct.__method_list_t",
4405 MethodListnfABITy);
4406 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004407 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004408
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004409 // struct _protocol_t {
4410 // id isa; // NULL
4411 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004412 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004413 // const struct method_list_t * const instance_methods;
4414 // const struct method_list_t * const class_methods;
4415 // const struct method_list_t *optionalInstanceMethods;
4416 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004417 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004418 // const uint32_t size; // sizeof(struct _protocol_t)
4419 // const uint32_t flags; // = 0
4420 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004421
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004422 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004423 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004424
Owen Anderson47a434f2009-08-05 23:18:46 +00004425 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004426 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004427 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004428 ProtocolListTyHolder),
4429 MethodListnfABIPtrTy,
4430 MethodListnfABIPtrTy,
4431 MethodListnfABIPtrTy,
4432 MethodListnfABIPtrTy,
4433 PropertyListPtrTy,
4434 IntTy,
4435 IntTy,
4436 NULL);
4437 CGM.getModule().addTypeName("struct._protocol_t",
4438 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004439
4440 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004441 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004442
Fariborz Jahanianda320092009-01-29 19:24:30 +00004443 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004444 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004445 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004446 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004447 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004448 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004449 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004450 NULL);
4451 CGM.getModule().addTypeName("struct._objc_protocol_list",
4452 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004453 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004454 ProtocolListnfABITy);
4455
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004456 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004457 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004459 // struct _ivar_t {
4460 // unsigned long int *offset; // pointer to ivar offset location
4461 // char *name;
4462 // char *type;
4463 // uint32_t alignment;
4464 // uint32_t size;
4465 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004466 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004467 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004468 Int8PtrTy,
4469 Int8PtrTy,
4470 IntTy,
4471 IntTy,
4472 NULL);
4473 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004475 // struct _ivar_list_t {
4476 // uint32 entsize; // sizeof(struct _ivar_t)
4477 // uint32 count;
4478 // struct _iver_t list[count];
4479 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004480 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004481 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004482 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004483 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004484 NULL);
4485 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004486
Owen Anderson96e0fc72009-07-29 22:16:19 +00004487 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004488
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004489 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004490 // uint32_t const flags;
4491 // uint32_t const instanceStart;
4492 // uint32_t const instanceSize;
4493 // uint32_t const reserved; // only when building for 64bit targets
4494 // const uint8_t * const ivarLayout;
4495 // const char *const name;
4496 // const struct _method_list_t * const baseMethods;
4497 // const struct _objc_protocol_list *const baseProtocols;
4498 // const struct _ivar_list_t *const ivars;
4499 // const uint8_t * const weakIvarLayout;
4500 // const struct _prop_list_t * const properties;
4501 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004502
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004503 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004504 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004505 IntTy,
4506 IntTy,
4507 Int8PtrTy,
4508 Int8PtrTy,
4509 MethodListnfABIPtrTy,
4510 ProtocolListnfABIPtrTy,
4511 IvarListnfABIPtrTy,
4512 Int8PtrTy,
4513 PropertyListPtrTy,
4514 NULL);
4515 CGM.getModule().addTypeName("struct._class_ro_t",
4516 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004517
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004518 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4519 std::vector<const llvm::Type*> Params;
4520 Params.push_back(ObjectPtrTy);
4521 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004522 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004523 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4524
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004525 // struct _class_t {
4526 // struct _class_t *isa;
4527 // struct _class_t * const superclass;
4528 // void *cache;
4529 // IMP *vtable;
4530 // struct class_ro_t *ro;
4531 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004532
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004533 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004534 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004535 llvm::StructType::get(VMContext,
4536 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004537 llvm::PointerType::getUnqual(ClassTyHolder),
4538 CachePtrTy,
4539 llvm::PointerType::getUnqual(ImpnfABITy),
4540 llvm::PointerType::getUnqual(ClassRonfABITy),
4541 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004542 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4543
4544 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545 ClassnfABITy);
4546
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004547 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004548 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004549
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004550 // struct _category_t {
4551 // const char * const name;
4552 // struct _class_t *const cls;
4553 // const struct _method_list_t * const instance_methods;
4554 // const struct _method_list_t * const class_methods;
4555 // const struct _protocol_list_t * const protocols;
4556 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004557 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004558 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004559 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004560 MethodListnfABIPtrTy,
4561 MethodListnfABIPtrTy,
4562 ProtocolListnfABIPtrTy,
4563 PropertyListPtrTy,
4564 NULL);
4565 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004567 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004568 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4569 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004570
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004571 // MessageRefTy - LLVM for:
4572 // struct _message_ref_t {
4573 // IMP messenger;
4574 // SEL name;
4575 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004576
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004577 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004578 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004579 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004580 SourceLocation(),
4581 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004582 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004583 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004584 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004585 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004586 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004588 MessageRefCTy = Ctx.getTagDeclType(RD);
4589 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4590 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004591
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004592 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004593 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004595 // SuperMessageRefTy - LLVM for:
4596 // struct _super_message_ref_t {
4597 // SUPER_IMP messenger;
4598 // SEL name;
4599 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004600 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004601 SelectorPtrTy,
4602 NULL);
4603 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004605 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004606 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4607
Daniel Dunbare588b992009-03-01 04:46:24 +00004608
4609 // struct objc_typeinfo {
4610 // const void** vtable; // objc_ehtype_vtable + 2
4611 // const char* name; // c++ typeinfo string
4612 // Class cls;
4613 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004614 EHTypeTy = llvm::StructType::get(VMContext,
4615 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004616 Int8PtrTy,
4617 ClassnfABIPtrTy,
4618 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004619 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004620 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004621}
4622
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004623llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004624 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004626 return NULL;
4627}
4628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629void CGObjCNonFragileABIMac::AddModuleClassList(const
4630 std::vector<llvm::GlobalValue*>
4631 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004632 const char *SymbolName,
4633 const char *SectionName) {
4634 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004635
Daniel Dunbar463b8762009-05-15 21:48:48 +00004636 if (!NumClasses)
4637 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638
Daniel Dunbar463b8762009-05-15 21:48:48 +00004639 std::vector<llvm::Constant*> Symbols(NumClasses);
4640 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004641 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004642 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004643 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004644 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004645 NumClasses),
4646 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004647
Daniel Dunbar463b8762009-05-15 21:48:48 +00004648 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004649 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004650 llvm::GlobalValue::InternalLinkage,
4651 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004652 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004653 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004654 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004655 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004656}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004657
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004658void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4659 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004660
Daniel Dunbar463b8762009-05-15 21:48:48 +00004661 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004662 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004664 "\01L_OBJC_LABEL_CLASS_$",
4665 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004666
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004667 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4668 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4669 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4670 continue;
4671 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004672 }
4673
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004674 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4675 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4676 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4677 continue;
4678 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4679 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004680
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004681 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004682 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4683 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004684
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004685 // Build list of all implemented category addresses in array
4686 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004687 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004688 "\01L_OBJC_LABEL_CATEGORY_$",
4689 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004690 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004691 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4692 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004693
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004694 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004695}
4696
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004697/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004698/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004699/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004700/// message dispatch call for all the rest.
4701///
4702bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004703 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4704 default:
4705 assert(0 && "Invalid dispatch method!");
4706 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004707 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004708 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004709 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004710 case CodeGenOptions::Mixed:
4711 break;
4712 }
4713
4714 // If so, see whether this selector is in the white-list of things which must
4715 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004716 if (NonLegacyDispatchMethods.empty()) {
4717 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4718 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4719 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4720 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4721 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4722 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4723 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4724 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4725 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4726 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004727
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004728 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4729 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4730 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4731 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4732 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4733 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4734 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4735 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004736 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004737 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004738 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4739 &CGM.getContext().Idents.get("objects"),
4740 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004741 };
4742 NonLegacyDispatchMethods.insert(
4743 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004744 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004745
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004746 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004747}
4748
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004749// Metadata flags
4750enum MetaDataDlags {
4751 CLS = 0x0,
4752 CLS_META = 0x1,
4753 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004754 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004755 CLS_EXCEPTION = 0x20
4756};
4757/// BuildClassRoTInitializer - generate meta-data for:
4758/// struct _class_ro_t {
4759/// uint32_t const flags;
4760/// uint32_t const instanceStart;
4761/// uint32_t const instanceSize;
4762/// uint32_t const reserved; // only when building for 64bit targets
4763/// const uint8_t * const ivarLayout;
4764/// const char *const name;
4765/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004766/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004767/// const struct _ivar_list_t *const ivars;
4768/// const uint8_t * const weakIvarLayout;
4769/// const struct _prop_list_t * const properties;
4770/// }
4771///
4772llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004773 unsigned flags,
4774 unsigned InstanceStart,
4775 unsigned InstanceSize,
4776 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004777 std::string ClassName = ID->getNameAsString();
4778 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004779 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4780 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4781 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004782 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004783 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4784 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004785 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004786 // const struct _method_list_t * const baseMethods;
4787 std::vector<llvm::Constant*> Methods;
4788 std::string MethodListName("\01l_OBJC_$_");
4789 if (flags & CLS_META) {
4790 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004791 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004792 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004793 // Class methods should always be defined.
4794 Methods.push_back(GetMethodConstant(*i));
4795 }
4796 } else {
4797 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004798 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004799 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004800 // Instance methods should always be defined.
4801 Methods.push_back(GetMethodConstant(*i));
4802 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004803 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004804 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004805 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004806
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004807 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4808 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004809
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004810 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4811 if (llvm::Constant *C = GetMethodConstant(MD))
4812 Methods.push_back(C);
4813 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4814 if (llvm::Constant *C = GetMethodConstant(MD))
4815 Methods.push_back(C);
4816 }
4817 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004818 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004819 Values[ 5] = EmitMethodList(MethodListName,
4820 "__DATA, __objc_const", Methods);
4821
Fariborz Jahanianda320092009-01-29 19:24:30 +00004822 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4823 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004824 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004825 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004826 OID->all_referenced_protocol_begin(),
4827 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004828
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004829 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004830 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004831 else
4832 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004833 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4834 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004835 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004836 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004837 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004838 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4839 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004840 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004841 Values);
4842 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004843 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4844 llvm::GlobalValue::InternalLinkage,
4845 Init,
4846 (flags & CLS_META) ?
4847 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4848 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004849 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004850 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004851 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004852 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004853
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004854}
4855
4856/// BuildClassMetaData - This routine defines that to-level meta-data
4857/// for the given ClassName for:
4858/// struct _class_t {
4859/// struct _class_t *isa;
4860/// struct _class_t * const superclass;
4861/// void *cache;
4862/// IMP *vtable;
4863/// struct class_ro_t *ro;
4864/// }
4865///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004866llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004867 std::string &ClassName,
4868 llvm::Constant *IsAGV,
4869 llvm::Constant *SuperClassGV,
4870 llvm::Constant *ClassRoGV,
4871 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004872 std::vector<llvm::Constant*> Values(5);
4873 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004874 Values[1] = SuperClassGV;
4875 if (!Values[1])
4876 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004877 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4878 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4879 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004880 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004881 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004882 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4883 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004884 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004885 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004886 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004887 if (HiddenVisibility)
4888 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004889 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004890}
4891
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004892bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004893CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004894 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004895}
4896
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004897void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004898 uint32_t &InstanceStart,
4899 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004900 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004901 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004902
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004903 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004904 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004905
4906 // If there are no fields, the start is the same as the end.
4907 if (!RL.getFieldCount())
4908 InstanceStart = InstanceSize;
4909 else
4910 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004911}
4912
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004913void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4914 std::string ClassName = ID->getNameAsString();
4915 if (!ObjCEmptyCacheVar) {
4916 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004917 CGM.getModule(),
4918 ObjCTypes.CacheTy,
4919 false,
4920 llvm::GlobalValue::ExternalLinkage,
4921 0,
4922 "_objc_empty_cache");
4923
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004924 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004925 CGM.getModule(),
4926 ObjCTypes.ImpnfABITy,
4927 false,
4928 llvm::GlobalValue::ExternalLinkage,
4929 0,
4930 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004931 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004932 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004933 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004934 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004935 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004936 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004937 uint32_t InstanceSize = InstanceStart;
4938 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004939 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4940 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004941
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004942 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004943
4944 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004945 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004946 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004947 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004948 if (ID->getNumIvarInitializers())
4949 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004950 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004951 // class is root
4952 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004953 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004954 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004955 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004956 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004957 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4958 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4959 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004960 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004961 if (Root->hasAttr<WeakImportAttr>())
4962 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004963 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004964 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004965 ObjCMetaClassName +
4966 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004967 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004968 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4969 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004970 }
4971 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4972 InstanceStart,
4973 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004974 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004976 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4977 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004978 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004979
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004980 // Metadata for the class
4981 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004982 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004983 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004984 if (ID->getNumIvarInitializers())
4985 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004986
Douglas Gregor68584ed2009-06-18 16:11:24 +00004987 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004988 flags |= CLS_EXCEPTION;
4989
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004990 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004991 flags |= CLS_ROOT;
4992 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004993 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004994 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004995 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004996 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004997 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004998 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4999 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005000 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005001 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005002 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005003 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005004 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005005 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005006
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005007 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005009 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5010 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005011 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005012
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005013 // Determine if this class is also "non-lazy".
5014 if (ImplementationIsNonLazy(ID))
5015 DefinedNonLazyClasses.push_back(ClassMD);
5016
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005017 // Force the definition of the EHType if necessary.
5018 if (flags & CLS_EXCEPTION)
5019 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005020}
5021
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005022/// GenerateProtocolRef - This routine is called to generate code for
5023/// a protocol reference expression; as in:
5024/// @code
5025/// @protocol(Proto1);
5026/// @endcode
5027/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5028/// which will hold address of the protocol meta-data.
5029///
5030llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005031 const ObjCProtocolDecl *PD) {
5032
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005033 // This routine is called for @protocol only. So, we must build definition
5034 // of protocol's meta-data (not a reference to it!)
5035 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036 llvm::Constant *Init =
5037 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5038 ObjCTypes.ExternalProtocolPtrTy);
5039
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005040 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005041 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005043 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5044 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005045 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005046 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005047 CGM.getModule(),
5048 Init->getType(), false,
5049 llvm::GlobalValue::WeakAnyLinkage,
5050 Init,
5051 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005052 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5053 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005054 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005055 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005056}
5057
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005058/// GenerateCategory - Build metadata for a category implementation.
5059/// struct _category_t {
5060/// const char * const name;
5061/// struct _class_t *const cls;
5062/// const struct _method_list_t * const instance_methods;
5063/// const struct _method_list_t * const class_methods;
5064/// const struct _protocol_list_t * const protocols;
5065/// const struct _prop_list_t * const properties;
5066/// }
5067///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005068void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005069 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005070 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005071 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5072 "_$_" + OCD->getNameAsString());
5073 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005074 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005076 std::vector<llvm::Constant*> Values(6);
5077 Values[0] = GetClassName(OCD->getIdentifier());
5078 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005079 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005080 if (Interface->hasAttr<WeakImportAttr>())
5081 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5082
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005083 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005084 std::vector<llvm::Constant*> Methods;
5085 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005086 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005087 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005088
5089 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005090 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005091 // Instance methods should always be defined.
5092 Methods.push_back(GetMethodConstant(*i));
5093 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005094
5095 Values[2] = EmitMethodList(MethodListName,
5096 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005097 Methods);
5098
5099 MethodListName = Prefix;
5100 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5101 OCD->getNameAsString();
5102 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005103 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005104 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005105 // Class methods should always be defined.
5106 Methods.push_back(GetMethodConstant(*i));
5107 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005108
5109 Values[3] = EmitMethodList(MethodListName,
5110 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005111 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005112 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005113 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005114 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005115 llvm::SmallString<256> ExtName;
5116 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5117 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005118 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005119 + Interface->getName() + "_$_"
5120 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005121 Category->protocol_begin(),
5122 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005123 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5124 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005125 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005126 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5127 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005128 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005129
5130 llvm::Constant *Init =
5131 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005132 Values);
5133 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005134 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005135 false,
5136 llvm::GlobalValue::InternalLinkage,
5137 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005138 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005139 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005140 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005141 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005142 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005143 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005144
5145 // Determine if this category is also "non-lazy".
5146 if (ImplementationIsNonLazy(OCD))
5147 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005148}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005149
5150/// GetMethodConstant - Return a struct objc_method constant for the
5151/// given method if it has been defined. The result is null if the
5152/// method has not been defined. The return value has type MethodPtrTy.
5153llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005154 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005155 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005156 if (!Fn)
5157 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005158
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005159 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005160 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005161 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005162 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005163 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005164 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005165 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005166}
5167
5168/// EmitMethodList - Build meta-data for method declarations
5169/// struct _method_list_t {
5170/// uint32_t entsize; // sizeof(struct _objc_method)
5171/// uint32_t method_count;
5172/// struct _objc_method method_list[method_count];
5173/// }
5174///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005175llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5176 const char *Section,
5177 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005178 // Return null for empty list.
5179 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005180 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005181
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005182 std::vector<llvm::Constant*> Values(3);
5183 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005184 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005185 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005186 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005187 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005188 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005189 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005190 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005191 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005193 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005194 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005195 llvm::GlobalValue::InternalLinkage,
5196 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005197 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005198 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005199 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005200 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005201 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005202 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005203 ObjCTypes.MethodListnfABIPtrTy);
5204}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005205
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005206/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5207/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005208llvm::GlobalVariable *
5209CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5210 const ObjCIvarDecl *Ivar) {
5211 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005212 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005213 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005214 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005215 CGM.getModule().getGlobalVariable(Name);
5216 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005217 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005218 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005219 false,
5220 llvm::GlobalValue::ExternalLinkage,
5221 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005222 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005223 return IvarOffsetGV;
5224}
5225
Daniel Dunbare83be122010-04-02 21:14:02 +00005226llvm::Constant *
5227CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5228 const ObjCIvarDecl *Ivar,
5229 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005230 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005231 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005232 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005233 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005234 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005235
Mike Stumpf5408fe2009-05-16 07:57:57 +00005236 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5237 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005238 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5239 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5240 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005241 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005242 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005243 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005244 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005245 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005246}
5247
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005248/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005249/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005250/// IvarListnfABIPtrTy.
5251/// struct _ivar_t {
5252/// unsigned long int *offset; // pointer to ivar offset location
5253/// char *name;
5254/// char *type;
5255/// uint32_t alignment;
5256/// uint32_t size;
5257/// }
5258/// struct _ivar_list_t {
5259/// uint32 entsize; // sizeof(struct _ivar_t)
5260/// uint32 count;
5261/// struct _iver_t list[count];
5262/// }
5263///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005264
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005265llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005266 const ObjCImplementationDecl *ID) {
5267
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005268 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005269
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005270 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5271 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005272
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005273 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005274
Daniel Dunbar91636d62009-04-20 00:33:43 +00005275 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005276 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005277 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005279 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5280 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005281 // Ignore unnamed bit-fields.
5282 if (!IVD->getDeclName())
5283 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005284 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005285 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005286 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5287 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005288 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005289 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005290 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005291 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005292 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005293 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005294 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005295 // NOTE. Size of a bitfield does not match gcc's, because of the
5296 // way bitfields are treated special in each. But I am told that
5297 // 'size' for bitfield ivars is ignored by the runtime so it does
5298 // not matter. If it matters, there is enough info to get the
5299 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005300 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005301 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005302 }
5303 // Return null for empty list.
5304 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005305 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005306 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005307 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005308 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5309 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005310 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005311 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005312 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005313 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005314 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5315 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005316 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005317 llvm::GlobalValue::InternalLinkage,
5318 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005319 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005320 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005321 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005322 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005323
Chris Lattnerad64e022009-07-17 23:57:13 +00005324 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005325 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005326}
5327
5328llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005329 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005330 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005331
Fariborz Jahanianda320092009-01-29 19:24:30 +00005332 if (!Entry) {
5333 // We use the initializer as a marker of whether this is a forward
5334 // reference or not. At module finalization we add the empty
5335 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005336 Entry =
5337 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5338 llvm::GlobalValue::ExternalLinkage,
5339 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005340 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005341 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005342 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343
Fariborz Jahanianda320092009-01-29 19:24:30 +00005344 return Entry;
5345}
5346
5347/// GetOrEmitProtocol - Generate the protocol meta-data:
5348/// @code
5349/// struct _protocol_t {
5350/// id isa; // NULL
5351/// const char * const protocol_name;
5352/// const struct _protocol_list_t * protocol_list; // super protocols
5353/// const struct method_list_t * const instance_methods;
5354/// const struct method_list_t * const class_methods;
5355/// const struct method_list_t *optionalInstanceMethods;
5356/// const struct method_list_t *optionalClassMethods;
5357/// const struct _prop_list_t * properties;
5358/// const uint32_t size; // sizeof(struct _protocol_t)
5359/// const uint32_t flags; // = 0
5360/// }
5361/// @endcode
5362///
5363
5364llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367
Fariborz Jahanianda320092009-01-29 19:24:30 +00005368 // Early exit if a defining object has already been generated.
5369 if (Entry && Entry->hasInitializer())
5370 return Entry;
5371
Fariborz Jahanianda320092009-01-29 19:24:30 +00005372 // Construct method lists.
5373 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5374 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005375 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005376 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005377 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005378 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5380 OptInstanceMethods.push_back(C);
5381 } else {
5382 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005383 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005385
5386 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005387 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005388 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005389 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005390 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5391 OptClassMethods.push_back(C);
5392 } else {
5393 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005394 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005395 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005396
Fariborz Jahanianda320092009-01-29 19:24:30 +00005397 std::vector<llvm::Constant*> Values(10);
5398 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005399 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005400 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005401 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5402 PD->protocol_begin(),
5403 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005405 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005406 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005407 "__DATA, __objc_const",
5408 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005410 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005411 "__DATA, __objc_const",
5412 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005413 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005414 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 "__DATA, __objc_const",
5416 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005418 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005419 "__DATA, __objc_const",
5420 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005421 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005423 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005424 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005425 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005426 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005427 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005428 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005429
Fariborz Jahanianda320092009-01-29 19:24:30 +00005430 if (Entry) {
5431 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005432 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005433 Entry->setInitializer(Init);
5434 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005436 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5437 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5438 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005439 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005440 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005441 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005442 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005443 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005444 CGM.AddUsedGlobal(Entry);
5445
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005446 // Use this protocol meta-data to build protocol list table in section
5447 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005448 llvm::GlobalVariable *PTGV =
5449 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5450 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5451 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005452 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005453 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005454 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005455 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005456 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 return Entry;
5458}
5459
5460/// EmitProtocolList - Generate protocol list meta-data:
5461/// @code
5462/// struct _protocol_list_t {
5463/// long protocol_count; // Note, this is 32/64 bit
5464/// struct _protocol_t[protocol_count];
5465/// }
5466/// @endcode
5467///
5468llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005469CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5470 ObjCProtocolDecl::protocol_iterator begin,
5471 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005472 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473
Fariborz Jahanianda320092009-01-29 19:24:30 +00005474 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005475 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005476 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477
Daniel Dunbar948e2582009-02-15 07:36:20 +00005478 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005479 llvm::SmallString<256> TmpName;
5480 Name.toVector(TmpName);
5481 llvm::GlobalVariable *GV =
5482 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005483 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005484 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005485
Daniel Dunbar948e2582009-02-15 07:36:20 +00005486 for (; begin != end; ++begin)
5487 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5488
Fariborz Jahanianda320092009-01-29 19:24:30 +00005489 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005490 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005491 ObjCTypes.ProtocolnfABIPtrTy));
5492
Fariborz Jahanianda320092009-01-29 19:24:30 +00005493 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005494 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005495 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005496 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005497 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005498 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 ProtocolRefs.size()),
5500 ProtocolRefs);
5501
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005502 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005503 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005504 llvm::GlobalValue::InternalLinkage,
5505 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005506 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005507 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005508 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005509 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005510 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005512 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005513}
5514
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005515/// GetMethodDescriptionConstant - This routine build following meta-data:
5516/// struct _objc_method {
5517/// SEL _cmd;
5518/// char *method_type;
5519/// char *_imp;
5520/// }
5521
5522llvm::Constant *
5523CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5524 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005525 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005526 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5527 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005528 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005529 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005530 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005531 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005532}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005533
5534/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5535/// This code gen. amounts to generating code for:
5536/// @code
5537/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5538/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005539///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005540LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005541 CodeGen::CodeGenFunction &CGF,
5542 QualType ObjectTy,
5543 llvm::Value *BaseValue,
5544 const ObjCIvarDecl *Ivar,
5545 unsigned CVRQualifiers) {
5546 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005547 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5548 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005549}
5550
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005551llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005552 CodeGen::CodeGenFunction &CGF,
5553 const ObjCInterfaceDecl *Interface,
5554 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005555 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005556}
5557
Fariborz Jahanian46551122009-02-04 00:22:57 +00005558CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005559 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005560 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005561 QualType ResultType,
5562 Selector Sel,
5563 llvm::Value *Receiver,
5564 QualType Arg0Ty,
5565 bool IsSuper,
5566 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005567 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5568 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005569 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005570 llvm::Value *Arg0 = Receiver;
5571 if (!IsSuper)
5572 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005574 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005575 // FIXME. This is too much work to get the ABI-specific result type needed to
5576 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005577 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005578 = Types.getFunctionInfo(ResultType, CallArgList(),
5579 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005580 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005581 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005582 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005583 if (IsSuper) {
5584 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5585 Name += "objc_msgSendSuper2_stret_fixup";
5586 } else {
5587 Fn = ObjCTypes.getMessageSendStretFixupFn();
5588 Name += "objc_msgSend_stret_fixup";
5589 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005590 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5591 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5592 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005593 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005594 if (IsSuper) {
5595 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5596 Name += "objc_msgSendSuper2_fixup";
5597 } else {
5598 Fn = ObjCTypes.getMessageSendFixupFn();
5599 Name += "objc_msgSend_fixup";
5600 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005601 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005602 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005603 Name += '_';
5604 std::string SelName(Sel.getAsString());
5605 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005606 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005607 if (SelName[i] == ':')
5608 SelName[i] = '_';
5609 Name += SelName;
5610 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5611 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005612 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005613 std::vector<llvm::Constant*> Values(2);
5614 Values[0] = Fn;
5615 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005616 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005617 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005618 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005619 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005620 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005621 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005622 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005623 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005624 }
5625 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005626
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005627 CallArgList ActualArgs;
5628 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005629 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005630 ObjCTypes.MessageRefCPtrTy));
5631 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005632 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005633 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005634 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5635 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005636 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005637 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005638 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005639 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005640}
5641
5642/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005643CodeGen::RValue
5644CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005645 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005646 QualType ResultType,
5647 Selector Sel,
5648 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005649 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005650 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005651 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005652 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005653 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5654 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005655 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005656 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005657 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005658 Receiver, CGF.getContext().getObjCIdType(),
5659 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005660}
5661
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005662llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005663CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005664 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5665
Daniel Dunbardfff2302009-03-02 05:18:14 +00005666 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005668 false, llvm::GlobalValue::ExternalLinkage,
5669 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005670 }
5671
5672 return GV;
5673}
5674
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5676 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005677 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005678
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005679 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005680 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005681 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005682 Entry =
5683 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005684 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005686 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005687 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005688 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005689 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005690 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005691 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005692 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005693
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005694 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005695}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005696
Daniel Dunbar11394522009-04-18 08:51:00 +00005697llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005698CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005699 const ObjCInterfaceDecl *ID) {
5700 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701
Daniel Dunbar11394522009-04-18 08:51:00 +00005702 if (!Entry) {
5703 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5704 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705 Entry =
5706 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005707 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005708 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005709 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005710 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005711 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005713 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005714 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005715 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005717 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005718}
5719
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005720/// EmitMetaClassRef - Return a Value * of the address of _class_t
5721/// meta-data
5722///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5724 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005725 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5726 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005727 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005728
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005729 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005730 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005731 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005732 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005733 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005735 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005736 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005737 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005738 ObjCTypes.ClassnfABIPtrTy));
5739
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005740 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005741 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005742
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005743 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005744}
5745
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005746/// GetClass - Return a reference to the class for the given interface
5747/// decl.
5748llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5749 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005750 if (ID->hasAttr<WeakImportAttr>()) {
5751 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5752 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5753 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5754 }
5755
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005756 return EmitClassRef(Builder, ID);
5757}
5758
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005759/// Generates a message send where the super is the receiver. This is
5760/// a message send to self with special delivery semantics indicating
5761/// which class's method should be called.
5762CodeGen::RValue
5763CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005764 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005765 QualType ResultType,
5766 Selector Sel,
5767 const ObjCInterfaceDecl *Class,
5768 bool isCategoryImpl,
5769 llvm::Value *Receiver,
5770 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005771 const CodeGen::CallArgList &CallArgs,
5772 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005773 // ...
5774 // Create and init a super structure; this is a (receiver, class)
5775 // pair we will pass to objc_msgSendSuper.
5776 llvm::Value *ObjCSuper =
5777 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005778
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005779 llvm::Value *ReceiverAsObject =
5780 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5781 CGF.Builder.CreateStore(ReceiverAsObject,
5782 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005783
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005784 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005785 llvm::Value *Target;
5786 if (IsClassMessage) {
5787 if (isCategoryImpl) {
5788 // Message sent to "super' in a class method defined in
5789 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005790 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005791 Target = CGF.Builder.CreateStructGEP(Target, 0);
5792 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005793 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005794 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005795 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005796 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005797
Mike Stumpf5408fe2009-05-16 07:57:57 +00005798 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5799 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005800 const llvm::Type *ClassTy =
5801 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5802 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5803 CGF.Builder.CreateStore(Target,
5804 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005805
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005806 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005807 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5808 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005809 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005810 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005811 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005812 ObjCSuper, ObjCTypes.SuperPtrCTy,
5813 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005814}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005815
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005816llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005817 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005818 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005819
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005820 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005821 llvm::Constant *Casted =
5822 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5823 ObjCTypes.SelectorPtrTy);
5824 Entry =
5825 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5826 llvm::GlobalValue::InternalLinkage,
5827 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005828 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005829 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005830 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005831
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005832 if (lval)
5833 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005834 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005835}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005836/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005837/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005838///
5839void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005840 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005841 llvm::Value *dst,
5842 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005843 const llvm::Type * SrcTy = src->getType();
5844 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005845 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005846 assert(Size <= 8 && "does not support size > 8");
5847 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5848 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005849 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5850 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005851 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5852 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005853 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5854 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005855 return;
5856}
5857
5858/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5859/// objc_assign_strongCast (id src, id *dst)
5860///
5861void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005862 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005863 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005864 const llvm::Type * SrcTy = src->getType();
5865 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005866 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005867 assert(Size <= 8 && "does not support size > 8");
5868 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005869 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005870 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5871 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005872 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5873 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005874 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005875 src, dst, "weakassign");
5876 return;
5877}
5878
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005879void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005880 CodeGen::CodeGenFunction &CGF,
5881 llvm::Value *DestPtr,
5882 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005883 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005884 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5885 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005886 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005887 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005888 return;
5889}
5890
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005891/// EmitObjCWeakRead - Code gen for loading value of a __weak
5892/// object: objc_read_weak (id *src)
5893///
5894llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005895 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005896 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005897 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005898 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5899 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005900 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005901 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005902 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005903 return read_weak;
5904}
5905
5906/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5907/// objc_assign_weak (id src, id *dst)
5908///
5909void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005910 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005911 const llvm::Type * SrcTy = src->getType();
5912 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005913 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005914 assert(Size <= 8 && "does not support size > 8");
5915 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5916 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005917 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5918 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005919 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5920 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005921 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005922 src, dst, "weakassign");
5923 return;
5924}
5925
5926/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5927/// objc_assign_global (id src, id *dst)
5928///
5929void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005930 llvm::Value *src, llvm::Value *dst,
5931 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005932 const llvm::Type * SrcTy = src->getType();
5933 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005934 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005935 assert(Size <= 8 && "does not support size > 8");
5936 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5937 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005938 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5939 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005940 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5941 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005942 if (!threadlocal)
5943 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5944 src, dst, "globalassign");
5945 else
5946 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5947 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005948 return;
5949}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005950
John McCall740e8072010-07-21 00:41:47 +00005951namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005952 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005953 llvm::Value *SyncExitFn;
5954 llvm::Value *SyncArg;
5955 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5956 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5957
5958 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5959 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5960 }
5961 };
5962}
5963
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005964void
John McCallf1549f62010-07-06 01:34:17 +00005965CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5966 const ObjCAtSynchronizedStmt &S) {
5967 // Evaluate the lock operand. This should dominate the cleanup.
5968 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005969
John McCallf1549f62010-07-06 01:34:17 +00005970 // Acquire the lock.
5971 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5972 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5973 ->setDoesNotThrow();
5974
5975 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005976 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5977 ObjCTypes.getSyncExitFn(),
5978 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005979
John McCallf1549f62010-07-06 01:34:17 +00005980 // Emit the body of the statement.
5981 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005982
John McCallf1549f62010-07-06 01:34:17 +00005983 // Pop the lock-release cleanup.
5984 CGF.PopCleanupBlock();
5985}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005986
John McCallf1549f62010-07-06 01:34:17 +00005987namespace {
5988 struct CatchHandler {
5989 const VarDecl *Variable;
5990 const Stmt *Body;
5991 llvm::BasicBlock *Block;
5992 llvm::Value *TypeInfo;
5993 };
John McCall8e3f8612010-07-13 22:12:14 +00005994
John McCall1f0fca52010-07-21 07:22:38 +00005995 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005996 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5997 MightThrow(MightThrow), Fn(Fn) {}
5998 bool MightThrow;
5999 llvm::Value *Fn;
6000
6001 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6002 if (!MightThrow) {
6003 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6004 return;
6005 }
6006
6007 CGF.EmitCallOrInvoke(Fn, 0, 0);
6008 }
6009 };
John McCallf1549f62010-07-06 01:34:17 +00006010}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006011
John McCall5a180392010-07-24 00:37:23 +00006012llvm::Constant *
6013CGObjCNonFragileABIMac::GetEHType(QualType T) {
6014 // There's a particular fixed type info for 'id'.
6015 if (T->isObjCIdType() ||
6016 T->isObjCQualifiedIdType()) {
6017 llvm::Constant *IDEHType =
6018 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6019 if (!IDEHType)
6020 IDEHType =
6021 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6022 false,
6023 llvm::GlobalValue::ExternalLinkage,
6024 0, "OBJC_EHTYPE_id");
6025 return IDEHType;
6026 }
6027
6028 // All other types should be Objective-C interface pointer types.
6029 const ObjCObjectPointerType *PT =
6030 T->getAs<ObjCObjectPointerType>();
6031 assert(PT && "Invalid @catch type.");
6032 const ObjCInterfaceType *IT = PT->getInterfaceType();
6033 assert(IT && "Invalid @catch type.");
6034 return GetInterfaceEHType(IT->getDecl(), false);
6035}
6036
John McCallf1549f62010-07-06 01:34:17 +00006037void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6038 const ObjCAtTryStmt &S) {
6039 // Jump destination for falling out of catch bodies.
6040 CodeGenFunction::JumpDest Cont;
6041 if (S.getNumCatchStmts())
6042 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006043
John McCallf1549f62010-07-06 01:34:17 +00006044 CodeGenFunction::FinallyInfo FinallyInfo;
6045 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6046 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6047 ObjCTypes.getObjCBeginCatchFn(),
6048 ObjCTypes.getObjCEndCatchFn(),
6049 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006050
John McCallf1549f62010-07-06 01:34:17 +00006051 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006052
John McCallf1549f62010-07-06 01:34:17 +00006053 // Enter the catch, if there is one.
6054 if (S.getNumCatchStmts()) {
6055 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6056 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006057 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006058
John McCallf1549f62010-07-06 01:34:17 +00006059 Handlers.push_back(CatchHandler());
6060 CatchHandler &Handler = Handlers.back();
6061 Handler.Variable = CatchDecl;
6062 Handler.Body = CatchStmt->getCatchBody();
6063 Handler.Block = CGF.createBasicBlock("catch");
6064
6065 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006066 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006067 Handler.TypeInfo = 0; // catch-all
6068 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006069 break;
6070 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006071
John McCall5a180392010-07-24 00:37:23 +00006072 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006073 }
John McCallf1549f62010-07-06 01:34:17 +00006074
6075 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6076 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6077 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006078 }
John McCallf1549f62010-07-06 01:34:17 +00006079
6080 // Emit the try body.
6081 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006082
John McCallf1549f62010-07-06 01:34:17 +00006083 // Leave the try.
6084 if (S.getNumCatchStmts())
6085 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006086
John McCallf1549f62010-07-06 01:34:17 +00006087 // Remember where we were.
6088 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006089
John McCallf1549f62010-07-06 01:34:17 +00006090 // Emit the handlers.
6091 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6092 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006093
John McCallf1549f62010-07-06 01:34:17 +00006094 CGF.EmitBlock(Handler.Block);
6095 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006096
John McCallf1549f62010-07-06 01:34:17 +00006097 // Enter the catch.
6098 llvm::CallInst *Exn =
6099 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6100 "exn.adjusted");
6101 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006102
John McCallf1549f62010-07-06 01:34:17 +00006103 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006104 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006105 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6106 EndCatchMightThrow,
6107 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006108
John McCallf1549f62010-07-06 01:34:17 +00006109 // Bind the catch parameter if it exists.
6110 if (const VarDecl *CatchParam = Handler.Variable) {
6111 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6112 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006113
John McCallf1549f62010-07-06 01:34:17 +00006114 CGF.EmitLocalBlockVarDecl(*CatchParam);
6115 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006116 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006117
John McCallf1549f62010-07-06 01:34:17 +00006118 CGF.ObjCEHValueStack.push_back(Exn);
6119 CGF.EmitStmt(Handler.Body);
6120 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006121
John McCallf1549f62010-07-06 01:34:17 +00006122 // Leave the earlier cleanup.
6123 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006124
John McCallf1549f62010-07-06 01:34:17 +00006125 CGF.EmitBranchThroughCleanup(Cont);
6126 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006127
John McCallf1549f62010-07-06 01:34:17 +00006128 // Go back to the try-statement fallthrough.
6129 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006130
John McCallf1549f62010-07-06 01:34:17 +00006131 // Pop out of the normal cleanup on the finally.
6132 if (S.getFinallyStmt())
6133 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006134
John McCallff8e1152010-07-23 21:56:41 +00006135 if (Cont.isValid())
6136 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006137}
6138
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006139/// EmitThrowStmt - Generate code for a throw statement.
6140void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6141 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006142 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006143 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006144 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006145 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006146 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006147 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006148 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006149 "Unexpected rethrow outside @catch block.");
6150 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006151 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006152 }
6153
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006154 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006155 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6156 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6157 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006158 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006159 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006160 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006161 } else {
6162 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6163 ->setDoesNotReturn();
6164 CGF.Builder.CreateUnreachable();
6165 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006166
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006167 // Clear the insertion point to indicate we are in unreachable code.
6168 CGF.Builder.ClearInsertionPoint();
6169}
Daniel Dunbare588b992009-03-01 04:46:24 +00006170
John McCall5a180392010-07-24 00:37:23 +00006171llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006172CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006173 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006174 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006175
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006176 // If we don't need a definition, return the entry if found or check
6177 // if we use an external reference.
6178 if (!ForDefinition) {
6179 if (Entry)
6180 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006181
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006182 // If this type (or a super class) has the __objc_exception__
6183 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006184 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006185 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006186 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006187 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006188 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006189 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006190 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006191 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006192
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006193 // Otherwise we need to either make a new entry or fill in the
6194 // initializer.
6195 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006196 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006197 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006198 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006199 CGM.getModule().getGlobalVariable(VTableName);
6200 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006201 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6202 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006203 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006204 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006205
Chris Lattner77b89b82010-06-27 07:15:29 +00006206 llvm::Value *VTableIdx =
6207 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006208
6209 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006210 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006211 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006212 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006213 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006214 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006215
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006216 if (Entry) {
6217 Entry->setInitializer(Init);
6218 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006219 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006220 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006221 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006222 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006223 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006224 }
6225
Daniel Dunbar04d40782009-04-14 06:00:08 +00006226 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006227 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006228 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6229 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006230
6231 if (ForDefinition) {
6232 Entry->setSection("__DATA,__objc_const");
6233 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6234 } else {
6235 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6236 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006237
6238 return Entry;
6239}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006240
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006241/* *** */
6242
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006243CodeGen::CGObjCRuntime *
6244CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006245 return new CGObjCMac(CGM);
6246}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006247
6248CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006249CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006250 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006251}