blob: f397c56b6deed4226b23fb8a17eeb81f1709eda3 [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 Dunbar56229f52010-04-05 16:20:33 +0000116 // We need to compute the bit offset for the bit-field, the offset is to the
117 // byte. Note, there is a subtle invariant here: we can only call this routine
118 // on non-synthesized ivars but we may be called for synthesized ivars.
119 // However, a synthesized ivar can never be a bit-field, so this is safe.
120 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
121 uint64_t BitFieldSize =
122 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000123
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000124 // Allocate a new CGBitFieldInfo object to describe this access.
125 //
126 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
127 // layout object. However, this is blocked on other cleanups to the
128 // Objective-C code, so for now we just live with allocating a bunch of these
129 // objects.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000130
Daniel Dunbarab970f92010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000133 AI.FieldIndex = 0;
134 AI.FieldByteOffset = 0;
135 AI.FieldBitStart = BitOffset;
136 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
137 AI.AccessAlignment = 0;
138 AI.TargetBitOffset = 0;
139 AI.TargetBitWidth = BitFieldSize;
140
Daniel Dunbar2df25692010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbar56229f52010-04-05 16:20:33 +0000145 // FIXME: We need to set a very conservative alignment on this, or make sure
146 // that the runtime is doing the right thing.
Daniel Dunbar76695ca2010-08-21 04:05:54 +0000147 return LValue::MakeBitfield(V, *Info,
148 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000149}
150
151///
152
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000153namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000154
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000156
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157// FIXME: We should find a nicer way to make the labels for metadata, string
158// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000159
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000160class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000161protected:
162 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000164private:
165 llvm::Constant *getMessageSendFn() const {
166 // id objc_msgSend (id, SEL, ...)
167 std::vector<const llvm::Type*> Params;
168 Params.push_back(ObjectPtrTy);
169 Params.push_back(SelectorPtrTy);
170 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000171 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
172 Params, true),
173 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000174 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000176 llvm::Constant *getMessageSendStretFn() const {
177 // id objc_msgSend_stret (id, SEL, ...)
178 std::vector<const llvm::Type*> Params;
179 Params.push_back(ObjectPtrTy);
180 Params.push_back(SelectorPtrTy);
181 return
Owen Anderson0032b272009-08-13 21:57:51 +0000182 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000183 Params, true),
184 "objc_msgSend_stret");
185
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000186 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000187
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000188 llvm::Constant *getMessageSendFpretFn() const {
189 // FIXME: This should be long double on x86_64?
190 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
191 std::vector<const llvm::Type*> Params;
192 Params.push_back(ObjectPtrTy);
193 Params.push_back(SelectorPtrTy);
194 return
Owen Anderson0032b272009-08-13 21:57:51 +0000195 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
196 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000197 Params,
198 true),
199 "objc_msgSend_fpret");
200
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000201 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000202
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000203 llvm::Constant *getMessageSendSuperFn() const {
204 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
205 const char *SuperName = "objc_msgSendSuper";
206 std::vector<const llvm::Type*> Params;
207 Params.push_back(SuperPtrTy);
208 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000209 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000210 Params, true),
211 SuperName);
212 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000213
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000214 llvm::Constant *getMessageSendSuperFn2() const {
215 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
216 const char *SuperName = "objc_msgSendSuper2";
217 std::vector<const llvm::Type*> Params;
218 Params.push_back(SuperPtrTy);
219 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000220 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000221 Params, true),
222 SuperName);
223 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000224
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000225 llvm::Constant *getMessageSendSuperStretFn() const {
226 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
227 // SEL op, ...)
228 std::vector<const llvm::Type*> Params;
229 Params.push_back(Int8PtrTy);
230 Params.push_back(SuperPtrTy);
231 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000232 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000233 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000234 Params, true),
235 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000236 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000237
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000238 llvm::Constant *getMessageSendSuperStretFn2() const {
239 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
240 // SEL op, ...)
241 std::vector<const llvm::Type*> Params;
242 Params.push_back(Int8PtrTy);
243 Params.push_back(SuperPtrTy);
244 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000245 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000246 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000247 Params, true),
248 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000251 llvm::Constant *getMessageSendSuperFpretFn() const {
252 // There is no objc_msgSendSuper_fpret? How can that work?
253 return getMessageSendSuperFn();
254 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000255
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000256 llvm::Constant *getMessageSendSuperFpretFn2() const {
257 // There is no objc_msgSendSuper_fpret? How can that work?
258 return getMessageSendSuperFn2();
259 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000260
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000261protected:
262 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000263
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000264public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000265 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000266 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000267
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000268 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
269 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000271 /// PtrObjectPtrTy - LLVM type for id *
272 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000273
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000274 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000275 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000276 /// ProtocolPtrTy - LLVM type for external protocol handles
277 /// (typeof(Protocol))
278 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000279
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000280 // SuperCTy - clang type for struct objc_super.
281 QualType SuperCTy;
282 // SuperPtrCTy - clang type for struct objc_super *.
283 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000284
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000285 /// SuperTy - LLVM type for struct objc_super.
286 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000287 /// SuperPtrTy - LLVM type for struct objc_super *.
288 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000289
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000290 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
291 /// in GCC parlance).
292 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000293
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000294 /// PropertyListTy - LLVM type for struct objc_property_list
295 /// (_prop_list_t in GCC parlance).
296 const llvm::StructType *PropertyListTy;
297 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
298 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000299
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000300 // MethodTy - LLVM type for struct objc_method.
301 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000302
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000303 /// CacheTy - LLVM type for struct objc_cache.
304 const llvm::Type *CacheTy;
305 /// CachePtrTy - LLVM type for struct objc_cache *.
306 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000307
Chris Lattner72db6c32009-04-22 02:44:54 +0000308 llvm::Constant *getGetPropertyFn() {
309 CodeGen::CodeGenTypes &Types = CGM.getTypes();
310 ASTContext &Ctx = CGM.getContext();
311 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000312 llvm::SmallVector<CanQualType,4> Params;
313 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
314 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000315 Params.push_back(IdType);
316 Params.push_back(SelType);
317 Params.push_back(Ctx.LongTy);
318 Params.push_back(Ctx.BoolTy);
319 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000320 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000321 FunctionType::ExtInfo()),
322 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000323 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
324 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000325
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 llvm::Constant *getSetPropertyFn() {
327 CodeGen::CodeGenTypes &Types = CGM.getTypes();
328 ASTContext &Ctx = CGM.getContext();
329 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000330 llvm::SmallVector<CanQualType,6> Params;
331 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
332 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000333 Params.push_back(IdType);
334 Params.push_back(SelType);
335 Params.push_back(Ctx.LongTy);
336 Params.push_back(IdType);
337 Params.push_back(Ctx.BoolTy);
338 Params.push_back(Ctx.BoolTy);
339 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000340 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000341 FunctionType::ExtInfo()),
342 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000343 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
344 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000345
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000346
347 llvm::Constant *getCopyStructFn() {
348 CodeGen::CodeGenTypes &Types = CGM.getTypes();
349 ASTContext &Ctx = CGM.getContext();
350 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
351 llvm::SmallVector<CanQualType,5> Params;
352 Params.push_back(Ctx.VoidPtrTy);
353 Params.push_back(Ctx.VoidPtrTy);
354 Params.push_back(Ctx.LongTy);
355 Params.push_back(Ctx.BoolTy);
356 Params.push_back(Ctx.BoolTy);
357 const llvm::FunctionType *FTy =
358 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
359 FunctionType::ExtInfo()),
360 false);
361 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
362 }
363
Chris Lattner72db6c32009-04-22 02:44:54 +0000364 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000365 CodeGen::CodeGenTypes &Types = CGM.getTypes();
366 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000367 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000368 llvm::SmallVector<CanQualType,1> Params;
369 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000370 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000371 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000372 FunctionType::ExtInfo()),
373 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000374 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
375 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000376
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000377 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000378 llvm::Constant *getGcReadWeakFn() {
379 // id objc_read_weak (id *)
380 std::vector<const llvm::Type*> Args;
381 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000383 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000384 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000385 }
386
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000387 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000388 llvm::Constant *getGcAssignWeakFn() {
389 // id objc_assign_weak (id, id *)
390 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
391 Args.push_back(ObjectPtrTy->getPointerTo());
392 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000393 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000394 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
395 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000396
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000397 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000398 llvm::Constant *getGcAssignGlobalFn() {
399 // id objc_assign_global(id, id *)
400 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
401 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000402 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000403 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000404 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
405 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000406
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000407 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
408 llvm::Constant *getGcAssignThreadLocalFn() {
409 // id objc_assign_threadlocal(id src, id * dest)
410 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
411 Args.push_back(ObjectPtrTy->getPointerTo());
412 llvm::FunctionType *FTy =
413 llvm::FunctionType::get(ObjectPtrTy, Args, false);
414 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
415 }
416
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000417 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000419 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000420 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
421 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000422 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000423 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000424 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
426 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000427
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000428 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
429 llvm::Constant *GcMemmoveCollectableFn() {
430 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
431 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
432 Args.push_back(Int8PtrTy);
433 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000434 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000435 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
436 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000437
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000438 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000439 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000440 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000441 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
442 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000443 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000444 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
446 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000447
448 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000449 llvm::Constant *getExceptionThrowFn() {
450 // void objc_exception_throw(id)
451 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
452 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000453 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000454 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000456
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000457 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
458 llvm::Constant *getExceptionRethrowFn() {
459 // void objc_exception_rethrow(void)
460 std::vector<const llvm::Type*> Args;
461 llvm::FunctionType *FTy =
462 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
463 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
464 }
465
Daniel Dunbar1c566672009-02-24 01:43:46 +0000466 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000467 llvm::Constant *getSyncEnterFn() {
468 // void objc_sync_enter (id)
469 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
470 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000471 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000472 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
473 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000474
Daniel Dunbar1c566672009-02-24 01:43:46 +0000475 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000476 llvm::Constant *getSyncExitFn() {
477 // void objc_sync_exit (id)
478 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
479 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000480 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000481 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
482 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000483
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000484 llvm::Constant *getSendFn(bool IsSuper) const {
485 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
486 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000487
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000488 llvm::Constant *getSendFn2(bool IsSuper) const {
489 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
490 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000491
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000492 llvm::Constant *getSendStretFn(bool IsSuper) const {
493 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
494 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000495
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000496 llvm::Constant *getSendStretFn2(bool IsSuper) const {
497 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
498 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000499
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000500 llvm::Constant *getSendFpretFn(bool IsSuper) const {
501 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
502 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000503
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000504 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
505 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
506 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000508 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
509 ~ObjCCommonTypesHelper(){}
510};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000511
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000512/// ObjCTypesHelper - Helper class that encapsulates lazy
513/// construction of varies types used during ObjC generation.
514class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000515public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000516 /// SymtabTy - LLVM type for struct objc_symtab.
517 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000518 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
519 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000520 /// ModuleTy - LLVM type for struct objc_module.
521 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000522
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000523 /// ProtocolTy - LLVM type for struct objc_protocol.
524 const llvm::StructType *ProtocolTy;
525 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
526 const llvm::Type *ProtocolPtrTy;
527 /// ProtocolExtensionTy - LLVM type for struct
528 /// objc_protocol_extension.
529 const llvm::StructType *ProtocolExtensionTy;
530 /// ProtocolExtensionTy - LLVM type for struct
531 /// objc_protocol_extension *.
532 const llvm::Type *ProtocolExtensionPtrTy;
533 /// MethodDescriptionTy - LLVM type for struct
534 /// objc_method_description.
535 const llvm::StructType *MethodDescriptionTy;
536 /// MethodDescriptionListTy - LLVM type for struct
537 /// objc_method_description_list.
538 const llvm::StructType *MethodDescriptionListTy;
539 /// MethodDescriptionListPtrTy - LLVM type for struct
540 /// objc_method_description_list *.
541 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000542 /// ProtocolListTy - LLVM type for struct objc_property_list.
543 const llvm::Type *ProtocolListTy;
544 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
545 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000546 /// CategoryTy - LLVM type for struct objc_category.
547 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000548 /// ClassTy - LLVM type for struct objc_class.
549 const llvm::StructType *ClassTy;
550 /// ClassPtrTy - LLVM type for struct objc_class *.
551 const llvm::Type *ClassPtrTy;
552 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
553 const llvm::StructType *ClassExtensionTy;
554 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
555 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000556 // IvarTy - LLVM type for struct objc_ivar.
557 const llvm::StructType *IvarTy;
558 /// IvarListTy - LLVM type for struct objc_ivar_list.
559 const llvm::Type *IvarListTy;
560 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
561 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000562 /// MethodListTy - LLVM type for struct objc_method_list.
563 const llvm::Type *MethodListTy;
564 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
565 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000566
Anders Carlsson124526b2008-09-09 10:10:21 +0000567 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
568 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000569
Anders Carlsson124526b2008-09-09 10:10:21 +0000570 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000571 llvm::Constant *getExceptionTryEnterFn() {
572 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000573 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000574 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000575 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 Params, false),
577 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000578 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000579
580 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 llvm::Constant *getExceptionTryExitFn() {
582 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000583 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000584 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000585 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000586 Params, false),
587 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000588 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000589
590 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000591 llvm::Constant *getExceptionExtractFn() {
592 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000593 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
594 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000595 Params, false),
596 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000597
Chris Lattner34b02a12009-04-22 02:26:14 +0000598 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000599
Anders Carlsson124526b2008-09-09 10:10:21 +0000600 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000601 llvm::Constant *getExceptionMatchFn() {
602 std::vector<const llvm::Type*> Params;
603 Params.push_back(ClassPtrTy);
604 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000606 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000607 Params, false),
608 "objc_exception_match");
609
Chris Lattner34b02a12009-04-22 02:26:14 +0000610 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Anders Carlsson124526b2008-09-09 10:10:21 +0000612 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000613 llvm::Constant *getSetJmpFn() {
614 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000615 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000616 return
Owen Anderson0032b272009-08-13 21:57:51 +0000617 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000618 Params, false),
619 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Chris Lattner34b02a12009-04-22 02:26:14 +0000621 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000622
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000623public:
624 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000625 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000626};
627
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000628/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000629/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000630class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000631public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000632
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000633 // MethodListnfABITy - LLVM for struct _method_list_t
634 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000635
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000636 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
637 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000639 // ProtocolnfABITy = LLVM for struct _protocol_t
640 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000641
Daniel Dunbar948e2582009-02-15 07:36:20 +0000642 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
643 const llvm::Type *ProtocolnfABIPtrTy;
644
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000645 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
646 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000647
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000648 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
649 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000650
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000651 // ClassnfABITy - LLVM for struct _class_t
652 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000654 // ClassnfABIPtrTy - LLVM for struct _class_t*
655 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000657 // IvarnfABITy - LLVM for struct _ivar_t
658 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000659
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000660 // IvarListnfABITy - LLVM for struct _ivar_list_t
661 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000663 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
664 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000666 // ClassRonfABITy - LLVM for struct _class_ro_t
667 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000669 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
670 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000671
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000672 // CategorynfABITy - LLVM for struct _category_t
673 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000675 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000676
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000677 // MessageRefTy - LLVM for:
678 // struct _message_ref_t {
679 // IMP messenger;
680 // SEL name;
681 // };
682 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000683 // MessageRefCTy - clang type for struct _message_ref_t
684 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000685
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000686 // MessageRefPtrTy - LLVM for struct _message_ref_t*
687 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000688 // MessageRefCPtrTy - clang type for struct _message_ref_t*
689 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000690
Fariborz Jahanianef163782009-02-05 01:13:09 +0000691 // MessengerTy - Type of the messenger (shown as IMP above)
692 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000693
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000694 // SuperMessageRefTy - LLVM for:
695 // struct _super_message_ref_t {
696 // SUPER_IMP messenger;
697 // SEL name;
698 // };
699 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000700
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000701 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
702 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000703
Chris Lattner1c02f862009-04-22 02:53:24 +0000704 llvm::Constant *getMessageSendFixupFn() {
705 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
706 std::vector<const llvm::Type*> Params;
707 Params.push_back(ObjectPtrTy);
708 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000710 Params, true),
711 "objc_msgSend_fixup");
712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000713
Chris Lattner1c02f862009-04-22 02:53:24 +0000714 llvm::Constant *getMessageSendFpretFixupFn() {
715 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
716 std::vector<const llvm::Type*> Params;
717 Params.push_back(ObjectPtrTy);
718 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000719 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000720 Params, true),
721 "objc_msgSend_fpret_fixup");
722 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000723
Chris Lattner1c02f862009-04-22 02:53:24 +0000724 llvm::Constant *getMessageSendStretFixupFn() {
725 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
726 std::vector<const llvm::Type*> Params;
727 Params.push_back(ObjectPtrTy);
728 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000729 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000730 Params, true),
731 "objc_msgSend_stret_fixup");
732 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000733
Chris Lattner1c02f862009-04-22 02:53:24 +0000734 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000735 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000736 // struct _super_message_ref_t*, ...)
737 std::vector<const llvm::Type*> Params;
738 Params.push_back(SuperPtrTy);
739 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000740 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000741 Params, true),
742 "objc_msgSendSuper2_fixup");
743 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000744
Chris Lattner1c02f862009-04-22 02:53:24 +0000745 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000746 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000747 // struct _super_message_ref_t*, ...)
748 std::vector<const llvm::Type*> Params;
749 Params.push_back(SuperPtrTy);
750 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000751 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000752 Params, true),
753 "objc_msgSendSuper2_stret_fixup");
754 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000755
Chris Lattner8a569112009-04-22 02:15:23 +0000756 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000757 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000758 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000759 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000760
Chris Lattner8a569112009-04-22 02:15:23 +0000761 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Chris Lattner8a569112009-04-22 02:15:23 +0000763 llvm::Constant *getObjCBeginCatchFn() {
764 std::vector<const llvm::Type*> Params;
765 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000766 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000767 Params, false),
768 "objc_begin_catch");
769 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000770
771 const llvm::StructType *EHTypeTy;
772 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000773
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000774 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
775 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000776};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000777
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000779public:
780 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000781 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000782 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000783 unsigned ivar_bytepos;
784 unsigned ivar_size;
785 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000786 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000787
788 // Allow sorting based on byte pos.
789 bool operator<(const GC_IVAR &b) const {
790 return ivar_bytepos < b.ivar_bytepos;
791 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000792 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000794 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000795 public:
796 unsigned skip;
797 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000798 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000799 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000800 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000802protected:
803 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000804 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000805 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000806 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000807
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000808 // gc ivar layout bitmap calculation helper caches.
809 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
810 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000811
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000812 /// LazySymbols - Symbols to generate a lazy reference for. See
813 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000814 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000816 /// DefinedSymbols - External symbols which are defined by this
817 /// module. The symbols in this list and LazySymbols are used to add
818 /// special linker symbols which ensure that Objective-C modules are
819 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000820 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000821
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000822 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000823 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000824
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000825 /// MethodVarNames - uniqued method variable names.
826 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000827
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000828 /// DefinedCategoryNames - list of category names in form Class_Category.
829 llvm::SetVector<std::string> DefinedCategoryNames;
830
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000831 /// MethodVarTypes - uniqued method type signatures. We have to use
832 /// a StringMap here because have no other unique reference.
833 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000834
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000835 /// MethodDefinitions - map of methods which have been defined in
836 /// this translation unit.
837 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000839 /// PropertyNames - uniqued method variable names.
840 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000842 /// ClassReferences - uniqued class references.
843 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000845 /// SelectorReferences - uniqued selector references.
846 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000848 /// Protocols - Protocols for which an objc_protocol structure has
849 /// been emitted. Forward declarations are handled by creating an
850 /// empty structure whose initializer is filled in when/if defined.
851 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000853 /// DefinedProtocols - Protocols which have actually been
854 /// defined. We should not need this, see FIXME in GenerateProtocol.
855 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000856
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000857 /// DefinedClasses - List of defined classes.
858 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000859
860 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
861 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000863 /// DefinedCategories - List of defined categories.
864 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000865
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000866 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
867 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000869 /// GetNameForMethod - Return a name for the given method.
870 /// \param[out] NameOut - The return value.
871 void GetNameForMethod(const ObjCMethodDecl *OMD,
872 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000873 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000874
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000875 /// GetMethodVarName - Return a unique constant for the given
876 /// selector's name. The return value has type char *.
877 llvm::Constant *GetMethodVarName(Selector Sel);
878 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000879
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000880 /// GetMethodVarType - Return a unique constant for the given
881 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000882
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000883 // FIXME: This is a horrible name.
884 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000885 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000887 /// GetPropertyName - Return a unique constant for the given
888 /// name. The return value has type char *.
889 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000891 // FIXME: This can be dropped once string functions are unified.
892 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
893 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000894
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000895 /// GetClassName - Return a unique constant for the given selector's
896 /// name. The return value has type char *.
897 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000899 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
900
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000901 /// BuildIvarLayout - Builds ivar layout bitmap for the class
902 /// implementation for the __strong or __weak case.
903 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000904 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
905 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000906
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000907 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000909 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000910 unsigned int BytePos, bool ForStrongLayout,
911 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000912 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000913 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000914 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000915 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000916 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000917 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000918
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000919 /// GetIvarLayoutName - Returns a unique constant for the given
920 /// ivar layout bitmap.
921 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
922 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000924 /// EmitPropertyList - Emit the given property list. The return
925 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000926 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000928 const ObjCContainerDecl *OCD,
929 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000930
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000931 /// PushProtocolProperties - Push protocol's property on the input stack.
932 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
933 std::vector<llvm::Constant*> &Properties,
934 const Decl *Container,
935 const ObjCProtocolDecl *PROTO,
936 const ObjCCommonTypesHelper &ObjCTypes);
937
Fariborz Jahanianda320092009-01-29 19:24:30 +0000938 /// GetProtocolRef - Return a reference to the internal protocol
939 /// description, creating an empty one if it has not been
940 /// defined. The return value has type ProtocolPtrTy.
941 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000942
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000943 /// CreateMetadataVar - Create a global variable with internal
944 /// linkage for use by the Objective-C runtime.
945 ///
946 /// This is a convenience wrapper which not only creates the
947 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000948 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000949 ///
950 /// \param Name - The variable name.
951 /// \param Init - The variable initializer; this is also used to
952 /// define the type of the variable.
953 /// \param Section - The section the variable should go into, or 0.
954 /// \param Align - The alignment for the variable, or 0.
955 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000956 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000957 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000958 llvm::Constant *Init,
959 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000960 unsigned Align,
961 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000962
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000963 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000964 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000965 QualType ResultType,
966 llvm::Value *Sel,
967 llvm::Value *Arg0,
968 QualType Arg0Ty,
969 bool IsSuper,
970 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000971 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000972 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000973
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000974 /// EmitImageInfo - Emit the image info marker used to encode some module
975 /// level information.
976 void EmitImageInfo();
977
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000978public:
Owen Anderson69243822009-07-13 04:10:07 +0000979 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000980 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000981
David Chisnall0d13f6f2010-01-23 02:40:42 +0000982 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000983
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000984 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
985 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000986
Fariborz Jahanianda320092009-01-29 19:24:30 +0000987 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000988
Fariborz Jahanianda320092009-01-29 19:24:30 +0000989 /// GetOrEmitProtocol - Get the protocol object for the given
990 /// declaration, emitting it if necessary. The return value has type
991 /// ProtocolPtrTy.
992 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000993
Fariborz Jahanianda320092009-01-29 19:24:30 +0000994 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
995 /// object for the given declaration, emitting it if needed. These
996 /// forward references will be filled in with empty bodies if no
997 /// definition is seen. The return value has type ProtocolPtrTy.
998 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000999 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1000 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1001
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001002};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001003
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001004class CGObjCMac : public CGObjCCommonMac {
1005private:
1006 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001007
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001008 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001009 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001010 void EmitModuleInfo();
1011
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012 /// EmitModuleSymols - Emit module symbols, the list of defined
1013 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001014 llvm::Constant *EmitModuleSymbols();
1015
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001016 /// FinishModule - Write out global data structures at the end of
1017 /// processing a translation unit.
1018 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001020 /// EmitClassExtension - Generate the class extension structure used
1021 /// to store the weak ivar layout and properties. The return value
1022 /// has type ClassExtensionPtrTy.
1023 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1024
1025 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1026 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001028 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001029
1030 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1031 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001032
1033 /// EmitIvarList - Emit the ivar list for the given
1034 /// implementation. If ForClass is true the list of class ivars
1035 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1036 /// interface ivars will be emitted. The return value has type
1037 /// IvarListPtrTy.
1038 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001039 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001040
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001041 /// EmitMetaClass - Emit a forward reference to the class structure
1042 /// for the metaclass of the given interface. The return value has
1043 /// type ClassPtrTy.
1044 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1045
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001046 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001047 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001048 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1049 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001050 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001051
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001052 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001053
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001054 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001055
1056 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001057 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001058 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001059 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001060 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001061
1062 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001063 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001064 /// - TypeName: The name for the type containing the methods.
1065 /// - IsProtocol: True iff these methods are for a protocol.
1066 /// - ClassMethds: True iff these are class methods.
1067 /// - Required: When true, only "required" methods are
1068 /// listed. Similarly, when false only "optional" methods are
1069 /// listed. For classes this should always be true.
1070 /// - begin, end: The method list to output.
1071 ///
1072 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001073 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001074 const char *Section,
1075 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001076
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001077 /// GetOrEmitProtocol - Get the protocol object for the given
1078 /// declaration, emitting it if necessary. The return value has type
1079 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001080 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001081
1082 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1083 /// object for the given declaration, emitting it if needed. These
1084 /// forward references will be filled in with empty bodies if no
1085 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001086 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001087
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001088 /// EmitProtocolExtension - Generate the protocol extension
1089 /// structure used to store optional instance and class methods, and
1090 /// protocol properties. The return value has type
1091 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001092 llvm::Constant *
1093 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1094 const ConstantVector &OptInstanceMethods,
1095 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001096
1097 /// EmitProtocolList - Generate the list of referenced
1098 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001099 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001100 ObjCProtocolDecl::protocol_iterator begin,
1101 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001102
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001103 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1104 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001105 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1106 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001107
1108public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001109 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001110
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001111 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001113 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001114 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001115 QualType ResultType,
1116 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001117 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001118 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001119 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001120 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001121
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001122 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001123 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001124 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001125 QualType ResultType,
1126 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001127 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001128 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001129 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001130 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001131 const CallArgList &CallArgs,
1132 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001133
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001134 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001135 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001136
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001137 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1138 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001139
1140 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1141 /// untyped one.
1142 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1143 const ObjCMethodDecl *Method);
1144
John McCall5a180392010-07-24 00:37:23 +00001145 virtual llvm::Constant *GetEHType(QualType T);
1146
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001147 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001148
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001149 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001150
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001151 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001152 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001153
Chris Lattner74391b42009-03-22 21:03:39 +00001154 virtual llvm::Constant *GetPropertyGetFunction();
1155 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001156 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001157 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001158
John McCallf1549f62010-07-06 01:34:17 +00001159 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1160 const ObjCAtTryStmt &S);
1161 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1162 const ObjCAtSynchronizedStmt &S);
1163 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001164 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1165 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001166 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001167 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001168 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001169 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001170 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001171 llvm::Value *src, llvm::Value *dest,
1172 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001173 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001174 llvm::Value *src, llvm::Value *dest,
1175 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001176 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1177 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001178 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1179 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001180 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001182 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1183 QualType ObjectTy,
1184 llvm::Value *BaseValue,
1185 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001186 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001187 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001188 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001189 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001190};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001191
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001192class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001193private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001194 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001195 llvm::GlobalVariable* ObjCEmptyCacheVar;
1196 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197
Daniel Dunbar11394522009-04-18 08:51:00 +00001198 /// SuperClassReferences - uniqued super class references.
1199 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001201 /// MetaClassReferences - uniqued meta class references.
1202 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001203
1204 /// EHTypeReferences - uniqued class ehtype references.
1205 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001206
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001207 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001208 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001209 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001210
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001211 /// DefinedMetaClasses - List of defined meta-classes.
1212 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1213
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001214 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001215 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001216 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001217
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001218 /// FinishNonFragileABIModule - Write out global data structures at the end of
1219 /// processing a translation unit.
1220 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001221
Daniel Dunbar463b8762009-05-15 21:48:48 +00001222 /// AddModuleClassList - Add the given list of class pointers to the
1223 /// module with the provided symbol and section names.
1224 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1225 const char *SymbolName,
1226 const char *SectionName);
1227
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001228 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1229 unsigned InstanceStart,
1230 unsigned InstanceSize,
1231 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001232 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001233 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001234 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001235 llvm::Constant *ClassRoGV,
1236 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001237
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001238 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001239
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001240 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001242 /// EmitMethodList - Emit the method list for the given
1243 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001244 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001245 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001246 const ConstantVector &Methods);
1247 /// EmitIvarList - Emit the ivar list for the given
1248 /// implementation. If ForClass is true the list of class ivars
1249 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1250 /// interface ivars will be emitted. The return value has type
1251 /// IvarListnfABIPtrTy.
1252 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001253
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001254 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001255 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001256 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
Fariborz Jahanianda320092009-01-29 19:24:30 +00001258 /// GetOrEmitProtocol - Get the protocol object for the given
1259 /// declaration, emitting it if necessary. The return value has type
1260 /// ProtocolPtrTy.
1261 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001262
Fariborz Jahanianda320092009-01-29 19:24:30 +00001263 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1264 /// object for the given declaration, emitting it if needed. These
1265 /// forward references will be filled in with empty bodies if no
1266 /// definition is seen. The return value has type ProtocolPtrTy.
1267 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Fariborz Jahanianda320092009-01-29 19:24:30 +00001269 /// EmitProtocolList - Generate the list of referenced
1270 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001271 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001272 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001273 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274
Fariborz Jahanian46551122009-02-04 00:22:57 +00001275 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001276 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001277 QualType ResultType,
1278 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001279 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001280 QualType Arg0Ty,
1281 bool IsSuper,
1282 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001283
1284 /// GetClassGlobal - Return the global variable for the Objective-C
1285 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001286 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001288 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001289 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001290 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001291 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
Daniel Dunbar11394522009-04-18 08:51:00 +00001293 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1294 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001295 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1296 const ObjCInterfaceDecl *ID);
1297
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001298 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1299 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001300 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001301 const ObjCInterfaceDecl *ID);
1302
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001303 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1304 /// the given ivar.
1305 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001306 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001307 const ObjCInterfaceDecl *ID,
1308 const ObjCIvarDecl *Ivar);
1309
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001310 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1311 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001312 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1313 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001314
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001315 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001316 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001317 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001318 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001319
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001320 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001321 return "OBJC_METACLASS_$_";
1322 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001323
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001324 const char *getClassSymbolPrefix() const {
1325 return "OBJC_CLASS_$_";
1326 }
1327
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001328 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001329 uint32_t &InstanceStart,
1330 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001331
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001332 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001333 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001334 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1335 return CGM.getContext().Selectors.getSelector(0, &II);
1336 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001337
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001338 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001339 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1340 return CGM.getContext().Selectors.getSelector(1, &II);
1341 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001342
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001343 /// ImplementationIsNonLazy - Check whether the given category or
1344 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001345 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001346
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001347public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001348 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 // FIXME. All stubs for now!
1350 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001351
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001352 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001353 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001354 QualType ResultType,
1355 Selector Sel,
1356 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001357 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001358 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001359 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001360
1361 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001362 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001363 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001364 QualType ResultType,
1365 Selector Sel,
1366 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001367 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001368 llvm::Value *Receiver,
1369 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001370 const CallArgList &CallArgs,
1371 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001372
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001373 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001374 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001375
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001376 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1377 bool lvalue = false)
1378 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001379
1380 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1381 /// untyped one.
1382 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1383 const ObjCMethodDecl *Method)
1384 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001385
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001386 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001387
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001388 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001389 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001390 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001391
John McCall5a180392010-07-24 00:37:23 +00001392 virtual llvm::Constant *GetEHType(QualType T);
1393
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001394 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001395 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001396 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397 virtual llvm::Constant *GetPropertySetFunction() {
1398 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001399 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001400
1401 virtual llvm::Constant *GetCopyStructFunction() {
1402 return ObjCTypes.getCopyStructFn();
1403 }
1404
Chris Lattner74391b42009-03-22 21:03:39 +00001405 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001406 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001407 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
John McCallf1549f62010-07-06 01:34:17 +00001409 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1410 const ObjCAtTryStmt &S);
1411 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1412 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001413 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001414 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001415 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001416 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001417 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001418 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001419 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001420 llvm::Value *src, llvm::Value *dest,
1421 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001423 llvm::Value *src, llvm::Value *dest,
1424 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001426 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001427 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1428 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001429 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001430 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1431 QualType ObjectTy,
1432 llvm::Value *BaseValue,
1433 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001434 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001435 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001436 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001437 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001438};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001439
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001440} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001441
1442/* *** Helper Functions *** */
1443
1444/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001445static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001446 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001447 unsigned idx0,
1448 unsigned idx1) {
1449 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001450 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1451 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001452 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001453 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001454}
1455
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001456/// hasObjCExceptionAttribute - Return true if this class or any super
1457/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001458static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001459 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001460 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001461 return true;
1462 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001463 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001464 return false;
1465}
1466
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001467/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001468
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001469CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001470 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001471 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001472 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001473}
1474
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001475/// GetClass - Return a reference to the class for the given interface
1476/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001477llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001478 const ObjCInterfaceDecl *ID) {
1479 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001480}
1481
1482/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001483llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1484 bool lval) {
1485 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001486}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001487llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001488 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001489 return EmitSelector(Builder, Method->getSelector());
1490}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001491
John McCall5a180392010-07-24 00:37:23 +00001492llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1493 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1494 return 0;
1495}
1496
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001497/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001498/*
1499 struct __builtin_CFString {
1500 const int *isa; // point to __CFConstantStringClassReference
1501 int flags;
1502 const char *str;
1503 long length;
1504 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001505*/
1506
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001507/// or Generate a constant NSString object.
1508/*
1509 struct __builtin_NSString {
1510 const int *isa; // point to __NSConstantStringClassReference
1511 const char *str;
1512 unsigned int length;
1513 };
1514*/
1515
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001516llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001517 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001518 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1519 CGM.GetAddrOfConstantCFString(SL) :
1520 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001521}
1522
1523/// Generates a message send where the super is the receiver. This is
1524/// a message send to self with special delivery semantics indicating
1525/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001526CodeGen::RValue
1527CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001528 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001529 QualType ResultType,
1530 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001531 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001532 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001533 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001534 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001535 const CodeGen::CallArgList &CallArgs,
1536 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001537 // Create and init a super structure; this is a (receiver, class)
1538 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001539 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001540 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001541 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001542 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001543 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001544 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001545
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001546 // If this is a class message the metaclass is passed as the target.
1547 llvm::Value *Target;
1548 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001549 if (isCategoryImpl) {
1550 // Message sent to 'super' in a class method defined in a category
1551 // implementation requires an odd treatment.
1552 // If we are in a class method, we must retrieve the
1553 // _metaclass_ for the current class, pointed at by
1554 // the class's "isa" pointer. The following assumes that
1555 // isa" is the first ivar in a class (which it must be).
1556 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1557 Target = CGF.Builder.CreateStructGEP(Target, 0);
1558 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001559 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001560 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1561 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1562 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1563 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001564 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001565 }
1566 else if (isCategoryImpl)
1567 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1568 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001569 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1570 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1571 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001572 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001573 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1574 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001575 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001576 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001577 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001578 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001579 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001580 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001581 EmitSelector(CGF.Builder, Sel),
1582 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001583 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001584}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001585
1586/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001587CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001588 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001589 QualType ResultType,
1590 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001591 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001592 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001593 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001594 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001595 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001596 EmitSelector(CGF.Builder, Sel),
1597 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001598 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001599}
1600
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001601CodeGen::RValue
1602CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001603 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001604 QualType ResultType,
1605 llvm::Value *Sel,
1606 llvm::Value *Arg0,
1607 QualType Arg0Ty,
1608 bool IsSuper,
1609 const CallArgList &CallArgs,
1610 const ObjCMethodDecl *Method,
1611 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001612 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001613 if (!IsSuper)
1614 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001615 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001616 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001617 CGF.getContext().getObjCSelType()));
1618 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001619
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001620 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001621 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001622 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001623 const llvm::FunctionType *FTy =
1624 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001625
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001626 if (Method)
1627 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1628 CGM.getContext().getCanonicalType(ResultType) &&
1629 "Result type mismatch!");
1630
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001631 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001632 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001633 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001634 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001635 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1636 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1637 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001638 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001639 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001640 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001641 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001642 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001643 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001644}
1645
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001646static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1647 if (FQT.isObjCGCStrong())
1648 return Qualifiers::Strong;
1649
1650 if (FQT.isObjCGCWeak())
1651 return Qualifiers::Weak;
1652
1653 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1654 return Qualifiers::Strong;
1655
1656 if (const PointerType *PT = FQT->getAs<PointerType>())
1657 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1658
1659 return Qualifiers::GCNone;
1660}
1661
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001662llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001663 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1664 llvm::Constant *NullPtr =
1665 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian5af12352010-08-05 15:52:12 +00001666 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
1667 DeclRefs.empty())
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001668 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001669 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001670 SkipIvars.clear();
1671 IvarsInfo.clear();
1672 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1673 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1674
1675 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1676 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1677 const ValueDecl *VD = BDRE->getDecl();
1678 CharUnits Offset = CGF.BlockDecls[VD];
1679 uint64_t FieldOffset = Offset.getQuantity();
1680 QualType Ty = VD->getType();
1681 assert(!Ty->isArrayType() &&
1682 "Array block variable should have been caught");
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001683 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001684 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1685 FieldOffset,
1686 true,
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001687 hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001688 continue;
1689 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001690
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001691 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1692 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1693 // __block variables are passed by their descriptior address. So, size
1694 // must reflect this.
1695 if (BDRE->isByRef())
1696 FieldSize = WordSizeInBits;
1697 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1698 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1699 FieldSize / WordSizeInBits));
1700 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1701 SkipIvars.push_back(GC_IVAR(FieldOffset,
1702 FieldSize / ByteSizeInBits));
1703 }
1704
1705 if (IvarsInfo.empty())
1706 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001707 // Sort on byte position in case we encounterred a union nested in
1708 // block variable type's aggregate type.
1709 if (hasUnion && !IvarsInfo.empty())
1710 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1711 if (hasUnion && !SkipIvars.empty())
1712 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001713
1714 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001715 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001716 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1717 printf("\n block variable layout for block: ");
1718 const unsigned char *s = (unsigned char*)BitMap.c_str();
1719 for (unsigned i = 0; i < BitMap.size(); i++)
1720 if (!(s[i] & 0xf0))
1721 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1722 else
1723 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1724 printf("\n");
1725 }
1726
1727 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001728}
1729
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001730llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001731 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001732 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001733 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001734 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1735
Owen Anderson3c4972d2009-07-29 18:54:39 +00001736 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001737 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001738}
1739
Fariborz Jahanianda320092009-01-29 19:24:30 +00001740void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001741 // FIXME: We shouldn't need this, the protocol decl should contain enough
1742 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001743 DefinedProtocols.insert(PD->getIdentifier());
1744
1745 // If we have generated a forward reference to this protocol, emit
1746 // it now. Otherwise do nothing, the protocol objects are lazily
1747 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001748 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001749 GetOrEmitProtocol(PD);
1750}
1751
Fariborz Jahanianda320092009-01-29 19:24:30 +00001752llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001753 if (DefinedProtocols.count(PD->getIdentifier()))
1754 return GetOrEmitProtocol(PD);
1755 return GetOrEmitProtocolRef(PD);
1756}
1757
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001758/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001759// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1760struct _objc_protocol {
1761struct _objc_protocol_extension *isa;
1762char *protocol_name;
1763struct _objc_protocol_list *protocol_list;
1764struct _objc__method_prototype_list *instance_methods;
1765struct _objc__method_prototype_list *class_methods
1766};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001767
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001768See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001769*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001770llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1771 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1772
1773 // Early exit if a defining object has already been generated.
1774 if (Entry && Entry->hasInitializer())
1775 return Entry;
1776
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001777 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001778 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001779 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1780
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001781 // Construct method lists.
1782 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1783 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001784 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001785 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001786 ObjCMethodDecl *MD = *i;
1787 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1788 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1789 OptInstanceMethods.push_back(C);
1790 } else {
1791 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001792 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001793 }
1794
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001795 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001796 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001797 ObjCMethodDecl *MD = *i;
1798 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1799 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1800 OptClassMethods.push_back(C);
1801 } else {
1802 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001803 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001804 }
1805
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001806 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001807 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001808 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001809 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001810 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001811 PD->protocol_begin(),
1812 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001814 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001815 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1816 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001817 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001818 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001819 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1820 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001821 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001823
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001824 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001825 // Already created, fix the linkage and update the initializer.
1826 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827 Entry->setInitializer(Init);
1828 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001829 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001830 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001831 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001832 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001833 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001834 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835 // FIXME: Is this necessary? Why only for protocol?
1836 Entry->setAlignment(4);
1837 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001838 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001839
1840 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001841}
1842
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001843llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001844 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1845
1846 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001847 // We use the initializer as a marker of whether this is a forward
1848 // reference or not. At module finalization we add the empty
1849 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001850 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001851 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001852 llvm::GlobalValue::ExternalLinkage,
1853 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001854 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001855 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856 // FIXME: Is this necessary? Why only for protocol?
1857 Entry->setAlignment(4);
1858 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 return Entry;
1861}
1862
1863/*
1864 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001865 uint32_t size;
1866 struct objc_method_description_list *optional_instance_methods;
1867 struct objc_method_description_list *optional_class_methods;
1868 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869 };
1870*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001871llvm::Constant *
1872CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1873 const ConstantVector &OptInstanceMethods,
1874 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001875 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001876 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001877 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001878 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001879 Values[1] =
1880 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001881 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001882 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1883 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001884 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001885 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001886 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1887 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001888 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001889 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001890
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001891 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001892 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001893 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001894 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001895
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001896 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001897 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001898
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001899 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001900 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001901 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001902 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001903}
1904
1905/*
1906 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 struct objc_protocol_list *next;
1908 long count;
1909 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001910 };
1911*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001912llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001913CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001914 ObjCProtocolDecl::protocol_iterator begin,
1915 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001916 std::vector<llvm::Constant*> ProtocolRefs;
1917
Daniel Dunbardbc933702008-08-21 21:57:41 +00001918 for (; begin != end; ++begin)
1919 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001920
1921 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001922 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001923 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001924
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001925 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001926 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001927
1928 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001929 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001930 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001931 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001932 ProtocolRefs.size() - 1);
1933 Values[2] =
1934 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1935 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001936 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001937
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001938 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001939 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001940 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001941 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001942 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001943}
1944
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001945void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1946 std::vector<llvm::Constant*> &Properties,
1947 const Decl *Container,
1948 const ObjCProtocolDecl *PROTO,
1949 const ObjCCommonTypesHelper &ObjCTypes) {
1950 std::vector<llvm::Constant*> Prop(2);
1951 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1952 E = PROTO->protocol_end(); P != E; ++P)
1953 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1954 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1955 E = PROTO->prop_end(); I != E; ++I) {
1956 const ObjCPropertyDecl *PD = *I;
1957 if (!PropertySet.insert(PD->getIdentifier()))
1958 continue;
1959 Prop[0] = GetPropertyName(PD->getIdentifier());
1960 Prop[1] = GetPropertyTypeString(PD, Container);
1961 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1962 }
1963}
1964
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001966 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001967 const char * const name;
1968 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001969 };
1970
1971 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001972 uint32_t entsize; // sizeof (struct _objc_property)
1973 uint32_t prop_count;
1974 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001975 };
1976*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001977llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001978 const Decl *Container,
1979 const ObjCContainerDecl *OCD,
1980 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001981 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001982 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001983 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1984 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001985 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001986 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001987 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001988 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001989 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001990 Prop));
1991 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001992 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001993 for (ObjCInterfaceDecl::all_protocol_iterator
1994 P = OID->all_referenced_protocol_begin(),
1995 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001996 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1997 ObjCTypes);
1998 }
1999 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2000 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2001 E = CD->protocol_end(); P != E; ++P)
2002 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2003 ObjCTypes);
2004 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002005
2006 // Return null for empty list.
2007 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002008 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002009
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002010 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002011 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002012 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002013 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2014 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002015 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002016 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002017 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002018 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002019
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002020 llvm::GlobalVariable *GV =
2021 CreateMetadataVar(Name, Init,
2022 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002023 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002024 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002025 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002026 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002027}
2028
2029/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002030 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002031 int count;
2032 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002033 };
2034*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002035llvm::Constant *
2036CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2037 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002038 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002039 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2040 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002041 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002042 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002043 Desc);
2044}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002045
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002046llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002047 const char *Section,
2048 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002049 // Return null for empty list.
2050 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002051 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002052
2053 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002054 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002055 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002056 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002057 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002058 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002059
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002060 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002061 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002063}
2064
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002065/*
2066 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002067 char *category_name;
2068 char *class_name;
2069 struct _objc_method_list *instance_methods;
2070 struct _objc_method_list *class_methods;
2071 struct _objc_protocol_list *protocols;
2072 uint32_t size; // <rdar://4585769>
2073 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002074 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002075*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002076void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002077 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002078
Mike Stumpf5408fe2009-05-16 07:57:57 +00002079 // FIXME: This is poor design, the OCD should have a pointer to the category
2080 // decl. Additionally, note that Category can be null for the @implementation
2081 // w/o an @interface case. Sema should just create one for us as it does for
2082 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002083 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002085 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002086
2087 llvm::SmallString<256> ExtName;
2088 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2089 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002090
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002091 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002092 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002093 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002094 // Instance methods should always be defined.
2095 InstanceMethods.push_back(GetMethodConstant(*i));
2096 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002097 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002098 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002099 // Class methods should always be defined.
2100 ClassMethods.push_back(GetMethodConstant(*i));
2101 }
2102
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002103 std::vector<llvm::Constant*> Values(7);
2104 Values[0] = GetClassName(OCD->getIdentifier());
2105 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002106 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002107 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002108 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002109 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002110 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002112 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002113 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002114 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002115 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002116 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002117 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002118 Category->protocol_begin(),
2119 Category->protocol_end());
2120 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002121 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002122 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002123 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002124
2125 // If there is no category @interface then there can be no properties.
2126 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002127 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002128 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002129 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002130 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002131 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002132
Owen Anderson08e25242009-07-27 22:29:56 +00002133 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002134 Values);
2135
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002137 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002138 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002139 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002140 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002141 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002142}
2143
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002144// FIXME: Get from somewhere?
2145enum ClassFlags {
2146 eClassFlags_Factory = 0x00001,
2147 eClassFlags_Meta = 0x00002,
2148 // <rdr://5142207>
2149 eClassFlags_HasCXXStructors = 0x02000,
2150 eClassFlags_Hidden = 0x20000,
2151 eClassFlags_ABI2_Hidden = 0x00010,
2152 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2153};
2154
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155/*
2156 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002157 Class isa;
2158 Class super_class;
2159 const char *name;
2160 long version;
2161 long info;
2162 long instance_size;
2163 struct _objc_ivar_list *ivars;
2164 struct _objc_method_list *methods;
2165 struct _objc_cache *cache;
2166 struct _objc_protocol_list *protocols;
2167 // Objective-C 1.0 extensions (<rdr://4585769>)
2168 const char *ivar_layout;
2169 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170 };
2171
2172 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002173*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002174void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002175 DefinedSymbols.insert(ID->getIdentifier());
2176
Chris Lattner8ec03f52008-11-24 03:54:41 +00002177 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002178 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002179 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002180 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002181 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002182 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002183 Interface->all_referenced_protocol_begin(),
2184 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002185 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002186 if (ID->getNumIvarInitializers())
2187 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002188 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002189 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002190
2191 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002192 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 Flags |= eClassFlags_Hidden;
2194
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002195 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002196 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002197 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002198 // Instance methods should always be defined.
2199 InstanceMethods.push_back(GetMethodConstant(*i));
2200 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002201 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002202 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002203 // Class methods should always be defined.
2204 ClassMethods.push_back(GetMethodConstant(*i));
2205 }
2206
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002207 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002208 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002209 ObjCPropertyImplDecl *PID = *i;
2210
2211 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2212 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2213
2214 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2215 if (llvm::Constant *C = GetMethodConstant(MD))
2216 InstanceMethods.push_back(C);
2217 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2218 if (llvm::Constant *C = GetMethodConstant(MD))
2219 InstanceMethods.push_back(C);
2220 }
2221 }
2222
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002224 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002226 // Record a reference to the super class.
2227 LazySymbols.insert(Super->getIdentifier());
2228
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002229 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002230 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231 ObjCTypes.ClassPtrTy);
2232 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002233 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234 }
2235 Values[ 2] = GetClassName(ID->getIdentifier());
2236 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002237 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2238 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2239 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002240 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002241 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002242 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002243 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002244 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002245 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002246 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002248 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002249 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002250 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002252 std::string Name("\01L_OBJC_CLASS_");
2253 Name += ClassName;
2254 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2255 // Check for a forward reference.
2256 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2257 if (GV) {
2258 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2259 "Forward metaclass reference has incorrect type.");
2260 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2261 GV->setInitializer(Init);
2262 GV->setSection(Section);
2263 GV->setAlignment(4);
2264 CGM.AddUsedGlobal(GV);
2265 }
2266 else
2267 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268 DefinedClasses.push_back(GV);
2269}
2270
2271llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2272 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002273 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002275 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276
Daniel Dunbar04d40782009-04-14 06:00:08 +00002277 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002278 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002279
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 std::vector<llvm::Constant*> Values(12);
2281 // The isa for the metaclass is the root of the hierarchy.
2282 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2283 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2284 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002285 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002286 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002288 // The super class for the metaclass is emitted as the name of the
2289 // super class. The runtime fixes this up to point to the
2290 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002292 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002293 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294 ObjCTypes.ClassPtrTy);
2295 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002296 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 }
2298 Values[ 2] = GetClassName(ID->getIdentifier());
2299 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002300 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2301 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2302 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002303 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002304 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002305 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002306 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002307 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002309 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002310 Values[ 9] = Protocols;
2311 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002312 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002313 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002314 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002315 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316 Values);
2317
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002318 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002319 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002320
2321 // Check for a forward reference.
2322 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2323 if (GV) {
2324 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2325 "Forward metaclass reference has incorrect type.");
2326 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2327 GV->setInitializer(Init);
2328 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002329 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002330 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002331 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002332 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002334 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002335 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336
2337 return GV;
2338}
2339
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002340llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002341 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002342
Mike Stumpf5408fe2009-05-16 07:57:57 +00002343 // FIXME: Should we look these up somewhere other than the module. Its a bit
2344 // silly since we only generate these while processing an implementation, so
2345 // exactly one pointer would work if know when we entered/exitted an
2346 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002347
2348 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002349 // Previously, metaclass with internal linkage may have been defined.
2350 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002351 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2352 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002353 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2354 "Forward metaclass reference has incorrect type.");
2355 return GV;
2356 } else {
2357 // Generate as an external reference to keep a consistent
2358 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002359 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002360 llvm::GlobalValue::ExternalLinkage,
2361 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002362 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002363 }
2364}
2365
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002366llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2367 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2368
2369 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2370 true)) {
2371 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2372 "Forward class metadata reference has incorrect type.");
2373 return GV;
2374 } else {
2375 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2376 llvm::GlobalValue::ExternalLinkage,
2377 0,
2378 Name);
2379 }
2380}
2381
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382/*
2383 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002384 uint32_t size;
2385 const char *weak_ivar_layout;
2386 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002387 };
2388*/
2389llvm::Constant *
2390CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002391 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002392 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393
2394 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002395 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002396 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002397 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002398 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002399
2400 // Return null if no extension bits are used.
2401 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002402 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002403
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002404 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002405 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002406 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002407 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002408 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002409}
2410
2411/*
2412 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002413 char *ivar_name;
2414 char *ivar_type;
2415 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416 };
2417
2418 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002419 int ivar_count;
2420 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002422*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002423llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002424 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002425 std::vector<llvm::Constant*> Ivars, Ivar(3);
2426
2427 // When emitting the root class GCC emits ivar entries for the
2428 // actual class structure. It is not clear if we need to follow this
2429 // behavior; for now lets try and get away with not doing it. If so,
2430 // the cleanest solution would be to make up an ObjCInterfaceDecl
2431 // for the class.
2432 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002433 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002434
2435 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002436 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002437
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002438 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002439 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002441 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2442 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002443 // Ignore unnamed bit-fields.
2444 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002445 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002446 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2447 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002449 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002450 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 }
2452
2453 // Return null for empty list.
2454 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002455 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002456
2457 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002458 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002459 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002460 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002461 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002462 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002463
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002464 llvm::GlobalVariable *GV;
2465 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002466 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002467 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002468 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002469 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002470 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002471 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002472 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002473 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002474}
2475
2476/*
2477 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002478 SEL method_name;
2479 char *method_types;
2480 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002483 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002484 struct objc_method_list *obsolete;
2485 int count;
2486 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002487 };
2488*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002489
2490/// GetMethodConstant - Return a struct objc_method constant for the
2491/// given method if it has been defined. The result is null if the
2492/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002493llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002494 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002495 if (!Fn)
2496 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002498 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002499 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002500 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002501 ObjCTypes.SelectorPtrTy);
2502 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002503 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002504 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002505}
2506
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002507llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002508 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002509 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002510 // Return null for empty list.
2511 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002512 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002513
2514 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002515 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002516 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002517 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002518 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002519 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002520 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002521
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002522 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002523 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002524 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002525}
2526
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002527llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002528 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002529 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002530 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002531
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002532 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002533 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002534 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002535 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002536 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002537 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002538 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002539 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002540 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002541
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002542 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002543}
2544
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002545llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002546CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002547 llvm::Constant *Init,
2548 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002549 unsigned Align,
2550 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002551 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002552 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002553 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002554 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002555 if (Section)
2556 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002557 if (Align)
2558 GV->setAlignment(Align);
2559 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002560 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002561 return GV;
2562}
2563
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002564llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002565 // Abuse this interface function as a place to finalize.
2566 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002567 return NULL;
2568}
2569
Chris Lattner74391b42009-03-22 21:03:39 +00002570llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002571 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002572}
2573
Chris Lattner74391b42009-03-22 21:03:39 +00002574llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002575 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002576}
2577
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002578llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2579 return ObjCTypes.getCopyStructFn();
2580}
2581
Chris Lattner74391b42009-03-22 21:03:39 +00002582llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002583 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002584}
2585
John McCallf1549f62010-07-06 01:34:17 +00002586void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2587 return EmitTryOrSynchronizedStmt(CGF, S);
2588}
2589
2590void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2591 const ObjCAtSynchronizedStmt &S) {
2592 return EmitTryOrSynchronizedStmt(CGF, S);
2593}
2594
John McCallcc505292010-07-21 06:59:36 +00002595namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002596 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002597 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002598 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002599 llvm::Value *CallTryExitVar;
2600 llvm::Value *ExceptionData;
2601 ObjCTypesHelper &ObjCTypes;
2602 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002603 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002604 llvm::Value *CallTryExitVar,
2605 llvm::Value *ExceptionData,
2606 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002607 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002608 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2609
2610 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2611 // Check whether we need to call objc_exception_try_exit.
2612 // In optimized code, this branch will always be folded.
2613 llvm::BasicBlock *FinallyCallExit =
2614 CGF.createBasicBlock("finally.call_exit");
2615 llvm::BasicBlock *FinallyNoCallExit =
2616 CGF.createBasicBlock("finally.no_call_exit");
2617 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2618 FinallyCallExit, FinallyNoCallExit);
2619
2620 CGF.EmitBlock(FinallyCallExit);
2621 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2622 ->setDoesNotThrow();
2623
2624 CGF.EmitBlock(FinallyNoCallExit);
2625
2626 if (isa<ObjCAtTryStmt>(S)) {
2627 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002628 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2629 // Save the current cleanup destination in case there's
2630 // control flow inside the finally statement.
2631 llvm::Value *CurCleanupDest =
2632 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2633
John McCallcc505292010-07-21 06:59:36 +00002634 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2635
John McCalld96a8e72010-08-11 00:16:14 +00002636 if (CGF.HaveInsertPoint()) {
2637 CGF.Builder.CreateStore(CurCleanupDest,
2638 CGF.getNormalCleanupDestSlot());
2639 } else {
2640 // Currently, the end of the cleanup must always exist.
2641 CGF.EnsureInsertPoint();
2642 }
2643 }
John McCallcc505292010-07-21 06:59:36 +00002644 } else {
2645 // Emit objc_sync_exit(expr); as finally's sole statement for
2646 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002647 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002648 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2649 ->setDoesNotThrow();
2650 }
2651 }
2652 };
John McCall87bb5822010-07-31 23:20:56 +00002653
2654 class FragileHazards {
2655 CodeGenFunction &CGF;
2656 llvm::SmallVector<llvm::Value*, 20> Locals;
2657 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2658
2659 llvm::InlineAsm *ReadHazard;
2660 llvm::InlineAsm *WriteHazard;
2661
2662 llvm::FunctionType *GetAsmFnType();
2663
2664 void collectLocals();
2665 void emitReadHazard(CGBuilderTy &Builder);
2666
2667 public:
2668 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002669
John McCall87bb5822010-07-31 23:20:56 +00002670 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002671 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002672 };
2673}
2674
2675/// Create the fragile-ABI read and write hazards based on the current
2676/// state of the function, which is presumed to be immediately prior
2677/// to a @try block. These hazards are used to maintain correct
2678/// semantics in the face of optimization and the fragile ABI's
2679/// cavalier use of setjmp/longjmp.
2680FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2681 collectLocals();
2682
2683 if (Locals.empty()) return;
2684
2685 // Collect all the blocks in the function.
2686 for (llvm::Function::iterator
2687 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2688 BlocksBeforeTry.insert(&*I);
2689
2690 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2691
2692 // Create a read hazard for the allocas. This inhibits dead-store
2693 // optimizations and forces the values to memory. This hazard is
2694 // inserted before any 'throwing' calls in the protected scope to
2695 // reflect the possibility that the variables might be read from the
2696 // catch block if the call throws.
2697 {
2698 std::string Constraint;
2699 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2700 if (I) Constraint += ',';
2701 Constraint += "*m";
2702 }
2703
2704 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2705 }
2706
2707 // Create a write hazard for the allocas. This inhibits folding
2708 // loads across the hazard. This hazard is inserted at the
2709 // beginning of the catch path to reflect the possibility that the
2710 // variables might have been written within the protected scope.
2711 {
2712 std::string Constraint;
2713 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2714 if (I) Constraint += ',';
2715 Constraint += "=*m";
2716 }
2717
2718 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2719 }
2720}
2721
2722/// Emit a write hazard at the current location.
2723void FragileHazards::emitWriteHazard() {
2724 if (Locals.empty()) return;
2725
2726 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2727 ->setDoesNotThrow();
2728}
2729
John McCall87bb5822010-07-31 23:20:56 +00002730void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2731 assert(!Locals.empty());
2732 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2733 ->setDoesNotThrow();
2734}
2735
2736/// Emit read hazards in all the protected blocks, i.e. all the blocks
2737/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002738void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002739 if (Locals.empty()) return;
2740
2741 CGBuilderTy Builder(CGF.getLLVMContext());
2742
2743 // Iterate through all blocks, skipping those prior to the try.
2744 for (llvm::Function::iterator
2745 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2746 llvm::BasicBlock &BB = *FI;
2747 if (BlocksBeforeTry.count(&BB)) continue;
2748
2749 // Walk through all the calls in the block.
2750 for (llvm::BasicBlock::iterator
2751 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2752 llvm::Instruction &I = *BI;
2753
2754 // Ignore instructions that aren't non-intrinsic calls.
2755 // These are the only calls that can possibly call longjmp.
2756 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2757 if (isa<llvm::IntrinsicInst>(I))
2758 continue;
2759
2760 // Ignore call sites marked nounwind. This may be questionable,
2761 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2762 llvm::CallSite CS(&I);
2763 if (CS.doesNotThrow()) continue;
2764
John McCall0b251722010-08-04 05:59:32 +00002765 // Insert a read hazard before the call. This will ensure that
2766 // any writes to the locals are performed before making the
2767 // call. If the call throws, then this is sufficient to
2768 // guarantee correctness as long as it doesn't also write to any
2769 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002770 Builder.SetInsertPoint(&BB, BI);
2771 emitReadHazard(Builder);
2772 }
2773 }
2774}
2775
2776static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2777 if (V) S.insert(V);
2778}
2779
2780void FragileHazards::collectLocals() {
2781 // Compute a set of allocas to ignore.
2782 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2783 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2784 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2785 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2786
2787 // Collect all the allocas currently in the function. This is
2788 // probably way too aggressive.
2789 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2790 for (llvm::BasicBlock::iterator
2791 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2792 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2793 Locals.push_back(&*I);
2794}
2795
2796llvm::FunctionType *FragileHazards::GetAsmFnType() {
2797 std::vector<const llvm::Type *> Tys(Locals.size());
2798 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2799 Tys[I] = Locals[I]->getType();
2800 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002801}
2802
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002803/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002804
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002805 Objective-C setjmp-longjmp (sjlj) Exception Handling
2806 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002807
John McCallf1549f62010-07-06 01:34:17 +00002808 A catch buffer is a setjmp buffer plus:
2809 - a pointer to the exception that was caught
2810 - a pointer to the previous exception data buffer
2811 - two pointers of reserved storage
2812 Therefore catch buffers form a stack, with a pointer to the top
2813 of the stack kept in thread-local storage.
2814
2815 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2816 objc_exception_try_exit pops the given catch buffer, which is
2817 required to be the top of the EH stack.
2818 objc_exception_throw pops the top of the EH stack, writes the
2819 thrown exception into the appropriate field, and longjmps
2820 to the setjmp buffer. It crashes the process (with a printf
2821 and an abort()) if there are no catch buffers on the stack.
2822 objc_exception_extract just reads the exception pointer out of the
2823 catch buffer.
2824
2825 There's no reason an implementation couldn't use a light-weight
2826 setjmp here --- something like __builtin_setjmp, but API-compatible
2827 with the heavyweight setjmp. This will be more important if we ever
2828 want to implement correct ObjC/C++ exception interactions for the
2829 fragile ABI.
2830
2831 Note that for this use of setjmp/longjmp to be correct, we may need
2832 to mark some local variables volatile: if a non-volatile local
2833 variable is modified between the setjmp and the longjmp, it has
2834 indeterminate value. For the purposes of LLVM IR, it may be
2835 sufficient to make loads and stores within the @try (to variables
2836 declared outside the @try) volatile. This is necessary for
2837 optimized correctness, but is not currently being done; this is
2838 being tracked as rdar://problem/8160285
2839
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002840 The basic framework for a @try-catch-finally is as follows:
2841 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002842 objc_exception_data d;
2843 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002844 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002845
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002846 objc_exception_try_enter(&d);
2847 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002848 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002849 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002850 // exception path
2851 id _caught = objc_exception_extract(&d);
2852
2853 // enter new try scope for handlers
2854 if (!setjmp(d.jmp_buf)) {
2855 ... match exception and execute catch blocks ...
2856
2857 // fell off end, rethrow.
2858 _rethrow = _caught;
2859 ... jump-through-finally to finally_rethrow ...
2860 } else {
2861 // exception in catch block
2862 _rethrow = objc_exception_extract(&d);
2863 _call_try_exit = false;
2864 ... jump-through-finally to finally_rethrow ...
2865 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002866 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002867 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002868
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002869 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002870 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002871 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002872
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002873 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002874 ... dispatch to finally destination ...
2875
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002876 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002877 objc_exception_throw(_rethrow);
2878
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002879 finally_end:
2880 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002881
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002882 This framework differs slightly from the one gcc uses, in that gcc
2883 uses _rethrow to determine if objc_exception_try_exit should be called
2884 and if the object should be rethrown. This breaks in the face of
2885 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002886
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002887 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002888
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002889 - If there are no catch blocks, then we avoid emitting the second
2890 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002891
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002892 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2893 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002894
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002895 - FIXME: If there is no @finally block we can do a few more
2896 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002897
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002898 Rethrows and Jumps-Through-Finally
2899 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002900
John McCallf1549f62010-07-06 01:34:17 +00002901 '@throw;' is supported by pushing the currently-caught exception
2902 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002903
John McCallf1549f62010-07-06 01:34:17 +00002904 Branches through the @finally block are handled with an ordinary
2905 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2906 exceptions are not compatible with C++ exceptions, and this is
2907 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002908
John McCallf1549f62010-07-06 01:34:17 +00002909 @synchronized(expr) { stmt; } is emitted as if it were:
2910 id synch_value = expr;
2911 objc_sync_enter(synch_value);
2912 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002913*/
2914
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002915void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2916 const Stmt &S) {
2917 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002918
2919 // A destination for the fall-through edges of the catch handlers to
2920 // jump to.
2921 CodeGenFunction::JumpDest FinallyEnd =
2922 CGF.getJumpDestInCurrentScope("finally.end");
2923
2924 // A destination for the rethrow edge of the catch handlers to jump
2925 // to.
2926 CodeGenFunction::JumpDest FinallyRethrow =
2927 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002928
Daniel Dunbar1c566672009-02-24 01:43:46 +00002929 // For @synchronized, call objc_sync_enter(sync.expr). The
2930 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002931 // @synchronized. We can't avoid a temp here because we need the
2932 // value to be preserved. If the backend ever does liveness
2933 // correctly after setjmp, this will be unnecessary.
2934 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002935 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002936 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002937 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2938 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002939 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2940 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002941
2942 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2943 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002944 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002945
John McCall0b251722010-08-04 05:59:32 +00002946 // Allocate memory for the setjmp buffer. This needs to be kept
2947 // live throughout the try and catch blocks.
2948 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2949 "exceptiondata.ptr");
2950
John McCall87bb5822010-07-31 23:20:56 +00002951 // Create the fragile hazards. Note that this will not capture any
2952 // of the allocas required for exception processing, but will
2953 // capture the current basic block (which extends all the way to the
2954 // setjmp call) as "before the @try".
2955 FragileHazards Hazards(CGF);
2956
John McCallf1549f62010-07-06 01:34:17 +00002957 // Create a flag indicating whether the cleanup needs to call
2958 // objc_exception_try_exit. This is true except when
2959 // - no catches match and we're branching through the cleanup
2960 // just to rethrow the exception, or
2961 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002962 // The setjmp-safety rule here is that we should always store to this
2963 // variable in a place that dominates the branch through the cleanup
2964 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002965 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002966 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002967
John McCallf1549f62010-07-06 01:34:17 +00002968 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002969 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002970 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002971 CallTryExitVar,
2972 ExceptionData,
2973 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002974
2975 // Enter a try block:
2976 // - Call objc_exception_try_enter to push ExceptionData on top of
2977 // the EH stack.
2978 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2979 ->setDoesNotThrow();
2980
2981 // - Call setjmp on the exception data buffer.
2982 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2983 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2984 llvm::Value *SetJmpBuffer =
2985 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2986 llvm::CallInst *SetJmpResult =
2987 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2988 SetJmpResult->setDoesNotThrow();
2989
2990 // If setjmp returned 0, enter the protected block; otherwise,
2991 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002992 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2993 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002994 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002995 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2996 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002997
John McCallf1549f62010-07-06 01:34:17 +00002998 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002999 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003000 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003001 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003002 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003003
3004 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003005
John McCallf1549f62010-07-06 01:34:17 +00003006 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003007 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003008
John McCall87bb5822010-07-31 23:20:56 +00003009 // Don't optimize loads of the in-scope locals across this point.
3010 Hazards.emitWriteHazard();
3011
John McCallf1549f62010-07-06 01:34:17 +00003012 // For a @synchronized (or a @try with no catches), just branch
3013 // through the cleanup to the rethrow block.
3014 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3015 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003016 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003017 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003018
3019 // Otherwise, we have to match against the caught exceptions.
3020 } else {
John McCall0b251722010-08-04 05:59:32 +00003021 // Retrieve the exception object. We may emit multiple blocks but
3022 // nothing can cross this so the value is already in SSA form.
3023 llvm::CallInst *Caught =
3024 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3025 ExceptionData, "caught");
3026 Caught->setDoesNotThrow();
3027
John McCallf1549f62010-07-06 01:34:17 +00003028 // Push the exception to rethrow onto the EH value stack for the
3029 // benefit of any @throws in the handlers.
3030 CGF.ObjCEHValueStack.push_back(Caught);
3031
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003032 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003033
John McCall0b251722010-08-04 05:59:32 +00003034 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003035
John McCall0b251722010-08-04 05:59:32 +00003036 llvm::BasicBlock *CatchBlock = 0;
3037 llvm::BasicBlock *CatchHandler = 0;
3038 if (HasFinally) {
3039 // Enter a new exception try block (in case a @catch block
3040 // throws an exception).
3041 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3042 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003043
John McCall0b251722010-08-04 05:59:32 +00003044 llvm::CallInst *SetJmpResult =
3045 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3046 "setjmp.result");
3047 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048
John McCall0b251722010-08-04 05:59:32 +00003049 llvm::Value *Threw =
3050 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3051
3052 CatchBlock = CGF.createBasicBlock("catch");
3053 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3054 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3055
3056 CGF.EmitBlock(CatchBlock);
3057 }
3058
3059 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003060
Daniel Dunbar55e40722008-09-27 07:03:52 +00003061 // Handle catch list. As a special case we check if everything is
3062 // matched and avoid generating code for falling off the end if
3063 // so.
3064 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003065 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3066 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003067
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003068 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003069 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003070
Anders Carlsson80f25672008-09-09 17:59:25 +00003071 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003072 if (!CatchParam) {
3073 AllMatched = true;
3074 } else {
John McCall183700f2009-09-21 23:43:11 +00003075 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003076
John McCallf1549f62010-07-06 01:34:17 +00003077 // catch(id e) always matches under this ABI, since only
3078 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003079 // FIXME: For the time being we also match id<X>; this should
3080 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003081 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003082 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003083 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003084
John McCallf1549f62010-07-06 01:34:17 +00003085 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003087 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3088
Anders Carlssondde0a942008-09-11 09:15:33 +00003089 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003090 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003091 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003092
3093 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003094 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003095 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096
Anders Carlssondde0a942008-09-11 09:15:33 +00003097 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003098
3099 // The scope of the catch variable ends right here.
3100 CatchVarCleanups.ForceCleanup();
3101
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003102 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003103 break;
3104 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105
Steve Naroff14108da2009-07-10 23:34:53 +00003106 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003107 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003108
3109 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003110 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3111 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003112
3113 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003114 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003115
John McCallf1549f62010-07-06 01:34:17 +00003116 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003117 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3118 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003119 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003120
John McCallf1549f62010-07-06 01:34:17 +00003121 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3122 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003123
3124 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003125 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003126
Anders Carlsson80f25672008-09-09 17:59:25 +00003127 // Emit the @catch block.
3128 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003129
3130 // Collect any cleanups for the catch variable. The scope lasts until
3131 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003132 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003133
Steve Naroff7ba138a2009-03-03 19:52:17 +00003134 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003135 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003136
John McCallf1549f62010-07-06 01:34:17 +00003137 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003138 llvm::Value *Tmp =
3139 CGF.Builder.CreateBitCast(Caught,
3140 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003141 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003142 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003143
Anders Carlssondde0a942008-09-11 09:15:33 +00003144 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003145
3146 // We're done with the catch variable.
3147 CatchVarCleanups.ForceCleanup();
3148
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003149 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003150
Anders Carlsson80f25672008-09-09 17:59:25 +00003151 CGF.EmitBlock(NextCatchBlock);
3152 }
3153
John McCallf1549f62010-07-06 01:34:17 +00003154 CGF.ObjCEHValueStack.pop_back();
3155
John McCall0b251722010-08-04 05:59:32 +00003156 // If nothing wanted anything to do with the caught exception,
3157 // kill the extract call.
3158 if (Caught->use_empty())
3159 Caught->eraseFromParent();
3160
3161 if (!AllMatched)
3162 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3163
3164 if (HasFinally) {
3165 // Emit the exception handler for the @catch blocks.
3166 CGF.EmitBlock(CatchHandler);
3167
3168 // In theory we might now need a write hazard, but actually it's
3169 // unnecessary because there's no local-accessing code between
3170 // the try's write hazard and here.
3171 //Hazards.emitWriteHazard();
3172
3173 // Don't pop the catch handler; the throw already did.
3174 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003175 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003176 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003177 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003178
John McCall87bb5822010-07-31 23:20:56 +00003179 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003180 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003181
John McCallf1549f62010-07-06 01:34:17 +00003182 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003183 CGF.Builder.restoreIP(TryFallthroughIP);
3184 if (CGF.HaveInsertPoint())
3185 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003186 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003187 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003188
John McCallf1549f62010-07-06 01:34:17 +00003189 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003190 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003191 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003192 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003193 // Just look in the buffer for the exception to throw.
3194 llvm::CallInst *Caught =
3195 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3196 ExceptionData);
3197 Caught->setDoesNotThrow();
3198
3199 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003200 ->setDoesNotThrow();
3201 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003202 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003203
John McCall87bb5822010-07-31 23:20:56 +00003204 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003205}
3206
3207void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003208 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003209 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003210
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003211 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3212 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003214 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3215 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003216 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003217 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003218 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003219 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003220
John McCallf1549f62010-07-06 01:34:17 +00003221 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3222 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003223 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003224
3225 // Clear the insertion point to indicate we are in unreachable code.
3226 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003227}
3228
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003229/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003230/// object: objc_read_weak (id *src)
3231///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003232llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003233 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003234 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003235 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3236 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3237 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003238 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003239 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003240 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003241 return read_weak;
3242}
3243
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003244/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3245/// objc_assign_weak (id src, id *dst)
3246///
3247void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003248 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003249 const llvm::Type * SrcTy = src->getType();
3250 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003251 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003252 assert(Size <= 8 && "does not support size > 8");
3253 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003254 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003255 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3256 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003257 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3258 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003259 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003260 src, dst, "weakassign");
3261 return;
3262}
3263
Fariborz Jahanian58626502008-11-19 00:59:10 +00003264/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3265/// objc_assign_global (id src, id *dst)
3266///
3267void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003268 llvm::Value *src, llvm::Value *dst,
3269 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003270 const llvm::Type * SrcTy = src->getType();
3271 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003272 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003273 assert(Size <= 8 && "does not support size > 8");
3274 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003275 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003276 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3277 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003278 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3279 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003280 if (!threadlocal)
3281 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3282 src, dst, "globalassign");
3283 else
3284 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3285 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003286 return;
3287}
3288
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003289/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003290/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003291///
3292void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003293 llvm::Value *src, llvm::Value *dst,
3294 llvm::Value *ivarOffset) {
3295 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003296 const llvm::Type * SrcTy = src->getType();
3297 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003298 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003299 assert(Size <= 8 && "does not support size > 8");
3300 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003301 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003302 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3303 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003304 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3305 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003306 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3307 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003308 return;
3309}
3310
Fariborz Jahanian58626502008-11-19 00:59:10 +00003311/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3312/// objc_assign_strongCast (id src, id *dst)
3313///
3314void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003315 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003316 const llvm::Type * SrcTy = src->getType();
3317 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003318 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003319 assert(Size <= 8 && "does not support size > 8");
3320 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003321 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003322 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3323 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003324 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3325 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003326 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003327 src, dst, "weakassign");
3328 return;
3329}
3330
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003331void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003332 llvm::Value *DestPtr,
3333 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003334 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003335 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3336 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003337 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003338 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003339 return;
3340}
3341
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003342/// EmitObjCValueForIvar - Code Gen for ivar reference.
3343///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003344LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3345 QualType ObjectTy,
3346 llvm::Value *BaseValue,
3347 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003348 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003349 const ObjCInterfaceDecl *ID =
3350 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003351 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3352 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003353}
3354
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003355llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003356 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003357 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003358 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003359 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003360 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3361 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003362}
3363
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003364/* *** Private Interface *** */
3365
3366/// EmitImageInfo - Emit the image info marker used to encode some module
3367/// level information.
3368///
3369/// See: <rdr://4810609&4810587&4810587>
3370/// struct IMAGE_INFO {
3371/// unsigned version;
3372/// unsigned flags;
3373/// };
3374enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003375 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003376 eImageInfo_GarbageCollected = (1 << 1),
3377 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003378 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3379
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003380 // A flag indicating that the module has no instances of a @synthesize of a
3381 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003382 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003383};
3384
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003385void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003386 unsigned version = 0; // Version is unused?
3387 unsigned flags = 0;
3388
3389 // FIXME: Fix and continue?
3390 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3391 flags |= eImageInfo_GarbageCollected;
3392 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3393 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003395 // We never allow @synthesize of a superclass property.
3396 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003397
Chris Lattner77b89b82010-06-27 07:15:29 +00003398 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3399
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003400 // Emitted as int[2];
3401 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003402 llvm::ConstantInt::get(Int32Ty, version),
3403 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003404 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003405 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003406
3407 const char *Section;
3408 if (ObjCABI == 1)
3409 Section = "__OBJC, __image_info,regular";
3410 else
3411 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003412 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003413 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003414 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003415 Section,
3416 0,
3417 true);
3418 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003419}
3420
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003421
3422// struct objc_module {
3423// unsigned long version;
3424// unsigned long size;
3425// const char *name;
3426// Symtab symtab;
3427// };
3428
3429// FIXME: Get from somewhere
3430static const int ModuleVersion = 7;
3431
3432void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003433 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003434
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003435 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003436 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3437 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003438 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003439 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003440 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003441 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003442 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003443 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003444 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003445}
3446
3447llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003448 unsigned NumClasses = DefinedClasses.size();
3449 unsigned NumCategories = DefinedCategories.size();
3450
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003451 // Return null if no symbols were defined.
3452 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003453 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003454
3455 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003456 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003457 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003458 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3459 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003461 // The runtime expects exactly the list of defined classes followed
3462 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003463 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003464 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003465 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003466 ObjCTypes.Int8PtrTy);
3467 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003469 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003470 ObjCTypes.Int8PtrTy);
3471
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003472 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003473 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003474 NumClasses + NumCategories),
3475 Symbols);
3476
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003477 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003478
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003479 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003480 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3481 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003482 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003483 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003484}
3485
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003486llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003487 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003488 LazySymbols.insert(ID->getIdentifier());
3489
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003490 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003491
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003492 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003494 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003495 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003496 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003497 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3498 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003499 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003500 }
3501
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003502 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003503}
3504
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003505llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3506 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003507 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003508
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003509 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003510 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003511 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003512 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003513 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003514 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3515 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003516 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003517 }
3518
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003519 if (lvalue)
3520 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003521 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003522}
3523
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003524llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003525 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003526
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003527 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003528 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003529 llvm::ConstantArray::get(VMContext,
3530 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003531 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003532 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003533
Owen Andersona1cf15f2009-07-14 23:10:40 +00003534 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003535}
3536
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003537llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3538 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3539 I = MethodDefinitions.find(MD);
3540 if (I != MethodDefinitions.end())
3541 return I->second;
3542
3543 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3544 // MD isn't emitted yet because it comes from PCH.
3545 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3546 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3547 return MethodDefinitions[MD];
3548 }
3549
3550 return NULL;
3551}
3552
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003553/// GetIvarLayoutName - Returns a unique constant for the given
3554/// ivar layout bitmap.
3555llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003557 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003558}
3559
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003560void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003561 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003562 bool ForStrongLayout,
3563 bool &HasUnion) {
3564 const RecordDecl *RD = RT->getDecl();
3565 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003566 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003567 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003568 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003569 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003570
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003571 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3572 ForStrongLayout, HasUnion);
3573}
3574
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003575void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003576 const llvm::StructLayout *Layout,
3577 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003578 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003579 unsigned int BytePos, bool ForStrongLayout,
3580 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003581 bool IsUnion = (RD && RD->isUnion());
3582 uint64_t MaxUnionIvarSize = 0;
3583 uint64_t MaxSkippedUnionIvarSize = 0;
3584 FieldDecl *MaxField = 0;
3585 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003586 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003587 uint64_t MaxFieldOffset = 0;
3588 uint64_t MaxSkippedFieldOffset = 0;
3589 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003590
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003591 if (RecFields.empty())
3592 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003593 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3594 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3595
Chris Lattnerf1690852009-03-31 08:48:01 +00003596 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003597 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003598 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003599 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003600 // Note that 'i' here is actually the field index inside RD of Field,
3601 // although this dependency is hidden.
3602 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3603 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003604 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003605 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003606
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003607 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003608 if (!Field->getIdentifier() || Field->isBitField()) {
3609 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003610 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003611 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003612 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003613
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003614 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003615 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003616 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003617 if (FQT->isUnionType())
3618 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003619
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003620 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003621 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003622 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003623 continue;
3624 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003625
Chris Lattnerf1690852009-03-31 08:48:01 +00003626 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003627 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003628 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003629 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003630 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003631 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003632 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3633 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003634 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003635 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003636 FQT = CArray->getElementType();
3637 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003638
3639 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 "layout for array of unions not supported");
3641 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003642 int OldIndex = IvarsInfo.size() - 1;
3643 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003644
Ted Kremenek6217b802009-07-29 21:53:49 +00003645 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003646 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003647 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003648
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003649 // Replicate layout information for each array element. Note that
3650 // one element is already done.
3651 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003652 for (int FirstIndex = IvarsInfo.size() - 1,
3653 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003654 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003655 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3656 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3657 IvarsInfo[i].ivar_size));
3658 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3659 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3660 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003661 }
3662 continue;
3663 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003664 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003665 // At this point, we are done with Record/Union and array there of.
3666 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003667 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003668
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003669 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003670 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3671 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003672 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003673 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003674 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003675 MaxUnionIvarSize = UnionIvarSize;
3676 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003677 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003678 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003679 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003680 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003681 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003682 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003683 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003684 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3685 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003686 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003687 // FIXME: Why the asymmetry? We divide by word size in bits on other
3688 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003689 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003690 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003691 MaxSkippedUnionIvarSize = UnionIvarSize;
3692 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003693 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003694 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003695 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003696 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003697 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003698 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003699 }
3700 }
3701 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003702
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003703 if (LastFieldBitfield) {
3704 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003705 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3706 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003707 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003708 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003709 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003710 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3711 + ((BitFieldSize % ByteSizeInBits) != 0);
3712 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003713 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003714
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003715 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003716 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003717 MaxUnionIvarSize));
3718 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003719 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003720 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003721}
3722
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003723/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3724/// the computations and returning the layout bitmap (for ivar or blocks) in
3725/// the given argument BitMap string container. Routine reads
3726/// two containers, IvarsInfo and SkipIvars which are assumed to be
3727/// filled already by the caller.
3728llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003729 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003730 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003731
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003732 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003733 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003734 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003735 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003736 if (IvarsInfo[0].ivar_bytepos == 0) {
3737 WordsToSkip = 0;
3738 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003739 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003740 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3741 WordsToScan = IvarsInfo[0].ivar_size;
3742 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003743 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003744 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003745 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003746 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003747 // consecutive 'scanned' object pointers.
3748 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003749 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003750 // Skip over 'gc'able object pointer which lay over each other.
3751 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3752 continue;
3753 // Must skip over 1 or more words. We save current skip/scan values
3754 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003755 SKIP_SCAN SkScan;
3756 SkScan.skip = WordsToSkip;
3757 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003758 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003759
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003760 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003761 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3762 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003763 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003764 WordsToSkip = 0;
3765 WordsToScan = IvarsInfo[i].ivar_size;
3766 }
3767 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003768 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003769 SKIP_SCAN SkScan;
3770 SkScan.skip = WordsToSkip;
3771 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003772 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003773 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003774
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003775 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003776 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003777 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003778 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003779 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003780 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003781 IvarsInfo[LastIndex].ivar_bytepos +
3782 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003783 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003784 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003785 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003786 SKIP_SCAN SkScan;
3787 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3788 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003789 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003790 }
3791 }
3792 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3793 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003794 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003795 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003796 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3797 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3798 // 0xM0 followed by 0x0N detected.
3799 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3800 for (int j = i+1; j < SkipScan; j++)
3801 SkipScanIvars[j] = SkipScanIvars[j+1];
3802 --SkipScan;
3803 }
3804 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003805
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003806 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003807 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003808 unsigned char byte;
3809 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3810 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3811 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3812 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003813
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003814 // first skip big.
3815 for (unsigned int ix = 0; ix < skip_big; ix++)
3816 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003817
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003818 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003819 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003820 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003821 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003822 byte |= 0xf;
3823 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003824 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003825 byte |= scan_small;
3826 scan_small = 0;
3827 }
3828 BitMap += byte;
3829 }
3830 // next scan big
3831 for (unsigned int ix = 0; ix < scan_big; ix++)
3832 BitMap += (unsigned char)(0x0f);
3833 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003834 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003835 byte = scan_small;
3836 BitMap += byte;
3837 }
3838 }
3839 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003840 unsigned char zero = 0;
3841 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003842
3843 llvm::GlobalVariable * Entry =
3844 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3845 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3846 "__TEXT,__cstring,cstring_literals",
3847 1, true);
3848 return getConstantGEP(VMContext, Entry, 0, 0);
3849}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003850
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003851/// BuildIvarLayout - Builds ivar layout bitmap for the class
3852/// implementation for the __strong or __weak case.
3853/// The layout map displays which words in ivar list must be skipped
3854/// and which must be scanned by GC (see below). String is built of bytes.
3855/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3856/// of words to skip and right nibble is count of words to scan. So, each
3857/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3858/// represented by a 0x00 byte which also ends the string.
3859/// 1. when ForStrongLayout is true, following ivars are scanned:
3860/// - id, Class
3861/// - object *
3862/// - __strong anything
3863///
3864/// 2. When ForStrongLayout is false, following ivars are scanned:
3865/// - __weak anything
3866///
3867llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3868 const ObjCImplementationDecl *OMD,
3869 bool ForStrongLayout) {
3870 bool hasUnion = false;
3871
3872 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3873 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3874 return llvm::Constant::getNullValue(PtrTy);
3875
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003876 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003877 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003878 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003879
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003880 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003881 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3882 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3883
3884 if (RecFields.empty())
3885 return llvm::Constant::getNullValue(PtrTy);
3886
3887 SkipIvars.clear();
3888 IvarsInfo.clear();
3889
3890 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3891 if (IvarsInfo.empty())
3892 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003893 // Sort on byte position in case we encounterred a union nested in
3894 // the ivar list.
3895 if (hasUnion && !IvarsInfo.empty())
3896 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3897 if (hasUnion && !SkipIvars.empty())
3898 std::sort(SkipIvars.begin(), SkipIvars.end());
3899
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003900 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003901 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003902
3903 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003904 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003905 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003906 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003907 const unsigned char *s = (unsigned char*)BitMap.c_str();
3908 for (unsigned i = 0; i < BitMap.size(); i++)
3909 if (!(s[i] & 0xf0))
3910 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3911 else
3912 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3913 printf("\n");
3914 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003915 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003916}
3917
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003918llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003919 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3920
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003921 // FIXME: Avoid std::string copying.
3922 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003923 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003924 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003925 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003926 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003927
Owen Andersona1cf15f2009-07-14 23:10:40 +00003928 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003929}
3930
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003931// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003932llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003933 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3934}
3935
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003936llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003937 std::string TypeStr;
3938 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3939
3940 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003941
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003942 if (!Entry)
3943 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003944 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003945 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003946 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003947
Owen Andersona1cf15f2009-07-14 23:10:40 +00003948 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003949}
3950
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003951llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003952 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003953 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3954 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003955
3956 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3957
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003958 if (!Entry)
3959 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003960 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003961 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003962 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003963
Owen Andersona1cf15f2009-07-14 23:10:40 +00003964 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003965}
3966
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003967// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003968llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003969 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003970
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003971 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003972 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003973 llvm::ConstantArray::get(VMContext,
3974 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003975 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003976 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003977
Owen Andersona1cf15f2009-07-14 23:10:40 +00003978 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003979}
3980
3981// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003982// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003983llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003984CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3985 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003986 std::string TypeStr;
3987 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003988 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3989}
3990
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003992 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003993 llvm::SmallVectorImpl<char> &Name) {
3994 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003995 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003996 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3997 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003998 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003999 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004000 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004001 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004002}
4003
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004004void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004005 EmitModuleInfo();
4006
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004007 // Emit the dummy bodies for any protocols which were referenced but
4008 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004009 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004010 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4011 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004012 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004013
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004014 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004015 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004016 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004017 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004018 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004019 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004020 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004021 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004022 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004023 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004024 }
4025
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004026 // Add assembler directives to add lazy undefined symbol references
4027 // for classes which are referenced but not defined. This is
4028 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004029 //
4030 // FIXME: It would be nice if we had an LLVM construct for this.
4031 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4032 llvm::SmallString<256> Asm;
4033 Asm += CGM.getModule().getModuleInlineAsm();
4034 if (!Asm.empty() && Asm.back() != '\n')
4035 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004036
Daniel Dunbar33063492009-09-07 00:20:42 +00004037 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004038 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4039 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004040 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4041 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004042 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004043 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004044 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004045 }
4046
4047 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4048 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4049 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4050 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004051
Daniel Dunbar33063492009-09-07 00:20:42 +00004052 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004053 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004054}
4055
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004056CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004057 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004058 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004059 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004060 ObjCABI = 2;
4061}
4062
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004063/* *** */
4064
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004065ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004066 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004067 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4068 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004069
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004070 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004071 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004072 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004073 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004074 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004075
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004076 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004077 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004078 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004079
Mike Stumpf5408fe2009-05-16 07:57:57 +00004080 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4081 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004082 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004083 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004084
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004085 // I'm not sure I like this. The implicit coordination is a bit
4086 // gross. We should solve this in a reasonable fashion because this
4087 // is a pretty common task (match some runtime data structure with
4088 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004089
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004090 // FIXME: This is leaked.
4091 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004092
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004093 // struct _objc_super {
4094 // id self;
4095 // Class cls;
4096 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004097 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004098 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004099 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004100 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004101 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004102 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004103 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004104 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004105 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004106
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004107 SuperCTy = Ctx.getTagDeclType(RD);
4108 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004109
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004110 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004111 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4112
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004113 // struct _prop_t {
4114 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004115 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004116 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004117 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004118 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004119 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004120
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004121 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004122 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004123 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004124 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004125 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004126 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004127 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004128 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004129 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004130 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004131 PropertyListTy);
4132 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004133 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004134
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004135 // struct _objc_method {
4136 // SEL _cmd;
4137 // char *method_type;
4138 // char *_imp;
4139 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004140 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004141 Int8PtrTy,
4142 Int8PtrTy,
4143 NULL);
4144 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004145
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004146 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004147 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004148 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004149 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004150}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004151
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004153 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004154 // struct _objc_method_description {
4155 // SEL name;
4156 // char *types;
4157 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004158 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004159 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004160 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004161 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004162 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004163 MethodDescriptionTy);
4164
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004165 // struct _objc_method_description_list {
4166 // int count;
4167 // struct _objc_method_description[1];
4168 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004169 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004170 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004171 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004172 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004174 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004175
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004176 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004178 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004179
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004180 // Protocol description structures
4181
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004182 // struct _objc_protocol_extension {
4183 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4184 // struct _objc_method_description_list *optional_instance_methods;
4185 // struct _objc_method_description_list *optional_class_methods;
4186 // struct _objc_property_list *instance_properties;
4187 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004188 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004189 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004190 MethodDescriptionListPtrTy,
4191 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004192 PropertyListPtrTy,
4193 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004194 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004195 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004196
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004197 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004198 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004199
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004200 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004201
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004202 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4203 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004204
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004205 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004206 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004207 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004208 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004209 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004210 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004211 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4212
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004213 // struct _objc_protocol {
4214 // struct _objc_protocol_extension *isa;
4215 // char *protocol_name;
4216 // struct _objc_protocol **_objc_protocol_list;
4217 // struct _objc_method_description_list *instance_methods;
4218 // struct _objc_method_description_list *class_methods;
4219 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004220 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004221 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004222 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004223 MethodDescriptionListPtrTy,
4224 MethodDescriptionListPtrTy,
4225 NULL);
4226 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4227
4228 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004231 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004232 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004233
4234 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004235 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004236 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004237
4238 // Class description structures
4239
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004240 // struct _objc_ivar {
4241 // char *ivar_name;
4242 // char *ivar_type;
4243 // int ivar_offset;
4244 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004245 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004246 Int8PtrTy,
4247 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004248 NULL);
4249 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4250
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004251 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004252 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004253 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004254 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004255
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004256 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004257 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004258 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004259 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004260
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004261 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004262 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004263 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004264 Int8PtrTy,
4265 PropertyListPtrTy,
4266 NULL);
4267 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004268 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004269
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004270 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004271
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004272 // struct _objc_class {
4273 // Class isa;
4274 // Class super_class;
4275 // char *name;
4276 // long version;
4277 // long info;
4278 // long instance_size;
4279 // struct _objc_ivar_list *ivars;
4280 // struct _objc_method_list *methods;
4281 // struct _objc_cache *cache;
4282 // struct _objc_protocol_list *protocols;
4283 // char *ivar_layout;
4284 // struct _objc_class_ext *ext;
4285 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004286 T = llvm::StructType::get(VMContext,
4287 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004288 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289 Int8PtrTy,
4290 LongTy,
4291 LongTy,
4292 LongTy,
4293 IvarListPtrTy,
4294 MethodListPtrTy,
4295 CachePtrTy,
4296 ProtocolListPtrTy,
4297 Int8PtrTy,
4298 ClassExtensionPtrTy,
4299 NULL);
4300 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004301
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004302 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4303 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004304 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004305
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004306 // struct _objc_category {
4307 // char *category_name;
4308 // char *class_name;
4309 // struct _objc_method_list *instance_method;
4310 // struct _objc_method_list *class_method;
4311 // uint32_t size; // sizeof(struct _objc_category)
4312 // struct _objc_property_list *instance_properties;// category's @property
4313 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004314 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004315 Int8PtrTy,
4316 MethodListPtrTy,
4317 MethodListPtrTy,
4318 ProtocolListPtrTy,
4319 IntTy,
4320 PropertyListPtrTy,
4321 NULL);
4322 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4323
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004324 // Global metadata structures
4325
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004326 // struct _objc_symtab {
4327 // long sel_ref_cnt;
4328 // SEL *refs;
4329 // short cls_def_cnt;
4330 // short cat_def_cnt;
4331 // char *defs[cls_def_cnt + cat_def_cnt];
4332 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004333 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004334 SelectorPtrTy,
4335 ShortTy,
4336 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004337 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004338 NULL);
4339 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004340 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004341
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004342 // struct _objc_module {
4343 // long version;
4344 // long size; // sizeof(struct _objc_module)
4345 // char *name;
4346 // struct _objc_symtab* symtab;
4347 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004348 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004349 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004350 LongTy,
4351 Int8PtrTy,
4352 SymtabPtrTy,
4353 NULL);
4354 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004355
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004356
Mike Stumpf5408fe2009-05-16 07:57:57 +00004357 // FIXME: This is the size of the setjmp buffer and should be target
4358 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004359 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004360
Anders Carlsson124526b2008-09-09 10:10:21 +00004361 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004362 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004363 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004364
4365 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004366 llvm::StructType::get(VMContext,
4367 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4368 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004369 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004370 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004371 ExceptionDataTy);
4372
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004373}
4374
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004375ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004376 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004377 // struct _method_list_t {
4378 // uint32_t entsize; // sizeof(struct _objc_method)
4379 // uint32_t method_count;
4380 // struct _objc_method method_list[method_count];
4381 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004382 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004383 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004384 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004385 NULL);
4386 CGM.getModule().addTypeName("struct.__method_list_t",
4387 MethodListnfABITy);
4388 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004389 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004390
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004391 // struct _protocol_t {
4392 // id isa; // NULL
4393 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004394 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395 // const struct method_list_t * const instance_methods;
4396 // const struct method_list_t * const class_methods;
4397 // const struct method_list_t *optionalInstanceMethods;
4398 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004399 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004400 // const uint32_t size; // sizeof(struct _protocol_t)
4401 // const uint32_t flags; // = 0
4402 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004404 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004405 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004406
Owen Anderson47a434f2009-08-05 23:18:46 +00004407 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004408 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004409 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004410 ProtocolListTyHolder),
4411 MethodListnfABIPtrTy,
4412 MethodListnfABIPtrTy,
4413 MethodListnfABIPtrTy,
4414 MethodListnfABIPtrTy,
4415 PropertyListPtrTy,
4416 IntTy,
4417 IntTy,
4418 NULL);
4419 CGM.getModule().addTypeName("struct._protocol_t",
4420 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004421
4422 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004423 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004424
Fariborz Jahanianda320092009-01-29 19:24:30 +00004425 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004426 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004427 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004428 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004429 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004430 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004431 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004432 NULL);
4433 CGM.getModule().addTypeName("struct._objc_protocol_list",
4434 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004435 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004436 ProtocolListnfABITy);
4437
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004438 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004439 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004440
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004441 // struct _ivar_t {
4442 // unsigned long int *offset; // pointer to ivar offset location
4443 // char *name;
4444 // char *type;
4445 // uint32_t alignment;
4446 // uint32_t size;
4447 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004448 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004449 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004450 Int8PtrTy,
4451 Int8PtrTy,
4452 IntTy,
4453 IntTy,
4454 NULL);
4455 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004456
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004457 // struct _ivar_list_t {
4458 // uint32 entsize; // sizeof(struct _ivar_t)
4459 // uint32 count;
4460 // struct _iver_t list[count];
4461 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004462 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004463 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004464 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004465 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004466 NULL);
4467 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468
Owen Anderson96e0fc72009-07-29 22:16:19 +00004469 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004470
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004471 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004472 // uint32_t const flags;
4473 // uint32_t const instanceStart;
4474 // uint32_t const instanceSize;
4475 // uint32_t const reserved; // only when building for 64bit targets
4476 // const uint8_t * const ivarLayout;
4477 // const char *const name;
4478 // const struct _method_list_t * const baseMethods;
4479 // const struct _objc_protocol_list *const baseProtocols;
4480 // const struct _ivar_list_t *const ivars;
4481 // const uint8_t * const weakIvarLayout;
4482 // const struct _prop_list_t * const properties;
4483 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004484
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004485 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004486 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004487 IntTy,
4488 IntTy,
4489 Int8PtrTy,
4490 Int8PtrTy,
4491 MethodListnfABIPtrTy,
4492 ProtocolListnfABIPtrTy,
4493 IvarListnfABIPtrTy,
4494 Int8PtrTy,
4495 PropertyListPtrTy,
4496 NULL);
4497 CGM.getModule().addTypeName("struct._class_ro_t",
4498 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004499
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004500 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4501 std::vector<const llvm::Type*> Params;
4502 Params.push_back(ObjectPtrTy);
4503 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004504 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004505 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4506
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004507 // struct _class_t {
4508 // struct _class_t *isa;
4509 // struct _class_t * const superclass;
4510 // void *cache;
4511 // IMP *vtable;
4512 // struct class_ro_t *ro;
4513 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004514
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004515 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004516 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004517 llvm::StructType::get(VMContext,
4518 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004519 llvm::PointerType::getUnqual(ClassTyHolder),
4520 CachePtrTy,
4521 llvm::PointerType::getUnqual(ImpnfABITy),
4522 llvm::PointerType::getUnqual(ClassRonfABITy),
4523 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004524 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4525
4526 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004527 ClassnfABITy);
4528
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004529 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004530 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004532 // struct _category_t {
4533 // const char * const name;
4534 // struct _class_t *const cls;
4535 // const struct _method_list_t * const instance_methods;
4536 // const struct _method_list_t * const class_methods;
4537 // const struct _protocol_list_t * const protocols;
4538 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004539 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004540 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004541 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004542 MethodListnfABIPtrTy,
4543 MethodListnfABIPtrTy,
4544 ProtocolListnfABIPtrTy,
4545 PropertyListPtrTy,
4546 NULL);
4547 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004548
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004549 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004550 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4551 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004553 // MessageRefTy - LLVM for:
4554 // struct _message_ref_t {
4555 // IMP messenger;
4556 // SEL name;
4557 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004558
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004559 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004560 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004561 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004562 SourceLocation(),
4563 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004564 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004565 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004566 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004567 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004568 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004569
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004570 MessageRefCTy = Ctx.getTagDeclType(RD);
4571 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4572 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004574 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004575 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004576
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004577 // SuperMessageRefTy - LLVM for:
4578 // struct _super_message_ref_t {
4579 // SUPER_IMP messenger;
4580 // SEL name;
4581 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004582 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004583 SelectorPtrTy,
4584 NULL);
4585 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004587 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004588 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4589
Daniel Dunbare588b992009-03-01 04:46:24 +00004590
4591 // struct objc_typeinfo {
4592 // const void** vtable; // objc_ehtype_vtable + 2
4593 // const char* name; // c++ typeinfo string
4594 // Class cls;
4595 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004596 EHTypeTy = llvm::StructType::get(VMContext,
4597 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004598 Int8PtrTy,
4599 ClassnfABIPtrTy,
4600 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004601 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004602 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004603}
4604
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004605llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004606 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004607
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004608 return NULL;
4609}
4610
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004611void CGObjCNonFragileABIMac::AddModuleClassList(const
4612 std::vector<llvm::GlobalValue*>
4613 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004614 const char *SymbolName,
4615 const char *SectionName) {
4616 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004617
Daniel Dunbar463b8762009-05-15 21:48:48 +00004618 if (!NumClasses)
4619 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Daniel Dunbar463b8762009-05-15 21:48:48 +00004621 std::vector<llvm::Constant*> Symbols(NumClasses);
4622 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004623 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004624 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004626 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004627 NumClasses),
4628 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629
Daniel Dunbar463b8762009-05-15 21:48:48 +00004630 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004631 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004632 llvm::GlobalValue::InternalLinkage,
4633 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004634 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004635 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004636 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004637 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004638}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004639
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004640void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4641 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004642
Daniel Dunbar463b8762009-05-15 21:48:48 +00004643 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004644 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004645 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004646 "\01L_OBJC_LABEL_CLASS_$",
4647 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004648
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004649 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4650 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4651 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4652 continue;
4653 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004654 }
4655
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004656 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4657 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4658 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4659 continue;
4660 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4661 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004662
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004664 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4665 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004666
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004667 // Build list of all implemented category addresses in array
4668 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004669 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004670 "\01L_OBJC_LABEL_CATEGORY_$",
4671 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004672 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004673 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4674 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004676 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004677}
4678
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004679/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004680/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004681/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004682/// message dispatch call for all the rest.
4683///
4684bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004685 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4686 default:
4687 assert(0 && "Invalid dispatch method!");
4688 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004689 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004690 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004691 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004692 case CodeGenOptions::Mixed:
4693 break;
4694 }
4695
4696 // If so, see whether this selector is in the white-list of things which must
4697 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004698 if (NonLegacyDispatchMethods.empty()) {
4699 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4700 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4701 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4702 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4703 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4704 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4705 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4706 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4707 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4708 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004709
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004710 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4711 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4712 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4713 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4714 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4715 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4716 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4717 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004718 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004719 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004720 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4721 &CGM.getContext().Idents.get("objects"),
4722 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004723 };
4724 NonLegacyDispatchMethods.insert(
4725 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004726 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004727
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004728 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004729}
4730
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004731// Metadata flags
4732enum MetaDataDlags {
4733 CLS = 0x0,
4734 CLS_META = 0x1,
4735 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004736 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004737 CLS_EXCEPTION = 0x20
4738};
4739/// BuildClassRoTInitializer - generate meta-data for:
4740/// struct _class_ro_t {
4741/// uint32_t const flags;
4742/// uint32_t const instanceStart;
4743/// uint32_t const instanceSize;
4744/// uint32_t const reserved; // only when building for 64bit targets
4745/// const uint8_t * const ivarLayout;
4746/// const char *const name;
4747/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004748/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004749/// const struct _ivar_list_t *const ivars;
4750/// const uint8_t * const weakIvarLayout;
4751/// const struct _prop_list_t * const properties;
4752/// }
4753///
4754llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004755 unsigned flags,
4756 unsigned InstanceStart,
4757 unsigned InstanceSize,
4758 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004759 std::string ClassName = ID->getNameAsString();
4760 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004761 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4762 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4763 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004764 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004765 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4766 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004767 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004768 // const struct _method_list_t * const baseMethods;
4769 std::vector<llvm::Constant*> Methods;
4770 std::string MethodListName("\01l_OBJC_$_");
4771 if (flags & CLS_META) {
4772 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004773 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004774 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004775 // Class methods should always be defined.
4776 Methods.push_back(GetMethodConstant(*i));
4777 }
4778 } else {
4779 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004780 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004781 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004782 // Instance methods should always be defined.
4783 Methods.push_back(GetMethodConstant(*i));
4784 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004786 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004787 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004789 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4790 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004791
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004792 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4793 if (llvm::Constant *C = GetMethodConstant(MD))
4794 Methods.push_back(C);
4795 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4796 if (llvm::Constant *C = GetMethodConstant(MD))
4797 Methods.push_back(C);
4798 }
4799 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004800 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004801 Values[ 5] = EmitMethodList(MethodListName,
4802 "__DATA, __objc_const", Methods);
4803
Fariborz Jahanianda320092009-01-29 19:24:30 +00004804 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4805 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004806 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004807 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004808 OID->all_referenced_protocol_begin(),
4809 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004810
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004811 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004812 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004813 else
4814 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004815 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4816 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004817 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004818 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004819 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004820 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4821 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004822 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004823 Values);
4824 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004825 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4826 llvm::GlobalValue::InternalLinkage,
4827 Init,
4828 (flags & CLS_META) ?
4829 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4830 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004831 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004832 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004833 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004834 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004835
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004836}
4837
4838/// BuildClassMetaData - This routine defines that to-level meta-data
4839/// for the given ClassName for:
4840/// struct _class_t {
4841/// struct _class_t *isa;
4842/// struct _class_t * const superclass;
4843/// void *cache;
4844/// IMP *vtable;
4845/// struct class_ro_t *ro;
4846/// }
4847///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004848llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004849 std::string &ClassName,
4850 llvm::Constant *IsAGV,
4851 llvm::Constant *SuperClassGV,
4852 llvm::Constant *ClassRoGV,
4853 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004854 std::vector<llvm::Constant*> Values(5);
4855 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856 Values[1] = SuperClassGV;
4857 if (!Values[1])
4858 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004859 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4860 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4861 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004863 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004864 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4865 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004866 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004867 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004868 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004869 if (HiddenVisibility)
4870 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004871 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004872}
4873
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004874bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004875CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004876 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004877}
4878
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004879void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004880 uint32_t &InstanceStart,
4881 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004883 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004884
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004885 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004886 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004887
4888 // If there are no fields, the start is the same as the end.
4889 if (!RL.getFieldCount())
4890 InstanceStart = InstanceSize;
4891 else
4892 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004893}
4894
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004895void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4896 std::string ClassName = ID->getNameAsString();
4897 if (!ObjCEmptyCacheVar) {
4898 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004899 CGM.getModule(),
4900 ObjCTypes.CacheTy,
4901 false,
4902 llvm::GlobalValue::ExternalLinkage,
4903 0,
4904 "_objc_empty_cache");
4905
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004906 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004907 CGM.getModule(),
4908 ObjCTypes.ImpnfABITy,
4909 false,
4910 llvm::GlobalValue::ExternalLinkage,
4911 0,
4912 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004913 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004914 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004915 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004916 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004917 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004918 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004919 uint32_t InstanceSize = InstanceStart;
4920 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004921 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4922 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004924 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004925
4926 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004927 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004928 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004929 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004930 if (ID->getNumIvarInitializers())
4931 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004932 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004933 // class is root
4934 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004935 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004936 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004937 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004938 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004939 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4940 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4941 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004942 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004943 if (Root->hasAttr<WeakImportAttr>())
4944 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004945 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004946 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004947 ObjCMetaClassName +
4948 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004949 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004950 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4951 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004952 }
4953 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4954 InstanceStart,
4955 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004956 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004957 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004958 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4959 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004960 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004961
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004962 // Metadata for the class
4963 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004964 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004965 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004966 if (ID->getNumIvarInitializers())
4967 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004968
Douglas Gregor68584ed2009-06-18 16:11:24 +00004969 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004970 flags |= CLS_EXCEPTION;
4971
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004972 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004973 flags |= CLS_ROOT;
4974 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004975 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004976 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004977 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004978 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004979 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004980 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4981 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004982 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004983 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004984 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004985 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004987 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004988
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004989 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004990 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004991 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4992 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004993 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004994
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004995 // Determine if this class is also "non-lazy".
4996 if (ImplementationIsNonLazy(ID))
4997 DefinedNonLazyClasses.push_back(ClassMD);
4998
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004999 // Force the definition of the EHType if necessary.
5000 if (flags & CLS_EXCEPTION)
5001 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005002}
5003
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005004/// GenerateProtocolRef - This routine is called to generate code for
5005/// a protocol reference expression; as in:
5006/// @code
5007/// @protocol(Proto1);
5008/// @endcode
5009/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5010/// which will hold address of the protocol meta-data.
5011///
5012llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 const ObjCProtocolDecl *PD) {
5014
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005015 // This routine is called for @protocol only. So, we must build definition
5016 // of protocol's meta-data (not a reference to it!)
5017 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005018 llvm::Constant *Init =
5019 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5020 ObjCTypes.ExternalProtocolPtrTy);
5021
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005022 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005023 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005024
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005025 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5026 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005027 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005028 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029 CGM.getModule(),
5030 Init->getType(), false,
5031 llvm::GlobalValue::WeakAnyLinkage,
5032 Init,
5033 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005034 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5035 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005036 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005037 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005038}
5039
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005040/// GenerateCategory - Build metadata for a category implementation.
5041/// struct _category_t {
5042/// const char * const name;
5043/// struct _class_t *const cls;
5044/// const struct _method_list_t * const instance_methods;
5045/// const struct _method_list_t * const class_methods;
5046/// const struct _protocol_list_t * const protocols;
5047/// const struct _prop_list_t * const properties;
5048/// }
5049///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005050void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005051 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005052 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5054 "_$_" + OCD->getNameAsString());
5055 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005056 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005057
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005058 std::vector<llvm::Constant*> Values(6);
5059 Values[0] = GetClassName(OCD->getIdentifier());
5060 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005061 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005062 if (Interface->hasAttr<WeakImportAttr>())
5063 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5064
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005065 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005066 std::vector<llvm::Constant*> Methods;
5067 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005068 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005069 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005070
5071 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005072 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005073 // Instance methods should always be defined.
5074 Methods.push_back(GetMethodConstant(*i));
5075 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005076
5077 Values[2] = EmitMethodList(MethodListName,
5078 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005079 Methods);
5080
5081 MethodListName = Prefix;
5082 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5083 OCD->getNameAsString();
5084 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005086 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005087 // Class methods should always be defined.
5088 Methods.push_back(GetMethodConstant(*i));
5089 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005090
5091 Values[3] = EmitMethodList(MethodListName,
5092 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005093 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005094 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005095 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005096 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005097 llvm::SmallString<256> ExtName;
5098 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5099 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005100 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005101 + Interface->getName() + "_$_"
5102 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005103 Category->protocol_begin(),
5104 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005105 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5106 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005107 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005108 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5109 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005110 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005111
5112 llvm::Constant *Init =
5113 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005114 Values);
5115 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005116 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005117 false,
5118 llvm::GlobalValue::InternalLinkage,
5119 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005120 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005121 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005122 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005123 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005124 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005125 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005126
5127 // Determine if this category is also "non-lazy".
5128 if (ImplementationIsNonLazy(OCD))
5129 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005130}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005131
5132/// GetMethodConstant - Return a struct objc_method constant for the
5133/// given method if it has been defined. The result is null if the
5134/// method has not been defined. The return value has type MethodPtrTy.
5135llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005137 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005138 if (!Fn)
5139 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005140
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005141 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005142 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005143 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005145 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005146 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005147 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005148}
5149
5150/// EmitMethodList - Build meta-data for method declarations
5151/// struct _method_list_t {
5152/// uint32_t entsize; // sizeof(struct _objc_method)
5153/// uint32_t method_count;
5154/// struct _objc_method method_list[method_count];
5155/// }
5156///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005157llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5158 const char *Section,
5159 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005160 // Return null for empty list.
5161 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005162 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005163
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005164 std::vector<llvm::Constant*> Values(3);
5165 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005166 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005167 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005168 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005169 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005170 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005171 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005172 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005173 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005174
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005175 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005176 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005177 llvm::GlobalValue::InternalLinkage,
5178 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005179 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005180 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005181 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005182 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005183 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005184 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005185 ObjCTypes.MethodListnfABIPtrTy);
5186}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005187
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005188/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5189/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005190llvm::GlobalVariable *
5191CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5192 const ObjCIvarDecl *Ivar) {
5193 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005194 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005195 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005196 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005197 CGM.getModule().getGlobalVariable(Name);
5198 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005199 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005200 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005201 false,
5202 llvm::GlobalValue::ExternalLinkage,
5203 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005204 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005205 return IvarOffsetGV;
5206}
5207
Daniel Dunbare83be122010-04-02 21:14:02 +00005208llvm::Constant *
5209CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5210 const ObjCIvarDecl *Ivar,
5211 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005212 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005213 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005214 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005215 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005216 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005217
Mike Stumpf5408fe2009-05-16 07:57:57 +00005218 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5219 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005220 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5221 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5222 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005223 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005224 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005225 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005226 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005227 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005228}
5229
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005230/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005231/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005232/// IvarListnfABIPtrTy.
5233/// struct _ivar_t {
5234/// unsigned long int *offset; // pointer to ivar offset location
5235/// char *name;
5236/// char *type;
5237/// uint32_t alignment;
5238/// uint32_t size;
5239/// }
5240/// struct _ivar_list_t {
5241/// uint32 entsize; // sizeof(struct _ivar_t)
5242/// uint32 count;
5243/// struct _iver_t list[count];
5244/// }
5245///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005246
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005247llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005248 const ObjCImplementationDecl *ID) {
5249
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005250 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005251
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005252 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5253 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005254
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005255 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005256
Daniel Dunbar91636d62009-04-20 00:33:43 +00005257 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005258 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005259 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005260
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005261 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5262 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005263 // Ignore unnamed bit-fields.
5264 if (!IVD->getDeclName())
5265 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005266 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005267 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005268 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5269 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005270 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005271 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005272 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005273 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005274 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005275 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005276 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005277 // NOTE. Size of a bitfield does not match gcc's, because of the
5278 // way bitfields are treated special in each. But I am told that
5279 // 'size' for bitfield ivars is ignored by the runtime so it does
5280 // not matter. If it matters, there is enough info to get the
5281 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005282 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005283 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005284 }
5285 // Return null for empty list.
5286 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005287 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005288 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005289 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005290 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5291 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005292 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005293 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005294 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005295 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005296 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5297 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005298 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005299 llvm::GlobalValue::InternalLinkage,
5300 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005301 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005302 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005303 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005304 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005305
Chris Lattnerad64e022009-07-17 23:57:13 +00005306 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005307 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005308}
5309
5310llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005312 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005313
Fariborz Jahanianda320092009-01-29 19:24:30 +00005314 if (!Entry) {
5315 // We use the initializer as a marker of whether this is a forward
5316 // reference or not. At module finalization we add the empty
5317 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318 Entry =
5319 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5320 llvm::GlobalValue::ExternalLinkage,
5321 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005322 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005323 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005324 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005325
Fariborz Jahanianda320092009-01-29 19:24:30 +00005326 return Entry;
5327}
5328
5329/// GetOrEmitProtocol - Generate the protocol meta-data:
5330/// @code
5331/// struct _protocol_t {
5332/// id isa; // NULL
5333/// const char * const protocol_name;
5334/// const struct _protocol_list_t * protocol_list; // super protocols
5335/// const struct method_list_t * const instance_methods;
5336/// const struct method_list_t * const class_methods;
5337/// const struct method_list_t *optionalInstanceMethods;
5338/// const struct method_list_t *optionalClassMethods;
5339/// const struct _prop_list_t * properties;
5340/// const uint32_t size; // sizeof(struct _protocol_t)
5341/// const uint32_t flags; // = 0
5342/// }
5343/// @endcode
5344///
5345
5346llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005348 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005349
Fariborz Jahanianda320092009-01-29 19:24:30 +00005350 // Early exit if a defining object has already been generated.
5351 if (Entry && Entry->hasInitializer())
5352 return Entry;
5353
Fariborz Jahanianda320092009-01-29 19:24:30 +00005354 // Construct method lists.
5355 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5356 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005358 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005359 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005360 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005361 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5362 OptInstanceMethods.push_back(C);
5363 } else {
5364 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367
5368 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005369 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005370 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005371 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005372 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5373 OptClassMethods.push_back(C);
5374 } else {
5375 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005376 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005377 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 std::vector<llvm::Constant*> Values(10);
5380 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005381 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005382 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005383 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5384 PD->protocol_begin(),
5385 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005387 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005388 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005389 "__DATA, __objc_const",
5390 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005391 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005392 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 "__DATA, __objc_const",
5394 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005395 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005396 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005397 "__DATA, __objc_const",
5398 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005399 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005400 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005401 "__DATA, __objc_const",
5402 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005403 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005405 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005406 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005407 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005408 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005409 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005411
Fariborz Jahanianda320092009-01-29 19:24:30 +00005412 if (Entry) {
5413 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005414 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 Entry->setInitializer(Init);
5416 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005418 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5419 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5420 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005421 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005422 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005423 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005424 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005425 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005426 CGM.AddUsedGlobal(Entry);
5427
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005428 // Use this protocol meta-data to build protocol list table in section
5429 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005430 llvm::GlobalVariable *PTGV =
5431 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5432 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5433 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005434 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005435 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005436 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005437 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005438 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005439 return Entry;
5440}
5441
5442/// EmitProtocolList - Generate protocol list meta-data:
5443/// @code
5444/// struct _protocol_list_t {
5445/// long protocol_count; // Note, this is 32/64 bit
5446/// struct _protocol_t[protocol_count];
5447/// }
5448/// @endcode
5449///
5450llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005451CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5452 ObjCProtocolDecl::protocol_iterator begin,
5453 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005454 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455
Fariborz Jahanianda320092009-01-29 19:24:30 +00005456 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005457 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005458 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459
Daniel Dunbar948e2582009-02-15 07:36:20 +00005460 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005461 llvm::SmallString<256> TmpName;
5462 Name.toVector(TmpName);
5463 llvm::GlobalVariable *GV =
5464 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005466 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467
Daniel Dunbar948e2582009-02-15 07:36:20 +00005468 for (; begin != end; ++begin)
5469 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5470
Fariborz Jahanianda320092009-01-29 19:24:30 +00005471 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005472 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 ObjCTypes.ProtocolnfABIPtrTy));
5474
Fariborz Jahanianda320092009-01-29 19:24:30 +00005475 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005476 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005477 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005479 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005480 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005481 ProtocolRefs.size()),
5482 ProtocolRefs);
5483
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005484 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005485 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005486 llvm::GlobalValue::InternalLinkage,
5487 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005488 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005489 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005490 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005491 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005492 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005493 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005494 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005495}
5496
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005497/// GetMethodDescriptionConstant - This routine build following meta-data:
5498/// struct _objc_method {
5499/// SEL _cmd;
5500/// char *method_type;
5501/// char *_imp;
5502/// }
5503
5504llvm::Constant *
5505CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5506 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005507 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005508 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5509 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005510 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005511 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005512 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005513 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005514}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005515
5516/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5517/// This code gen. amounts to generating code for:
5518/// @code
5519/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5520/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005521///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005522LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005523 CodeGen::CodeGenFunction &CGF,
5524 QualType ObjectTy,
5525 llvm::Value *BaseValue,
5526 const ObjCIvarDecl *Ivar,
5527 unsigned CVRQualifiers) {
5528 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005529 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5530 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005531}
5532
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005533llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005534 CodeGen::CodeGenFunction &CGF,
5535 const ObjCInterfaceDecl *Interface,
5536 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005537 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005538}
5539
Fariborz Jahanian46551122009-02-04 00:22:57 +00005540CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005542 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005543 QualType ResultType,
5544 Selector Sel,
5545 llvm::Value *Receiver,
5546 QualType Arg0Ty,
5547 bool IsSuper,
5548 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005549 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5550 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005551 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005552 llvm::Value *Arg0 = Receiver;
5553 if (!IsSuper)
5554 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005555
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005556 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005557 // FIXME. This is too much work to get the ABI-specific result type needed to
5558 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005559 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005560 = Types.getFunctionInfo(ResultType, CallArgList(),
5561 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005562 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005563 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005564 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005565 if (IsSuper) {
5566 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5567 Name += "objc_msgSendSuper2_stret_fixup";
5568 } else {
5569 Fn = ObjCTypes.getMessageSendStretFixupFn();
5570 Name += "objc_msgSend_stret_fixup";
5571 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005572 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5573 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5574 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005575 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005576 if (IsSuper) {
5577 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5578 Name += "objc_msgSendSuper2_fixup";
5579 } else {
5580 Fn = ObjCTypes.getMessageSendFixupFn();
5581 Name += "objc_msgSend_fixup";
5582 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005583 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005584 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005585 Name += '_';
5586 std::string SelName(Sel.getAsString());
5587 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005588 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005589 if (SelName[i] == ':')
5590 SelName[i] = '_';
5591 Name += SelName;
5592 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5593 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005594 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005595 std::vector<llvm::Constant*> Values(2);
5596 Values[0] = Fn;
5597 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005598 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005599 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005600 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005601 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005602 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005603 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005604 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005605 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005606 }
5607 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005608
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005609 CallArgList ActualArgs;
5610 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005612 ObjCTypes.MessageRefCPtrTy));
5613 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005614 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005615 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005616 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5617 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005618 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005619 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005620 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005621 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005622}
5623
5624/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005625CodeGen::RValue
5626CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005627 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005628 QualType ResultType,
5629 Selector Sel,
5630 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005631 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005632 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005633 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005634 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005635 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5636 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005637 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005638 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005639 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005640 Receiver, CGF.getContext().getObjCIdType(),
5641 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005642}
5643
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005644llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005645CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005646 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5647
Daniel Dunbardfff2302009-03-02 05:18:14 +00005648 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005649 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005650 false, llvm::GlobalValue::ExternalLinkage,
5651 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005652 }
5653
5654 return GV;
5655}
5656
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5658 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005659 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005660
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005661 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005662 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005663 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005664 Entry =
5665 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005666 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005668 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005669 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005670 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005671 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005672 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005673 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005674 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005676 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005677}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005678
Daniel Dunbar11394522009-04-18 08:51:00 +00005679llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005681 const ObjCInterfaceDecl *ID) {
5682 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005683
Daniel Dunbar11394522009-04-18 08:51:00 +00005684 if (!Entry) {
5685 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5686 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005687 Entry =
5688 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005689 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005690 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005691 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005692 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005693 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005694 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005695 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005696 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005697 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005698
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005699 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005700}
5701
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005702/// EmitMetaClassRef - Return a Value * of the address of _class_t
5703/// meta-data
5704///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5706 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005707 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5708 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005709 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005711 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005712 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005713 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005714 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005715 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005717 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005718 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005719 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005720 ObjCTypes.ClassnfABIPtrTy));
5721
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005722 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005723 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005724
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005725 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005726}
5727
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005728/// GetClass - Return a reference to the class for the given interface
5729/// decl.
5730llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5731 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005732 if (ID->hasAttr<WeakImportAttr>()) {
5733 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5734 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5735 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5736 }
5737
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005738 return EmitClassRef(Builder, ID);
5739}
5740
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005741/// Generates a message send where the super is the receiver. This is
5742/// a message send to self with special delivery semantics indicating
5743/// which class's method should be called.
5744CodeGen::RValue
5745CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005746 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005747 QualType ResultType,
5748 Selector Sel,
5749 const ObjCInterfaceDecl *Class,
5750 bool isCategoryImpl,
5751 llvm::Value *Receiver,
5752 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005753 const CodeGen::CallArgList &CallArgs,
5754 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005755 // ...
5756 // Create and init a super structure; this is a (receiver, class)
5757 // pair we will pass to objc_msgSendSuper.
5758 llvm::Value *ObjCSuper =
5759 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005760
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005761 llvm::Value *ReceiverAsObject =
5762 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5763 CGF.Builder.CreateStore(ReceiverAsObject,
5764 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005765
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005766 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005767 llvm::Value *Target;
5768 if (IsClassMessage) {
5769 if (isCategoryImpl) {
5770 // Message sent to "super' in a class method defined in
5771 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005772 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005773 Target = CGF.Builder.CreateStructGEP(Target, 0);
5774 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005775 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005776 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005777 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005778 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005779
Mike Stumpf5408fe2009-05-16 07:57:57 +00005780 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5781 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005782 const llvm::Type *ClassTy =
5783 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5784 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5785 CGF.Builder.CreateStore(Target,
5786 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005787
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005788 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005789 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5790 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005791 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005792 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005793 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005794 ObjCSuper, ObjCTypes.SuperPtrCTy,
5795 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005796}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005797
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005798llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005799 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005800 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005801
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005802 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005803 llvm::Constant *Casted =
5804 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5805 ObjCTypes.SelectorPtrTy);
5806 Entry =
5807 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5808 llvm::GlobalValue::InternalLinkage,
5809 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005810 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005811 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005812 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005813
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005814 if (lval)
5815 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005816 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005817}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005818/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005819/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005820///
5821void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005822 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005823 llvm::Value *dst,
5824 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005825 const llvm::Type * SrcTy = src->getType();
5826 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005827 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005828 assert(Size <= 8 && "does not support size > 8");
5829 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5830 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005831 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5832 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005833 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5834 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005835 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5836 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005837 return;
5838}
5839
5840/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5841/// objc_assign_strongCast (id src, id *dst)
5842///
5843void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005845 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005846 const llvm::Type * SrcTy = src->getType();
5847 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005848 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005849 assert(Size <= 8 && "does not support size > 8");
5850 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005851 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005852 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5853 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005854 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5855 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005856 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005857 src, dst, "weakassign");
5858 return;
5859}
5860
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005861void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005862 CodeGen::CodeGenFunction &CGF,
5863 llvm::Value *DestPtr,
5864 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005865 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005866 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5867 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005868 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005869 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005870 return;
5871}
5872
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873/// EmitObjCWeakRead - Code gen for loading value of a __weak
5874/// object: objc_read_weak (id *src)
5875///
5876llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005877 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005878 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005879 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005880 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5881 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005882 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005883 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005884 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005885 return read_weak;
5886}
5887
5888/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5889/// objc_assign_weak (id src, id *dst)
5890///
5891void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005892 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005893 const llvm::Type * SrcTy = src->getType();
5894 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005895 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005896 assert(Size <= 8 && "does not support size > 8");
5897 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5898 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005899 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5900 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005901 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5902 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005903 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005904 src, dst, "weakassign");
5905 return;
5906}
5907
5908/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5909/// objc_assign_global (id src, id *dst)
5910///
5911void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005912 llvm::Value *src, llvm::Value *dst,
5913 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005914 const llvm::Type * SrcTy = src->getType();
5915 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005916 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005917 assert(Size <= 8 && "does not support size > 8");
5918 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5919 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005920 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5921 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005922 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5923 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005924 if (!threadlocal)
5925 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5926 src, dst, "globalassign");
5927 else
5928 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5929 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005930 return;
5931}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005932
John McCall740e8072010-07-21 00:41:47 +00005933namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005934 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005935 llvm::Value *SyncExitFn;
5936 llvm::Value *SyncArg;
5937 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5938 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5939
5940 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5941 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5942 }
5943 };
5944}
5945
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005946void
John McCallf1549f62010-07-06 01:34:17 +00005947CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5948 const ObjCAtSynchronizedStmt &S) {
5949 // Evaluate the lock operand. This should dominate the cleanup.
5950 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005951
John McCallf1549f62010-07-06 01:34:17 +00005952 // Acquire the lock.
5953 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5954 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5955 ->setDoesNotThrow();
5956
5957 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005958 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5959 ObjCTypes.getSyncExitFn(),
5960 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005961
John McCallf1549f62010-07-06 01:34:17 +00005962 // Emit the body of the statement.
5963 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005964
John McCallf1549f62010-07-06 01:34:17 +00005965 // Pop the lock-release cleanup.
5966 CGF.PopCleanupBlock();
5967}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005968
John McCallf1549f62010-07-06 01:34:17 +00005969namespace {
5970 struct CatchHandler {
5971 const VarDecl *Variable;
5972 const Stmt *Body;
5973 llvm::BasicBlock *Block;
5974 llvm::Value *TypeInfo;
5975 };
John McCall8e3f8612010-07-13 22:12:14 +00005976
John McCall1f0fca52010-07-21 07:22:38 +00005977 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005978 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5979 MightThrow(MightThrow), Fn(Fn) {}
5980 bool MightThrow;
5981 llvm::Value *Fn;
5982
5983 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5984 if (!MightThrow) {
5985 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5986 return;
5987 }
5988
5989 CGF.EmitCallOrInvoke(Fn, 0, 0);
5990 }
5991 };
John McCallf1549f62010-07-06 01:34:17 +00005992}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005993
John McCall5a180392010-07-24 00:37:23 +00005994llvm::Constant *
5995CGObjCNonFragileABIMac::GetEHType(QualType T) {
5996 // There's a particular fixed type info for 'id'.
5997 if (T->isObjCIdType() ||
5998 T->isObjCQualifiedIdType()) {
5999 llvm::Constant *IDEHType =
6000 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6001 if (!IDEHType)
6002 IDEHType =
6003 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6004 false,
6005 llvm::GlobalValue::ExternalLinkage,
6006 0, "OBJC_EHTYPE_id");
6007 return IDEHType;
6008 }
6009
6010 // All other types should be Objective-C interface pointer types.
6011 const ObjCObjectPointerType *PT =
6012 T->getAs<ObjCObjectPointerType>();
6013 assert(PT && "Invalid @catch type.");
6014 const ObjCInterfaceType *IT = PT->getInterfaceType();
6015 assert(IT && "Invalid @catch type.");
6016 return GetInterfaceEHType(IT->getDecl(), false);
6017}
6018
John McCallf1549f62010-07-06 01:34:17 +00006019void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6020 const ObjCAtTryStmt &S) {
6021 // Jump destination for falling out of catch bodies.
6022 CodeGenFunction::JumpDest Cont;
6023 if (S.getNumCatchStmts())
6024 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006025
John McCallf1549f62010-07-06 01:34:17 +00006026 CodeGenFunction::FinallyInfo FinallyInfo;
6027 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6028 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6029 ObjCTypes.getObjCBeginCatchFn(),
6030 ObjCTypes.getObjCEndCatchFn(),
6031 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006032
John McCallf1549f62010-07-06 01:34:17 +00006033 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006034
John McCallf1549f62010-07-06 01:34:17 +00006035 // Enter the catch, if there is one.
6036 if (S.getNumCatchStmts()) {
6037 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6038 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006039 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006040
John McCallf1549f62010-07-06 01:34:17 +00006041 Handlers.push_back(CatchHandler());
6042 CatchHandler &Handler = Handlers.back();
6043 Handler.Variable = CatchDecl;
6044 Handler.Body = CatchStmt->getCatchBody();
6045 Handler.Block = CGF.createBasicBlock("catch");
6046
6047 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006048 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006049 Handler.TypeInfo = 0; // catch-all
6050 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006051 break;
6052 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006053
John McCall5a180392010-07-24 00:37:23 +00006054 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006055 }
John McCallf1549f62010-07-06 01:34:17 +00006056
6057 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6058 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6059 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006060 }
John McCallf1549f62010-07-06 01:34:17 +00006061
6062 // Emit the try body.
6063 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006064
John McCallf1549f62010-07-06 01:34:17 +00006065 // Leave the try.
6066 if (S.getNumCatchStmts())
6067 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006068
John McCallf1549f62010-07-06 01:34:17 +00006069 // Remember where we were.
6070 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006071
John McCallf1549f62010-07-06 01:34:17 +00006072 // Emit the handlers.
6073 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6074 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006075
John McCallf1549f62010-07-06 01:34:17 +00006076 CGF.EmitBlock(Handler.Block);
6077 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006078
John McCallf1549f62010-07-06 01:34:17 +00006079 // Enter the catch.
6080 llvm::CallInst *Exn =
6081 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6082 "exn.adjusted");
6083 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006084
John McCallf1549f62010-07-06 01:34:17 +00006085 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006086 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006087 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6088 EndCatchMightThrow,
6089 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006090
John McCallf1549f62010-07-06 01:34:17 +00006091 // Bind the catch parameter if it exists.
6092 if (const VarDecl *CatchParam = Handler.Variable) {
6093 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6094 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006095
John McCallf1549f62010-07-06 01:34:17 +00006096 CGF.EmitLocalBlockVarDecl(*CatchParam);
6097 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006098 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006099
John McCallf1549f62010-07-06 01:34:17 +00006100 CGF.ObjCEHValueStack.push_back(Exn);
6101 CGF.EmitStmt(Handler.Body);
6102 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006103
John McCallf1549f62010-07-06 01:34:17 +00006104 // Leave the earlier cleanup.
6105 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006106
John McCallf1549f62010-07-06 01:34:17 +00006107 CGF.EmitBranchThroughCleanup(Cont);
6108 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006109
John McCallf1549f62010-07-06 01:34:17 +00006110 // Go back to the try-statement fallthrough.
6111 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006112
John McCallf1549f62010-07-06 01:34:17 +00006113 // Pop out of the normal cleanup on the finally.
6114 if (S.getFinallyStmt())
6115 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006116
John McCallff8e1152010-07-23 21:56:41 +00006117 if (Cont.isValid())
6118 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006119}
6120
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006121/// EmitThrowStmt - Generate code for a throw statement.
6122void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6123 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006124 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006125 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006126 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006127 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006128 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006129 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006130 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006131 "Unexpected rethrow outside @catch block.");
6132 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006133 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006134 }
6135
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006136 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006137 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6138 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6139 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006140 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006141 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006142 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006143 } else {
6144 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6145 ->setDoesNotReturn();
6146 CGF.Builder.CreateUnreachable();
6147 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006148
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006149 // Clear the insertion point to indicate we are in unreachable code.
6150 CGF.Builder.ClearInsertionPoint();
6151}
Daniel Dunbare588b992009-03-01 04:46:24 +00006152
John McCall5a180392010-07-24 00:37:23 +00006153llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006154CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006155 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006156 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006157
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006158 // If we don't need a definition, return the entry if found or check
6159 // if we use an external reference.
6160 if (!ForDefinition) {
6161 if (Entry)
6162 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006163
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006164 // If this type (or a super class) has the __objc_exception__
6165 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006166 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006167 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006168 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006169 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006170 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006171 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006172 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006173 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006174
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006175 // Otherwise we need to either make a new entry or fill in the
6176 // initializer.
6177 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006178 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006179 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006180 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006181 CGM.getModule().getGlobalVariable(VTableName);
6182 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006183 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6184 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006185 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006186 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006187
Chris Lattner77b89b82010-06-27 07:15:29 +00006188 llvm::Value *VTableIdx =
6189 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006190
6191 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006192 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006193 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006194 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006195 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006196 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006197
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006198 if (Entry) {
6199 Entry->setInitializer(Init);
6200 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006201 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006202 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006203 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006204 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006205 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006206 }
6207
Daniel Dunbar04d40782009-04-14 06:00:08 +00006208 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006209 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006210 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6211 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006212
6213 if (ForDefinition) {
6214 Entry->setSection("__DATA,__objc_const");
6215 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6216 } else {
6217 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6218 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006219
6220 return Entry;
6221}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006222
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006223/* *** */
6224
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006225CodeGen::CGObjCRuntime *
6226CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006227 return new CGObjCMac(CGM);
6228}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006229
6230CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006231CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006232 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006233}