blob: f4ff951be27cdb6b6203135e7f74f661bb339dac [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 Dunbardbc93372008-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 Dunbardbc93372008-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 Dunbardbc93372008-08-21 21:57:41 +00001912llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001913CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-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 Dunbardbc93372008-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)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001993 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1994 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001995 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1996 ObjCTypes);
1997 }
1998 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1999 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2000 E = CD->protocol_end(); P != E; ++P)
2001 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2002 ObjCTypes);
2003 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002004
2005 // Return null for empty list.
2006 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002007 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002008
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002009 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002010 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002011 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002012 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2013 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002014 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002015 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002016 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002017 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002018
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002019 llvm::GlobalVariable *GV =
2020 CreateMetadataVar(Name, Init,
2021 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002022 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002023 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002024 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002025 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002026}
2027
2028/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002029 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002030 int count;
2031 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002032 };
2033*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002034llvm::Constant *
2035CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2036 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002037 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002038 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2039 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002040 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002041 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002042 Desc);
2043}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002044
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002045llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002046 const char *Section,
2047 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002048 // Return null for empty list.
2049 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002050 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002051
2052 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002053 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002054 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002055 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002056 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002057 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002058
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002059 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002060 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002061 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002062}
2063
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002064/*
2065 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002066 char *category_name;
2067 char *class_name;
2068 struct _objc_method_list *instance_methods;
2069 struct _objc_method_list *class_methods;
2070 struct _objc_protocol_list *protocols;
2071 uint32_t size; // <rdar://4585769>
2072 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002073 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002074*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002075void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002076 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002077
Mike Stumpf5408fe2009-05-16 07:57:57 +00002078 // FIXME: This is poor design, the OCD should have a pointer to the category
2079 // decl. Additionally, note that Category can be null for the @implementation
2080 // w/o an @interface case. Sema should just create one for us as it does for
2081 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002082 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002083 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002084 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002085
2086 llvm::SmallString<256> ExtName;
2087 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2088 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002089
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002090 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002091 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002092 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002093 // Instance methods should always be defined.
2094 InstanceMethods.push_back(GetMethodConstant(*i));
2095 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002096 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002097 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002098 // Class methods should always be defined.
2099 ClassMethods.push_back(GetMethodConstant(*i));
2100 }
2101
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002102 std::vector<llvm::Constant*> Values(7);
2103 Values[0] = GetClassName(OCD->getIdentifier());
2104 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002105 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002107 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002108 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002109 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002110 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002111 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002112 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002113 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002114 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002115 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002116 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002117 Category->protocol_begin(),
2118 Category->protocol_end());
2119 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002120 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002121 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002122 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002123
2124 // If there is no category @interface then there can be no properties.
2125 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002126 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002127 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002128 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002129 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002131
Owen Anderson08e25242009-07-27 22:29:56 +00002132 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002133 Values);
2134
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002135 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002136 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002137 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002138 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002139 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002140 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002141}
2142
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002143// FIXME: Get from somewhere?
2144enum ClassFlags {
2145 eClassFlags_Factory = 0x00001,
2146 eClassFlags_Meta = 0x00002,
2147 // <rdr://5142207>
2148 eClassFlags_HasCXXStructors = 0x02000,
2149 eClassFlags_Hidden = 0x20000,
2150 eClassFlags_ABI2_Hidden = 0x00010,
2151 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2152};
2153
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002154/*
2155 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002156 Class isa;
2157 Class super_class;
2158 const char *name;
2159 long version;
2160 long info;
2161 long instance_size;
2162 struct _objc_ivar_list *ivars;
2163 struct _objc_method_list *methods;
2164 struct _objc_cache *cache;
2165 struct _objc_protocol_list *protocols;
2166 // Objective-C 1.0 extensions (<rdr://4585769>)
2167 const char *ivar_layout;
2168 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002169 };
2170
2171 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002172*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002174 DefinedSymbols.insert(ID->getIdentifier());
2175
Chris Lattner8ec03f52008-11-24 03:54:41 +00002176 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002178 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002180 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002181 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002182 Interface->protocol_begin(),
2183 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002185 if (ID->getNumIvarInitializers())
2186 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002187 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002188 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002189
2190 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002191 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002192 Flags |= eClassFlags_Hidden;
2193
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002194 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002195 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002196 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002197 // Instance methods should always be defined.
2198 InstanceMethods.push_back(GetMethodConstant(*i));
2199 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002200 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002201 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002202 // Class methods should always be defined.
2203 ClassMethods.push_back(GetMethodConstant(*i));
2204 }
2205
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002206 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002207 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002208 ObjCPropertyImplDecl *PID = *i;
2209
2210 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2211 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2212
2213 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2214 if (llvm::Constant *C = GetMethodConstant(MD))
2215 InstanceMethods.push_back(C);
2216 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2217 if (llvm::Constant *C = GetMethodConstant(MD))
2218 InstanceMethods.push_back(C);
2219 }
2220 }
2221
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002223 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002225 // Record a reference to the super class.
2226 LazySymbols.insert(Super->getIdentifier());
2227
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002228 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002229 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230 ObjCTypes.ClassPtrTy);
2231 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002232 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233 }
2234 Values[ 2] = GetClassName(ID->getIdentifier());
2235 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002236 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2237 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2238 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002239 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002240 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002241 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002242 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002243 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002245 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002247 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002248 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002249 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002251 std::string Name("\01L_OBJC_CLASS_");
2252 Name += ClassName;
2253 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2254 // Check for a forward reference.
2255 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2256 if (GV) {
2257 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2258 "Forward metaclass reference has incorrect type.");
2259 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2260 GV->setInitializer(Init);
2261 GV->setSection(Section);
2262 GV->setAlignment(4);
2263 CGM.AddUsedGlobal(GV);
2264 }
2265 else
2266 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 DefinedClasses.push_back(GV);
2268}
2269
2270llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2271 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002272 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002274 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002275
Daniel Dunbar04d40782009-04-14 06:00:08 +00002276 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002278
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002279 std::vector<llvm::Constant*> Values(12);
2280 // The isa for the metaclass is the root of the hierarchy.
2281 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2282 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2283 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002284 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002285 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002286 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002287 // The super class for the metaclass is emitted as the name of the
2288 // super class. The runtime fixes this up to point to the
2289 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002291 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002292 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002293 ObjCTypes.ClassPtrTy);
2294 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002295 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 }
2297 Values[ 2] = GetClassName(ID->getIdentifier());
2298 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002299 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2300 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2301 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002302 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002303 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002304 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002305 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002306 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002308 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 Values[ 9] = Protocols;
2310 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002311 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002313 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002314 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315 Values);
2316
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002317 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002318 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002319
2320 // Check for a forward reference.
2321 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2322 if (GV) {
2323 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2324 "Forward metaclass reference has incorrect type.");
2325 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2326 GV->setInitializer(Init);
2327 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002328 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002329 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002330 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002331 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002333 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002334 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335
2336 return GV;
2337}
2338
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002339llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002340 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002341
Mike Stumpf5408fe2009-05-16 07:57:57 +00002342 // FIXME: Should we look these up somewhere other than the module. Its a bit
2343 // silly since we only generate these while processing an implementation, so
2344 // exactly one pointer would work if know when we entered/exitted an
2345 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002346
2347 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002348 // Previously, metaclass with internal linkage may have been defined.
2349 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002350 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2351 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002352 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2353 "Forward metaclass reference has incorrect type.");
2354 return GV;
2355 } else {
2356 // Generate as an external reference to keep a consistent
2357 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002358 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002359 llvm::GlobalValue::ExternalLinkage,
2360 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002361 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002362 }
2363}
2364
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002365llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2366 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2367
2368 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2369 true)) {
2370 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2371 "Forward class metadata reference has incorrect type.");
2372 return GV;
2373 } else {
2374 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2375 llvm::GlobalValue::ExternalLinkage,
2376 0,
2377 Name);
2378 }
2379}
2380
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381/*
2382 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002383 uint32_t size;
2384 const char *weak_ivar_layout;
2385 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002386 };
2387*/
2388llvm::Constant *
2389CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002390 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002391 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002392
2393 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002394 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002395 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002396 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002397 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002398
2399 // Return null if no extension bits are used.
2400 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002401 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002402
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002403 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002404 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002405 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002406 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002407 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408}
2409
2410/*
2411 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412 char *ivar_name;
2413 char *ivar_type;
2414 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002415 };
2416
2417 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002418 int ivar_count;
2419 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002420 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002421*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002423 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002424 std::vector<llvm::Constant*> Ivars, Ivar(3);
2425
2426 // When emitting the root class GCC emits ivar entries for the
2427 // actual class structure. It is not clear if we need to follow this
2428 // behavior; for now lets try and get away with not doing it. If so,
2429 // the cleanest solution would be to make up an ObjCInterfaceDecl
2430 // for the class.
2431 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002432 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433
2434 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002435 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002437 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002438 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002439
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002440 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2441 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002442 // Ignore unnamed bit-fields.
2443 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002444 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002445 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2446 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002447 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002448 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002449 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 }
2451
2452 // Return null for empty list.
2453 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002454 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002455
2456 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002457 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002458 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002460 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002461 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002463 llvm::GlobalVariable *GV;
2464 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002465 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002466 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002467 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002468 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002469 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002470 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002471 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002472 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473}
2474
2475/*
2476 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477 SEL method_name;
2478 char *method_types;
2479 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002481
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002482 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002483 struct objc_method_list *obsolete;
2484 int count;
2485 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002486 };
2487*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002488
2489/// GetMethodConstant - Return a struct objc_method constant for the
2490/// given method if it has been defined. The result is null if the
2491/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002492llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002493 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002494 if (!Fn)
2495 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002496
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002497 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002498 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002499 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002500 ObjCTypes.SelectorPtrTy);
2501 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002502 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002503 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002504}
2505
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002506llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002507 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002508 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002509 // Return null for empty list.
2510 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002511 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002512
2513 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002514 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002515 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002516 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002517 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002518 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002519 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002520
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002521 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002522 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002523 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002524}
2525
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002526llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002527 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002528 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002529 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002530
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002531 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002532 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002533 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002534 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002535 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002536 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002537 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002538 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002539 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002540
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002541 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002542}
2543
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002544llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002545CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002546 llvm::Constant *Init,
2547 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002548 unsigned Align,
2549 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002550 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002551 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002552 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002553 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002554 if (Section)
2555 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002556 if (Align)
2557 GV->setAlignment(Align);
2558 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002559 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002560 return GV;
2561}
2562
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002563llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002564 // Abuse this interface function as a place to finalize.
2565 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002566 return NULL;
2567}
2568
Chris Lattner74391b42009-03-22 21:03:39 +00002569llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002570 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002571}
2572
Chris Lattner74391b42009-03-22 21:03:39 +00002573llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002574 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002575}
2576
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002577llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2578 return ObjCTypes.getCopyStructFn();
2579}
2580
Chris Lattner74391b42009-03-22 21:03:39 +00002581llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002582 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002583}
2584
John McCallf1549f62010-07-06 01:34:17 +00002585void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2586 return EmitTryOrSynchronizedStmt(CGF, S);
2587}
2588
2589void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2590 const ObjCAtSynchronizedStmt &S) {
2591 return EmitTryOrSynchronizedStmt(CGF, S);
2592}
2593
John McCallcc505292010-07-21 06:59:36 +00002594namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002595 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002596 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002597 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002598 llvm::Value *CallTryExitVar;
2599 llvm::Value *ExceptionData;
2600 ObjCTypesHelper &ObjCTypes;
2601 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002602 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002603 llvm::Value *CallTryExitVar,
2604 llvm::Value *ExceptionData,
2605 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002606 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002607 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2608
2609 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2610 // Check whether we need to call objc_exception_try_exit.
2611 // In optimized code, this branch will always be folded.
2612 llvm::BasicBlock *FinallyCallExit =
2613 CGF.createBasicBlock("finally.call_exit");
2614 llvm::BasicBlock *FinallyNoCallExit =
2615 CGF.createBasicBlock("finally.no_call_exit");
2616 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2617 FinallyCallExit, FinallyNoCallExit);
2618
2619 CGF.EmitBlock(FinallyCallExit);
2620 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2621 ->setDoesNotThrow();
2622
2623 CGF.EmitBlock(FinallyNoCallExit);
2624
2625 if (isa<ObjCAtTryStmt>(S)) {
2626 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002627 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2628 // Save the current cleanup destination in case there's
2629 // control flow inside the finally statement.
2630 llvm::Value *CurCleanupDest =
2631 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2632
John McCallcc505292010-07-21 06:59:36 +00002633 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2634
John McCalld96a8e72010-08-11 00:16:14 +00002635 if (CGF.HaveInsertPoint()) {
2636 CGF.Builder.CreateStore(CurCleanupDest,
2637 CGF.getNormalCleanupDestSlot());
2638 } else {
2639 // Currently, the end of the cleanup must always exist.
2640 CGF.EnsureInsertPoint();
2641 }
2642 }
John McCallcc505292010-07-21 06:59:36 +00002643 } else {
2644 // Emit objc_sync_exit(expr); as finally's sole statement for
2645 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002646 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002647 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2648 ->setDoesNotThrow();
2649 }
2650 }
2651 };
John McCall87bb5822010-07-31 23:20:56 +00002652
2653 class FragileHazards {
2654 CodeGenFunction &CGF;
2655 llvm::SmallVector<llvm::Value*, 20> Locals;
2656 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2657
2658 llvm::InlineAsm *ReadHazard;
2659 llvm::InlineAsm *WriteHazard;
2660
2661 llvm::FunctionType *GetAsmFnType();
2662
2663 void collectLocals();
2664 void emitReadHazard(CGBuilderTy &Builder);
2665
2666 public:
2667 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002668
John McCall87bb5822010-07-31 23:20:56 +00002669 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002670 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002671 };
2672}
2673
2674/// Create the fragile-ABI read and write hazards based on the current
2675/// state of the function, which is presumed to be immediately prior
2676/// to a @try block. These hazards are used to maintain correct
2677/// semantics in the face of optimization and the fragile ABI's
2678/// cavalier use of setjmp/longjmp.
2679FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2680 collectLocals();
2681
2682 if (Locals.empty()) return;
2683
2684 // Collect all the blocks in the function.
2685 for (llvm::Function::iterator
2686 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2687 BlocksBeforeTry.insert(&*I);
2688
2689 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2690
2691 // Create a read hazard for the allocas. This inhibits dead-store
2692 // optimizations and forces the values to memory. This hazard is
2693 // inserted before any 'throwing' calls in the protected scope to
2694 // reflect the possibility that the variables might be read from the
2695 // catch block if the call throws.
2696 {
2697 std::string Constraint;
2698 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2699 if (I) Constraint += ',';
2700 Constraint += "*m";
2701 }
2702
2703 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2704 }
2705
2706 // Create a write hazard for the allocas. This inhibits folding
2707 // loads across the hazard. This hazard is inserted at the
2708 // beginning of the catch path to reflect the possibility that the
2709 // variables might have been written within the protected scope.
2710 {
2711 std::string Constraint;
2712 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2713 if (I) Constraint += ',';
2714 Constraint += "=*m";
2715 }
2716
2717 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2718 }
2719}
2720
2721/// Emit a write hazard at the current location.
2722void FragileHazards::emitWriteHazard() {
2723 if (Locals.empty()) return;
2724
2725 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2726 ->setDoesNotThrow();
2727}
2728
John McCall87bb5822010-07-31 23:20:56 +00002729void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2730 assert(!Locals.empty());
2731 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2732 ->setDoesNotThrow();
2733}
2734
2735/// Emit read hazards in all the protected blocks, i.e. all the blocks
2736/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002737void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002738 if (Locals.empty()) return;
2739
2740 CGBuilderTy Builder(CGF.getLLVMContext());
2741
2742 // Iterate through all blocks, skipping those prior to the try.
2743 for (llvm::Function::iterator
2744 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2745 llvm::BasicBlock &BB = *FI;
2746 if (BlocksBeforeTry.count(&BB)) continue;
2747
2748 // Walk through all the calls in the block.
2749 for (llvm::BasicBlock::iterator
2750 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2751 llvm::Instruction &I = *BI;
2752
2753 // Ignore instructions that aren't non-intrinsic calls.
2754 // These are the only calls that can possibly call longjmp.
2755 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2756 if (isa<llvm::IntrinsicInst>(I))
2757 continue;
2758
2759 // Ignore call sites marked nounwind. This may be questionable,
2760 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2761 llvm::CallSite CS(&I);
2762 if (CS.doesNotThrow()) continue;
2763
John McCall0b251722010-08-04 05:59:32 +00002764 // Insert a read hazard before the call. This will ensure that
2765 // any writes to the locals are performed before making the
2766 // call. If the call throws, then this is sufficient to
2767 // guarantee correctness as long as it doesn't also write to any
2768 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002769 Builder.SetInsertPoint(&BB, BI);
2770 emitReadHazard(Builder);
2771 }
2772 }
2773}
2774
2775static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2776 if (V) S.insert(V);
2777}
2778
2779void FragileHazards::collectLocals() {
2780 // Compute a set of allocas to ignore.
2781 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2782 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2783 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2784 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2785
2786 // Collect all the allocas currently in the function. This is
2787 // probably way too aggressive.
2788 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2789 for (llvm::BasicBlock::iterator
2790 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2791 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2792 Locals.push_back(&*I);
2793}
2794
2795llvm::FunctionType *FragileHazards::GetAsmFnType() {
2796 std::vector<const llvm::Type *> Tys(Locals.size());
2797 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2798 Tys[I] = Locals[I]->getType();
2799 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002800}
2801
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002802/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002803
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002804 Objective-C setjmp-longjmp (sjlj) Exception Handling
2805 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002806
John McCallf1549f62010-07-06 01:34:17 +00002807 A catch buffer is a setjmp buffer plus:
2808 - a pointer to the exception that was caught
2809 - a pointer to the previous exception data buffer
2810 - two pointers of reserved storage
2811 Therefore catch buffers form a stack, with a pointer to the top
2812 of the stack kept in thread-local storage.
2813
2814 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2815 objc_exception_try_exit pops the given catch buffer, which is
2816 required to be the top of the EH stack.
2817 objc_exception_throw pops the top of the EH stack, writes the
2818 thrown exception into the appropriate field, and longjmps
2819 to the setjmp buffer. It crashes the process (with a printf
2820 and an abort()) if there are no catch buffers on the stack.
2821 objc_exception_extract just reads the exception pointer out of the
2822 catch buffer.
2823
2824 There's no reason an implementation couldn't use a light-weight
2825 setjmp here --- something like __builtin_setjmp, but API-compatible
2826 with the heavyweight setjmp. This will be more important if we ever
2827 want to implement correct ObjC/C++ exception interactions for the
2828 fragile ABI.
2829
2830 Note that for this use of setjmp/longjmp to be correct, we may need
2831 to mark some local variables volatile: if a non-volatile local
2832 variable is modified between the setjmp and the longjmp, it has
2833 indeterminate value. For the purposes of LLVM IR, it may be
2834 sufficient to make loads and stores within the @try (to variables
2835 declared outside the @try) volatile. This is necessary for
2836 optimized correctness, but is not currently being done; this is
2837 being tracked as rdar://problem/8160285
2838
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002839 The basic framework for a @try-catch-finally is as follows:
2840 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002841 objc_exception_data d;
2842 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002843 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002844
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002845 objc_exception_try_enter(&d);
2846 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002847 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002848 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002849 // exception path
2850 id _caught = objc_exception_extract(&d);
2851
2852 // enter new try scope for handlers
2853 if (!setjmp(d.jmp_buf)) {
2854 ... match exception and execute catch blocks ...
2855
2856 // fell off end, rethrow.
2857 _rethrow = _caught;
2858 ... jump-through-finally to finally_rethrow ...
2859 } else {
2860 // exception in catch block
2861 _rethrow = objc_exception_extract(&d);
2862 _call_try_exit = false;
2863 ... jump-through-finally to finally_rethrow ...
2864 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002865 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002866 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002867
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002868 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002869 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002870 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002871
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002872 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002873 ... dispatch to finally destination ...
2874
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002875 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002876 objc_exception_throw(_rethrow);
2877
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002878 finally_end:
2879 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002880
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002881 This framework differs slightly from the one gcc uses, in that gcc
2882 uses _rethrow to determine if objc_exception_try_exit should be called
2883 and if the object should be rethrown. This breaks in the face of
2884 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002885
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002886 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002887
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002888 - If there are no catch blocks, then we avoid emitting the second
2889 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002890
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002891 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2892 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002893
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002894 - FIXME: If there is no @finally block we can do a few more
2895 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002896
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002897 Rethrows and Jumps-Through-Finally
2898 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002899
John McCallf1549f62010-07-06 01:34:17 +00002900 '@throw;' is supported by pushing the currently-caught exception
2901 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002902
John McCallf1549f62010-07-06 01:34:17 +00002903 Branches through the @finally block are handled with an ordinary
2904 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2905 exceptions are not compatible with C++ exceptions, and this is
2906 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002907
John McCallf1549f62010-07-06 01:34:17 +00002908 @synchronized(expr) { stmt; } is emitted as if it were:
2909 id synch_value = expr;
2910 objc_sync_enter(synch_value);
2911 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002912*/
2913
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002914void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2915 const Stmt &S) {
2916 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002917
2918 // A destination for the fall-through edges of the catch handlers to
2919 // jump to.
2920 CodeGenFunction::JumpDest FinallyEnd =
2921 CGF.getJumpDestInCurrentScope("finally.end");
2922
2923 // A destination for the rethrow edge of the catch handlers to jump
2924 // to.
2925 CodeGenFunction::JumpDest FinallyRethrow =
2926 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002927
Daniel Dunbar1c566672009-02-24 01:43:46 +00002928 // For @synchronized, call objc_sync_enter(sync.expr). The
2929 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002930 // @synchronized. We can't avoid a temp here because we need the
2931 // value to be preserved. If the backend ever does liveness
2932 // correctly after setjmp, this will be unnecessary.
2933 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002934 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002935 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002936 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2937 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002938 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2939 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002940
2941 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2942 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002943 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002944
John McCall0b251722010-08-04 05:59:32 +00002945 // Allocate memory for the setjmp buffer. This needs to be kept
2946 // live throughout the try and catch blocks.
2947 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2948 "exceptiondata.ptr");
2949
John McCall87bb5822010-07-31 23:20:56 +00002950 // Create the fragile hazards. Note that this will not capture any
2951 // of the allocas required for exception processing, but will
2952 // capture the current basic block (which extends all the way to the
2953 // setjmp call) as "before the @try".
2954 FragileHazards Hazards(CGF);
2955
John McCallf1549f62010-07-06 01:34:17 +00002956 // Create a flag indicating whether the cleanup needs to call
2957 // objc_exception_try_exit. This is true except when
2958 // - no catches match and we're branching through the cleanup
2959 // just to rethrow the exception, or
2960 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002961 // The setjmp-safety rule here is that we should always store to this
2962 // variable in a place that dominates the branch through the cleanup
2963 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002964 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002965 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002966
John McCallf1549f62010-07-06 01:34:17 +00002967 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002968 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002969 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002970 CallTryExitVar,
2971 ExceptionData,
2972 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002973
2974 // Enter a try block:
2975 // - Call objc_exception_try_enter to push ExceptionData on top of
2976 // the EH stack.
2977 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2978 ->setDoesNotThrow();
2979
2980 // - Call setjmp on the exception data buffer.
2981 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2982 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2983 llvm::Value *SetJmpBuffer =
2984 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2985 llvm::CallInst *SetJmpResult =
2986 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2987 SetJmpResult->setDoesNotThrow();
2988
2989 // If setjmp returned 0, enter the protected block; otherwise,
2990 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002991 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2992 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002993 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002994 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2995 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002996
John McCallf1549f62010-07-06 01:34:17 +00002997 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002998 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002999 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003000 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003001 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003002
3003 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003004
John McCallf1549f62010-07-06 01:34:17 +00003005 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003006 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003007
John McCall87bb5822010-07-31 23:20:56 +00003008 // Don't optimize loads of the in-scope locals across this point.
3009 Hazards.emitWriteHazard();
3010
John McCallf1549f62010-07-06 01:34:17 +00003011 // For a @synchronized (or a @try with no catches), just branch
3012 // through the cleanup to the rethrow block.
3013 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3014 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003015 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003016 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003017
3018 // Otherwise, we have to match against the caught exceptions.
3019 } else {
John McCall0b251722010-08-04 05:59:32 +00003020 // Retrieve the exception object. We may emit multiple blocks but
3021 // nothing can cross this so the value is already in SSA form.
3022 llvm::CallInst *Caught =
3023 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3024 ExceptionData, "caught");
3025 Caught->setDoesNotThrow();
3026
John McCallf1549f62010-07-06 01:34:17 +00003027 // Push the exception to rethrow onto the EH value stack for the
3028 // benefit of any @throws in the handlers.
3029 CGF.ObjCEHValueStack.push_back(Caught);
3030
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003031 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003032
John McCall0b251722010-08-04 05:59:32 +00003033 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003034
John McCall0b251722010-08-04 05:59:32 +00003035 llvm::BasicBlock *CatchBlock = 0;
3036 llvm::BasicBlock *CatchHandler = 0;
3037 if (HasFinally) {
3038 // Enter a new exception try block (in case a @catch block
3039 // throws an exception).
3040 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3041 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003042
John McCall0b251722010-08-04 05:59:32 +00003043 llvm::CallInst *SetJmpResult =
3044 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3045 "setjmp.result");
3046 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003047
John McCall0b251722010-08-04 05:59:32 +00003048 llvm::Value *Threw =
3049 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3050
3051 CatchBlock = CGF.createBasicBlock("catch");
3052 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3053 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3054
3055 CGF.EmitBlock(CatchBlock);
3056 }
3057
3058 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003059
Daniel Dunbar55e40722008-09-27 07:03:52 +00003060 // Handle catch list. As a special case we check if everything is
3061 // matched and avoid generating code for falling off the end if
3062 // so.
3063 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003064 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3065 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003066
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003067 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003068 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003069
Anders Carlsson80f25672008-09-09 17:59:25 +00003070 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003071 if (!CatchParam) {
3072 AllMatched = true;
3073 } else {
John McCall183700f2009-09-21 23:43:11 +00003074 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003075
John McCallf1549f62010-07-06 01:34:17 +00003076 // catch(id e) always matches under this ABI, since only
3077 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003078 // FIXME: For the time being we also match id<X>; this should
3079 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003080 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003081 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003082 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003083
John McCallf1549f62010-07-06 01:34:17 +00003084 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003086 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3087
Anders Carlssondde0a942008-09-11 09:15:33 +00003088 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003089 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003090 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003091
3092 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003093 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003094 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095
Anders Carlssondde0a942008-09-11 09:15:33 +00003096 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003097
3098 // The scope of the catch variable ends right here.
3099 CatchVarCleanups.ForceCleanup();
3100
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003101 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003102 break;
3103 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003104
Steve Naroff14108da2009-07-10 23:34:53 +00003105 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003106 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003107
3108 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003109 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3110 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003111
3112 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003113 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003114
John McCallf1549f62010-07-06 01:34:17 +00003115 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003116 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3117 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003118 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003119
John McCallf1549f62010-07-06 01:34:17 +00003120 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3121 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003122
3123 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003124 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003125
Anders Carlsson80f25672008-09-09 17:59:25 +00003126 // Emit the @catch block.
3127 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003128
3129 // Collect any cleanups for the catch variable. The scope lasts until
3130 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003131 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003132
Steve Naroff7ba138a2009-03-03 19:52:17 +00003133 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003134 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003135
John McCallf1549f62010-07-06 01:34:17 +00003136 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003137 llvm::Value *Tmp =
3138 CGF.Builder.CreateBitCast(Caught,
3139 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003140 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003141 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003142
Anders Carlssondde0a942008-09-11 09:15:33 +00003143 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003144
3145 // We're done with the catch variable.
3146 CatchVarCleanups.ForceCleanup();
3147
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003148 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003149
Anders Carlsson80f25672008-09-09 17:59:25 +00003150 CGF.EmitBlock(NextCatchBlock);
3151 }
3152
John McCallf1549f62010-07-06 01:34:17 +00003153 CGF.ObjCEHValueStack.pop_back();
3154
John McCall0b251722010-08-04 05:59:32 +00003155 // If nothing wanted anything to do with the caught exception,
3156 // kill the extract call.
3157 if (Caught->use_empty())
3158 Caught->eraseFromParent();
3159
3160 if (!AllMatched)
3161 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3162
3163 if (HasFinally) {
3164 // Emit the exception handler for the @catch blocks.
3165 CGF.EmitBlock(CatchHandler);
3166
3167 // In theory we might now need a write hazard, but actually it's
3168 // unnecessary because there's no local-accessing code between
3169 // the try's write hazard and here.
3170 //Hazards.emitWriteHazard();
3171
3172 // Don't pop the catch handler; the throw already did.
3173 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003174 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003175 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003176 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003177
John McCall87bb5822010-07-31 23:20:56 +00003178 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003179 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003180
John McCallf1549f62010-07-06 01:34:17 +00003181 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003182 CGF.Builder.restoreIP(TryFallthroughIP);
3183 if (CGF.HaveInsertPoint())
3184 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003185 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003186 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003187
John McCallf1549f62010-07-06 01:34:17 +00003188 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003189 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003190 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003191 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003192 // Just look in the buffer for the exception to throw.
3193 llvm::CallInst *Caught =
3194 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3195 ExceptionData);
3196 Caught->setDoesNotThrow();
3197
3198 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003199 ->setDoesNotThrow();
3200 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003201 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003202
John McCall87bb5822010-07-31 23:20:56 +00003203 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003204}
3205
3206void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003207 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003208 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003209
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003210 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3211 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003212 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003213 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3214 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003215 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003216 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003217 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003218 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003219
John McCallf1549f62010-07-06 01:34:17 +00003220 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3221 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003222 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003223
3224 // Clear the insertion point to indicate we are in unreachable code.
3225 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003226}
3227
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003228/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003229/// object: objc_read_weak (id *src)
3230///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003231llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003232 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003233 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003234 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3235 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3236 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003237 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003238 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003239 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003240 return read_weak;
3241}
3242
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003243/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3244/// objc_assign_weak (id src, id *dst)
3245///
3246void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003247 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003248 const llvm::Type * SrcTy = src->getType();
3249 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003250 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003251 assert(Size <= 8 && "does not support size > 8");
3252 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003253 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003254 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3255 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003256 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3257 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003258 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003259 src, dst, "weakassign");
3260 return;
3261}
3262
Fariborz Jahanian58626502008-11-19 00:59:10 +00003263/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3264/// objc_assign_global (id src, id *dst)
3265///
3266void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003267 llvm::Value *src, llvm::Value *dst,
3268 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003269 const llvm::Type * SrcTy = src->getType();
3270 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003271 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003272 assert(Size <= 8 && "does not support size > 8");
3273 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003274 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003275 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3276 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003277 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3278 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003279 if (!threadlocal)
3280 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3281 src, dst, "globalassign");
3282 else
3283 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3284 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003285 return;
3286}
3287
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003288/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003289/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003290///
3291void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003292 llvm::Value *src, llvm::Value *dst,
3293 llvm::Value *ivarOffset) {
3294 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003295 const llvm::Type * SrcTy = src->getType();
3296 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003297 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003298 assert(Size <= 8 && "does not support size > 8");
3299 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003300 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003301 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3302 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003303 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3304 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003305 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3306 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003307 return;
3308}
3309
Fariborz Jahanian58626502008-11-19 00:59:10 +00003310/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3311/// objc_assign_strongCast (id src, id *dst)
3312///
3313void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003314 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003315 const llvm::Type * SrcTy = src->getType();
3316 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003317 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003318 assert(Size <= 8 && "does not support size > 8");
3319 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003321 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3322 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003323 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3324 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003325 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003326 src, dst, "weakassign");
3327 return;
3328}
3329
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003330void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003331 llvm::Value *DestPtr,
3332 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003333 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003334 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3335 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003336 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003337 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003338 return;
3339}
3340
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003341/// EmitObjCValueForIvar - Code Gen for ivar reference.
3342///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003343LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3344 QualType ObjectTy,
3345 llvm::Value *BaseValue,
3346 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003347 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003348 const ObjCInterfaceDecl *ID =
3349 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003350 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3351 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003352}
3353
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003354llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003355 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003356 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003357 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003358 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003359 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3360 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003361}
3362
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003363/* *** Private Interface *** */
3364
3365/// EmitImageInfo - Emit the image info marker used to encode some module
3366/// level information.
3367///
3368/// See: <rdr://4810609&4810587&4810587>
3369/// struct IMAGE_INFO {
3370/// unsigned version;
3371/// unsigned flags;
3372/// };
3373enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003374 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003375 eImageInfo_GarbageCollected = (1 << 1),
3376 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003377 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3378
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003379 // A flag indicating that the module has no instances of a @synthesize of a
3380 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003381 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003382};
3383
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003384void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003385 unsigned version = 0; // Version is unused?
3386 unsigned flags = 0;
3387
3388 // FIXME: Fix and continue?
3389 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3390 flags |= eImageInfo_GarbageCollected;
3391 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3392 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003393
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003394 // We never allow @synthesize of a superclass property.
3395 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003396
Chris Lattner77b89b82010-06-27 07:15:29 +00003397 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3398
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003399 // Emitted as int[2];
3400 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003401 llvm::ConstantInt::get(Int32Ty, version),
3402 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003403 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003404 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003405
3406 const char *Section;
3407 if (ObjCABI == 1)
3408 Section = "__OBJC, __image_info,regular";
3409 else
3410 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003411 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003412 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003413 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003414 Section,
3415 0,
3416 true);
3417 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003418}
3419
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003420
3421// struct objc_module {
3422// unsigned long version;
3423// unsigned long size;
3424// const char *name;
3425// Symtab symtab;
3426// };
3427
3428// FIXME: Get from somewhere
3429static const int ModuleVersion = 7;
3430
3431void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003432 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003433
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003434 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003435 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3436 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003437 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003438 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003439 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003440 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003441 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003442 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003443 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003444}
3445
3446llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003447 unsigned NumClasses = DefinedClasses.size();
3448 unsigned NumCategories = DefinedCategories.size();
3449
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003450 // Return null if no symbols were defined.
3451 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003452 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003453
3454 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003455 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003456 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003457 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3458 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003459
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003460 // The runtime expects exactly the list of defined classes followed
3461 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003462 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003463 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003464 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003465 ObjCTypes.Int8PtrTy);
3466 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003467 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003468 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003469 ObjCTypes.Int8PtrTy);
3470
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003471 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003472 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003473 NumClasses + NumCategories),
3474 Symbols);
3475
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003476 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003477
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003478 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003479 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3480 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003481 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003482 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003483}
3484
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003485llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003486 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003487 LazySymbols.insert(ID->getIdentifier());
3488
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003489 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003490
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003491 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003492 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003493 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003494 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003495 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003496 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3497 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003498 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003499 }
3500
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003501 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003502}
3503
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003504llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3505 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003506 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003507
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003508 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003509 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003510 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003511 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003512 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003513 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3514 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003515 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003516 }
3517
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003518 if (lvalue)
3519 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003520 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003521}
3522
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003523llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003524 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003525
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003526 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003528 llvm::ConstantArray::get(VMContext,
3529 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003530 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003531 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003532
Owen Andersona1cf15f2009-07-14 23:10:40 +00003533 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003534}
3535
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003536llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3537 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3538 I = MethodDefinitions.find(MD);
3539 if (I != MethodDefinitions.end())
3540 return I->second;
3541
3542 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3543 // MD isn't emitted yet because it comes from PCH.
3544 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3545 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3546 return MethodDefinitions[MD];
3547 }
3548
3549 return NULL;
3550}
3551
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003552/// GetIvarLayoutName - Returns a unique constant for the given
3553/// ivar layout bitmap.
3554llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003555 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003556 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003557}
3558
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003559void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003560 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003561 bool ForStrongLayout,
3562 bool &HasUnion) {
3563 const RecordDecl *RD = RT->getDecl();
3564 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003565 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003566 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003567 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003568 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003570 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3571 ForStrongLayout, HasUnion);
3572}
3573
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003574void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003575 const llvm::StructLayout *Layout,
3576 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003577 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003578 unsigned int BytePos, bool ForStrongLayout,
3579 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003580 bool IsUnion = (RD && RD->isUnion());
3581 uint64_t MaxUnionIvarSize = 0;
3582 uint64_t MaxSkippedUnionIvarSize = 0;
3583 FieldDecl *MaxField = 0;
3584 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003585 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003586 uint64_t MaxFieldOffset = 0;
3587 uint64_t MaxSkippedFieldOffset = 0;
3588 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003589
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003590 if (RecFields.empty())
3591 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003592 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3593 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3594
Chris Lattnerf1690852009-03-31 08:48:01 +00003595 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003596 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003597 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003598 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003599 // Note that 'i' here is actually the field index inside RD of Field,
3600 // although this dependency is hidden.
3601 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3602 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003603 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003604 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003605
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003606 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003607 if (!Field->getIdentifier() || Field->isBitField()) {
3608 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003609 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003610 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003611 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003612
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003613 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003614 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003615 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003616 if (FQT->isUnionType())
3617 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003618
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003619 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003620 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003621 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003622 continue;
3623 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003624
Chris Lattnerf1690852009-03-31 08:48:01 +00003625 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003626 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003627 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003628 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003629 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003630 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003631 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3632 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003633 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003634 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003635 FQT = CArray->getElementType();
3636 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003637
3638 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003639 "layout for array of unions not supported");
3640 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003641 int OldIndex = IvarsInfo.size() - 1;
3642 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003643
Ted Kremenek6217b802009-07-29 21:53:49 +00003644 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003645 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003646 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003647
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003648 // Replicate layout information for each array element. Note that
3649 // one element is already done.
3650 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003651 for (int FirstIndex = IvarsInfo.size() - 1,
3652 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003653 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003654 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3655 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3656 IvarsInfo[i].ivar_size));
3657 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3658 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3659 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003660 }
3661 continue;
3662 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003663 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003664 // At this point, we are done with Record/Union and array there of.
3665 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003666 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003667
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003668 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003669 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3670 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003671 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003672 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003673 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003674 MaxUnionIvarSize = UnionIvarSize;
3675 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003676 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003677 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003678 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003679 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003680 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003681 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003682 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003683 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3684 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003685 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003686 // FIXME: Why the asymmetry? We divide by word size in bits on other
3687 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003688 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003689 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003690 MaxSkippedUnionIvarSize = UnionIvarSize;
3691 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003692 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003693 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003694 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003695 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003696 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003697 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003698 }
3699 }
3700 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003701
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003702 if (LastFieldBitfield) {
3703 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003704 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3705 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003706 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003707 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003708 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003709 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3710 + ((BitFieldSize % ByteSizeInBits) != 0);
3711 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003713
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003714 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003715 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003716 MaxUnionIvarSize));
3717 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003718 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003719 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003720}
3721
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003722/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3723/// the computations and returning the layout bitmap (for ivar or blocks) in
3724/// the given argument BitMap string container. Routine reads
3725/// two containers, IvarsInfo and SkipIvars which are assumed to be
3726/// filled already by the caller.
3727llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003728 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003729 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003730
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003731 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003732 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003733 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003734 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003735 if (IvarsInfo[0].ivar_bytepos == 0) {
3736 WordsToSkip = 0;
3737 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003738 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003739 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3740 WordsToScan = IvarsInfo[0].ivar_size;
3741 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003742 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003743 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003744 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003745 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003746 // consecutive 'scanned' object pointers.
3747 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003748 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003749 // Skip over 'gc'able object pointer which lay over each other.
3750 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3751 continue;
3752 // Must skip over 1 or more words. We save current skip/scan values
3753 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003754 SKIP_SCAN SkScan;
3755 SkScan.skip = WordsToSkip;
3756 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003757 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003758
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003759 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003760 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3761 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003762 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003763 WordsToSkip = 0;
3764 WordsToScan = IvarsInfo[i].ivar_size;
3765 }
3766 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003767 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003768 SKIP_SCAN SkScan;
3769 SkScan.skip = WordsToSkip;
3770 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003771 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003772 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003773
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003774 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003775 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003776 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003777 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003778 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003779 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003780 IvarsInfo[LastIndex].ivar_bytepos +
3781 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003782 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003783 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003784 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003785 SKIP_SCAN SkScan;
3786 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3787 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003788 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 }
3790 }
3791 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3792 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003793 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003794 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003795 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3796 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3797 // 0xM0 followed by 0x0N detected.
3798 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3799 for (int j = i+1; j < SkipScan; j++)
3800 SkipScanIvars[j] = SkipScanIvars[j+1];
3801 --SkipScan;
3802 }
3803 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003804
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003805 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003806 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003807 unsigned char byte;
3808 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3809 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3810 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3811 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003812
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003813 // first skip big.
3814 for (unsigned int ix = 0; ix < skip_big; ix++)
3815 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003816
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003817 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003818 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003819 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003820 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003821 byte |= 0xf;
3822 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003823 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003824 byte |= scan_small;
3825 scan_small = 0;
3826 }
3827 BitMap += byte;
3828 }
3829 // next scan big
3830 for (unsigned int ix = 0; ix < scan_big; ix++)
3831 BitMap += (unsigned char)(0x0f);
3832 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003833 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003834 byte = scan_small;
3835 BitMap += byte;
3836 }
3837 }
3838 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003839 unsigned char zero = 0;
3840 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003841
3842 llvm::GlobalVariable * Entry =
3843 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3844 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3845 "__TEXT,__cstring,cstring_literals",
3846 1, true);
3847 return getConstantGEP(VMContext, Entry, 0, 0);
3848}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003849
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003850/// BuildIvarLayout - Builds ivar layout bitmap for the class
3851/// implementation for the __strong or __weak case.
3852/// The layout map displays which words in ivar list must be skipped
3853/// and which must be scanned by GC (see below). String is built of bytes.
3854/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3855/// of words to skip and right nibble is count of words to scan. So, each
3856/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3857/// represented by a 0x00 byte which also ends the string.
3858/// 1. when ForStrongLayout is true, following ivars are scanned:
3859/// - id, Class
3860/// - object *
3861/// - __strong anything
3862///
3863/// 2. When ForStrongLayout is false, following ivars are scanned:
3864/// - __weak anything
3865///
3866llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3867 const ObjCImplementationDecl *OMD,
3868 bool ForStrongLayout) {
3869 bool hasUnion = false;
3870
3871 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3872 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3873 return llvm::Constant::getNullValue(PtrTy);
3874
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003875 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003876 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003877 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003878
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003879 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003880 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3881 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3882
3883 if (RecFields.empty())
3884 return llvm::Constant::getNullValue(PtrTy);
3885
3886 SkipIvars.clear();
3887 IvarsInfo.clear();
3888
3889 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3890 if (IvarsInfo.empty())
3891 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003892 // Sort on byte position in case we encounterred a union nested in
3893 // the ivar list.
3894 if (hasUnion && !IvarsInfo.empty())
3895 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3896 if (hasUnion && !SkipIvars.empty())
3897 std::sort(SkipIvars.begin(), SkipIvars.end());
3898
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003899 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003900 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003901
3902 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003903 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003904 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003905 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003906 const unsigned char *s = (unsigned char*)BitMap.c_str();
3907 for (unsigned i = 0; i < BitMap.size(); i++)
3908 if (!(s[i] & 0xf0))
3909 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3910 else
3911 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3912 printf("\n");
3913 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003914 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003915}
3916
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003917llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003918 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3919
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003920 // FIXME: Avoid std::string copying.
3921 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003922 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003923 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003924 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003925 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003926
Owen Andersona1cf15f2009-07-14 23:10:40 +00003927 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003928}
3929
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003930// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003931llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003932 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3933}
3934
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003935llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003936 std::string TypeStr;
3937 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3938
3939 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003940
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003941 if (!Entry)
3942 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003943 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003944 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003945 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946
Owen Andersona1cf15f2009-07-14 23:10:40 +00003947 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003948}
3949
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003950llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003951 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003952 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3953 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003954
3955 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3956
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003957 if (!Entry)
3958 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003959 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003960 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003961 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003962
Owen Andersona1cf15f2009-07-14 23:10:40 +00003963 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003964}
3965
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003966// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003967llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003968 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003969
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003970 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003971 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003972 llvm::ConstantArray::get(VMContext,
3973 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003974 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003975 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003976
Owen Andersona1cf15f2009-07-14 23:10:40 +00003977 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003978}
3979
3980// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003981// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003982llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003983CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3984 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003985 std::string TypeStr;
3986 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003987 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3988}
3989
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003990void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003991 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003992 llvm::SmallVectorImpl<char> &Name) {
3993 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003994 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003995 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3996 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003997 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003998 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003999 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004000 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004001}
4002
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004003void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004004 EmitModuleInfo();
4005
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004006 // Emit the dummy bodies for any protocols which were referenced but
4007 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004008 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004009 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4010 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004011 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004012
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004013 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004014 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004015 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004016 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004017 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004018 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004019 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004020 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004021 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004022 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004023 }
4024
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004025 // Add assembler directives to add lazy undefined symbol references
4026 // for classes which are referenced but not defined. This is
4027 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004028 //
4029 // FIXME: It would be nice if we had an LLVM construct for this.
4030 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4031 llvm::SmallString<256> Asm;
4032 Asm += CGM.getModule().getModuleInlineAsm();
4033 if (!Asm.empty() && Asm.back() != '\n')
4034 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004035
Daniel Dunbar33063492009-09-07 00:20:42 +00004036 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004037 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4038 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004039 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4040 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004041 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004042 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004043 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004044 }
4045
4046 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4047 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4048 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4049 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004050
Daniel Dunbar33063492009-09-07 00:20:42 +00004051 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004052 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004053}
4054
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004055CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004056 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004057 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004058 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004059 ObjCABI = 2;
4060}
4061
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004062/* *** */
4063
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004064ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004065 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004066 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4067 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004068
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004069 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004070 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004071 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004072 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004073 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004074
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004075 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004076 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004077 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004078
Mike Stumpf5408fe2009-05-16 07:57:57 +00004079 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4080 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004081 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004082 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004083
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004084 // I'm not sure I like this. The implicit coordination is a bit
4085 // gross. We should solve this in a reasonable fashion because this
4086 // is a pretty common task (match some runtime data structure with
4087 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004088
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004089 // FIXME: This is leaked.
4090 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004091
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004092 // struct _objc_super {
4093 // id self;
4094 // Class cls;
4095 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004096 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004097 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004098 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004099 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004100 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004101 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004102 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004103 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004104 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004105
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004106 SuperCTy = Ctx.getTagDeclType(RD);
4107 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004108
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004109 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004110 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4111
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004112 // struct _prop_t {
4113 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004114 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004115 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004116 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004117 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004118 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004119
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004120 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004121 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004122 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004123 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004124 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004125 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004126 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004127 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004128 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004129 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004130 PropertyListTy);
4131 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004132 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004133
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004134 // struct _objc_method {
4135 // SEL _cmd;
4136 // char *method_type;
4137 // char *_imp;
4138 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004139 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004140 Int8PtrTy,
4141 Int8PtrTy,
4142 NULL);
4143 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004145 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004146 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004147 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004148 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004149}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004150
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004151ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004152 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004153 // struct _objc_method_description {
4154 // SEL name;
4155 // char *types;
4156 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004157 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004158 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004159 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004160 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004161 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004162 MethodDescriptionTy);
4163
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004164 // struct _objc_method_description_list {
4165 // int count;
4166 // struct _objc_method_description[1];
4167 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004168 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004169 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004170 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004171 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004172 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004173 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004175 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004177 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004178
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004179 // Protocol description structures
4180
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004181 // struct _objc_protocol_extension {
4182 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4183 // struct _objc_method_description_list *optional_instance_methods;
4184 // struct _objc_method_description_list *optional_class_methods;
4185 // struct _objc_property_list *instance_properties;
4186 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004188 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004189 MethodDescriptionListPtrTy,
4190 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004191 PropertyListPtrTy,
4192 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004193 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004194 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004195
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004196 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004197 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004198
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004199 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004200
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004201 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4202 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004203
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004204 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004205 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004206 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004207 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004208 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004209 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004210 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4211
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004212 // struct _objc_protocol {
4213 // struct _objc_protocol_extension *isa;
4214 // char *protocol_name;
4215 // struct _objc_protocol **_objc_protocol_list;
4216 // struct _objc_method_description_list *instance_methods;
4217 // struct _objc_method_description_list *class_methods;
4218 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004219 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004220 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004221 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004222 MethodDescriptionListPtrTy,
4223 MethodDescriptionListPtrTy,
4224 NULL);
4225 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4226
4227 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004228 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004229 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004230 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004231 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004232
4233 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004234 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004235 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004236
4237 // Class description structures
4238
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004239 // struct _objc_ivar {
4240 // char *ivar_name;
4241 // char *ivar_type;
4242 // int ivar_offset;
4243 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004244 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004245 Int8PtrTy,
4246 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004247 NULL);
4248 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4249
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004250 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004251 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004252 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004253 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004254
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004255 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004256 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004257 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004258 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004259
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004260 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004261 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004262 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004263 Int8PtrTy,
4264 PropertyListPtrTy,
4265 NULL);
4266 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004267 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004268
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004269 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004270
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004271 // struct _objc_class {
4272 // Class isa;
4273 // Class super_class;
4274 // char *name;
4275 // long version;
4276 // long info;
4277 // long instance_size;
4278 // struct _objc_ivar_list *ivars;
4279 // struct _objc_method_list *methods;
4280 // struct _objc_cache *cache;
4281 // struct _objc_protocol_list *protocols;
4282 // char *ivar_layout;
4283 // struct _objc_class_ext *ext;
4284 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004285 T = llvm::StructType::get(VMContext,
4286 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004287 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004288 Int8PtrTy,
4289 LongTy,
4290 LongTy,
4291 LongTy,
4292 IvarListPtrTy,
4293 MethodListPtrTy,
4294 CachePtrTy,
4295 ProtocolListPtrTy,
4296 Int8PtrTy,
4297 ClassExtensionPtrTy,
4298 NULL);
4299 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004300
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004301 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4302 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004303 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004304
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004305 // struct _objc_category {
4306 // char *category_name;
4307 // char *class_name;
4308 // struct _objc_method_list *instance_method;
4309 // struct _objc_method_list *class_method;
4310 // uint32_t size; // sizeof(struct _objc_category)
4311 // struct _objc_property_list *instance_properties;// category's @property
4312 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004313 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004314 Int8PtrTy,
4315 MethodListPtrTy,
4316 MethodListPtrTy,
4317 ProtocolListPtrTy,
4318 IntTy,
4319 PropertyListPtrTy,
4320 NULL);
4321 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4322
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323 // Global metadata structures
4324
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004325 // struct _objc_symtab {
4326 // long sel_ref_cnt;
4327 // SEL *refs;
4328 // short cls_def_cnt;
4329 // short cat_def_cnt;
4330 // char *defs[cls_def_cnt + cat_def_cnt];
4331 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004332 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004333 SelectorPtrTy,
4334 ShortTy,
4335 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004336 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004337 NULL);
4338 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004339 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004340
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004341 // struct _objc_module {
4342 // long version;
4343 // long size; // sizeof(struct _objc_module)
4344 // char *name;
4345 // struct _objc_symtab* symtab;
4346 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004347 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004348 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004349 LongTy,
4350 Int8PtrTy,
4351 SymtabPtrTy,
4352 NULL);
4353 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004354
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004355
Mike Stumpf5408fe2009-05-16 07:57:57 +00004356 // FIXME: This is the size of the setjmp buffer and should be target
4357 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004358 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004359
Anders Carlsson124526b2008-09-09 10:10:21 +00004360 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004361 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004362 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004363
4364 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004365 llvm::StructType::get(VMContext,
4366 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4367 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004368 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004370 ExceptionDataTy);
4371
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004372}
4373
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004375 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004376 // struct _method_list_t {
4377 // uint32_t entsize; // sizeof(struct _objc_method)
4378 // uint32_t method_count;
4379 // struct _objc_method method_list[method_count];
4380 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004381 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004382 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004383 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004384 NULL);
4385 CGM.getModule().addTypeName("struct.__method_list_t",
4386 MethodListnfABITy);
4387 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004388 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004389
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004390 // struct _protocol_t {
4391 // id isa; // NULL
4392 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004393 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004394 // const struct method_list_t * const instance_methods;
4395 // const struct method_list_t * const class_methods;
4396 // const struct method_list_t *optionalInstanceMethods;
4397 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004398 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004399 // const uint32_t size; // sizeof(struct _protocol_t)
4400 // const uint32_t flags; // = 0
4401 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004402
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004403 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004404 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004405
Owen Anderson47a434f2009-08-05 23:18:46 +00004406 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004407 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004408 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004409 ProtocolListTyHolder),
4410 MethodListnfABIPtrTy,
4411 MethodListnfABIPtrTy,
4412 MethodListnfABIPtrTy,
4413 MethodListnfABIPtrTy,
4414 PropertyListPtrTy,
4415 IntTy,
4416 IntTy,
4417 NULL);
4418 CGM.getModule().addTypeName("struct._protocol_t",
4419 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004420
4421 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004422 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423
Fariborz Jahanianda320092009-01-29 19:24:30 +00004424 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004425 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004426 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004427 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004428 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004429 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004430 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004431 NULL);
4432 CGM.getModule().addTypeName("struct._objc_protocol_list",
4433 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004434 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004435 ProtocolListnfABITy);
4436
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004437 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004438 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004439
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004440 // struct _ivar_t {
4441 // unsigned long int *offset; // pointer to ivar offset location
4442 // char *name;
4443 // char *type;
4444 // uint32_t alignment;
4445 // uint32_t size;
4446 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004447 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004448 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004449 Int8PtrTy,
4450 Int8PtrTy,
4451 IntTy,
4452 IntTy,
4453 NULL);
4454 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004455
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004456 // struct _ivar_list_t {
4457 // uint32 entsize; // sizeof(struct _ivar_t)
4458 // uint32 count;
4459 // struct _iver_t list[count];
4460 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004461 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004462 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004463 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004464 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004465 NULL);
4466 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004467
Owen Anderson96e0fc72009-07-29 22:16:19 +00004468 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004469
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004470 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004471 // uint32_t const flags;
4472 // uint32_t const instanceStart;
4473 // uint32_t const instanceSize;
4474 // uint32_t const reserved; // only when building for 64bit targets
4475 // const uint8_t * const ivarLayout;
4476 // const char *const name;
4477 // const struct _method_list_t * const baseMethods;
4478 // const struct _objc_protocol_list *const baseProtocols;
4479 // const struct _ivar_list_t *const ivars;
4480 // const uint8_t * const weakIvarLayout;
4481 // const struct _prop_list_t * const properties;
4482 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004483
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004484 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004485 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004486 IntTy,
4487 IntTy,
4488 Int8PtrTy,
4489 Int8PtrTy,
4490 MethodListnfABIPtrTy,
4491 ProtocolListnfABIPtrTy,
4492 IvarListnfABIPtrTy,
4493 Int8PtrTy,
4494 PropertyListPtrTy,
4495 NULL);
4496 CGM.getModule().addTypeName("struct._class_ro_t",
4497 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004499 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4500 std::vector<const llvm::Type*> Params;
4501 Params.push_back(ObjectPtrTy);
4502 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004503 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004504 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4505
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004506 // struct _class_t {
4507 // struct _class_t *isa;
4508 // struct _class_t * const superclass;
4509 // void *cache;
4510 // IMP *vtable;
4511 // struct class_ro_t *ro;
4512 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004513
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004514 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004515 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004516 llvm::StructType::get(VMContext,
4517 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004518 llvm::PointerType::getUnqual(ClassTyHolder),
4519 CachePtrTy,
4520 llvm::PointerType::getUnqual(ImpnfABITy),
4521 llvm::PointerType::getUnqual(ClassRonfABITy),
4522 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004523 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4524
4525 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004526 ClassnfABITy);
4527
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004528 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004529 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004530
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004531 // struct _category_t {
4532 // const char * const name;
4533 // struct _class_t *const cls;
4534 // const struct _method_list_t * const instance_methods;
4535 // const struct _method_list_t * const class_methods;
4536 // const struct _protocol_list_t * const protocols;
4537 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004538 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004539 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004540 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004541 MethodListnfABIPtrTy,
4542 MethodListnfABIPtrTy,
4543 ProtocolListnfABIPtrTy,
4544 PropertyListPtrTy,
4545 NULL);
4546 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004547
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004548 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004549 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4550 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004552 // MessageRefTy - LLVM for:
4553 // struct _message_ref_t {
4554 // IMP messenger;
4555 // SEL name;
4556 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004558 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004559 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004560 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004561 SourceLocation(),
4562 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004563 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004564 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004565 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004566 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004567 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004568
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004569 MessageRefCTy = Ctx.getTagDeclType(RD);
4570 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4571 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004572
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004573 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004574 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004576 // SuperMessageRefTy - LLVM for:
4577 // struct _super_message_ref_t {
4578 // SUPER_IMP messenger;
4579 // SEL name;
4580 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004581 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004582 SelectorPtrTy,
4583 NULL);
4584 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004585
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004586 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4588
Daniel Dunbare588b992009-03-01 04:46:24 +00004589
4590 // struct objc_typeinfo {
4591 // const void** vtable; // objc_ehtype_vtable + 2
4592 // const char* name; // c++ typeinfo string
4593 // Class cls;
4594 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004595 EHTypeTy = llvm::StructType::get(VMContext,
4596 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004597 Int8PtrTy,
4598 ClassnfABIPtrTy,
4599 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004600 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004601 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004602}
4603
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004605 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004606
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004607 return NULL;
4608}
4609
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004610void CGObjCNonFragileABIMac::AddModuleClassList(const
4611 std::vector<llvm::GlobalValue*>
4612 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004613 const char *SymbolName,
4614 const char *SectionName) {
4615 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004616
Daniel Dunbar463b8762009-05-15 21:48:48 +00004617 if (!NumClasses)
4618 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004619
Daniel Dunbar463b8762009-05-15 21:48:48 +00004620 std::vector<llvm::Constant*> Symbols(NumClasses);
4621 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004622 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004623 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004624 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004625 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004626 NumClasses),
4627 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004628
Daniel Dunbar463b8762009-05-15 21:48:48 +00004629 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004630 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004631 llvm::GlobalValue::InternalLinkage,
4632 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004633 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004634 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004635 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004636 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004637}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004639void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4640 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004641
Daniel Dunbar463b8762009-05-15 21:48:48 +00004642 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004643 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004645 "\01L_OBJC_LABEL_CLASS_$",
4646 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004647
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004648 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4649 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4650 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4651 continue;
4652 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004653 }
4654
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004655 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4656 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4657 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4658 continue;
4659 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4660 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004661
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004662 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004663 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4664 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004665
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004666 // Build list of all implemented category addresses in array
4667 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004668 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004669 "\01L_OBJC_LABEL_CATEGORY_$",
4670 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004671 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004672 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4673 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004674
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004675 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004676}
4677
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004678/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004679/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004680/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004681/// message dispatch call for all the rest.
4682///
4683bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004684 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4685 default:
4686 assert(0 && "Invalid dispatch method!");
4687 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004688 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004689 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004690 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004691 case CodeGenOptions::Mixed:
4692 break;
4693 }
4694
4695 // If so, see whether this selector is in the white-list of things which must
4696 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004697 if (NonLegacyDispatchMethods.empty()) {
4698 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4699 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4700 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4701 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4702 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4703 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4704 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4705 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4706 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4707 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004708
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004709 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4710 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4711 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4712 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4713 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4714 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4715 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4716 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004717 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004718 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004719 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4720 &CGM.getContext().Idents.get("objects"),
4721 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004722 };
4723 NonLegacyDispatchMethods.insert(
4724 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004725 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004726
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004727 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004728}
4729
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004730// Metadata flags
4731enum MetaDataDlags {
4732 CLS = 0x0,
4733 CLS_META = 0x1,
4734 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004735 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004736 CLS_EXCEPTION = 0x20
4737};
4738/// BuildClassRoTInitializer - generate meta-data for:
4739/// struct _class_ro_t {
4740/// uint32_t const flags;
4741/// uint32_t const instanceStart;
4742/// uint32_t const instanceSize;
4743/// uint32_t const reserved; // only when building for 64bit targets
4744/// const uint8_t * const ivarLayout;
4745/// const char *const name;
4746/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004747/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004748/// const struct _ivar_list_t *const ivars;
4749/// const uint8_t * const weakIvarLayout;
4750/// const struct _prop_list_t * const properties;
4751/// }
4752///
4753llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004754 unsigned flags,
4755 unsigned InstanceStart,
4756 unsigned InstanceSize,
4757 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004758 std::string ClassName = ID->getNameAsString();
4759 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004760 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4761 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4762 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004763 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4765 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004766 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004767 // const struct _method_list_t * const baseMethods;
4768 std::vector<llvm::Constant*> Methods;
4769 std::string MethodListName("\01l_OBJC_$_");
4770 if (flags & CLS_META) {
4771 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004772 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004773 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004774 // Class methods should always be defined.
4775 Methods.push_back(GetMethodConstant(*i));
4776 }
4777 } else {
4778 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004779 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004780 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004781 // Instance methods should always be defined.
4782 Methods.push_back(GetMethodConstant(*i));
4783 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004784 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004785 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004786 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004787
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004788 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4789 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004790
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004791 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4792 if (llvm::Constant *C = GetMethodConstant(MD))
4793 Methods.push_back(C);
4794 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4795 if (llvm::Constant *C = GetMethodConstant(MD))
4796 Methods.push_back(C);
4797 }
4798 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004799 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004800 Values[ 5] = EmitMethodList(MethodListName,
4801 "__DATA, __objc_const", Methods);
4802
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4804 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004805 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004806 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004807 OID->protocol_begin(),
4808 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004809
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004810 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004811 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004812 else
4813 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004814 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4815 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004816 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004817 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004818 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004819 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4820 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004821 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004822 Values);
4823 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004824 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4825 llvm::GlobalValue::InternalLinkage,
4826 Init,
4827 (flags & CLS_META) ?
4828 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4829 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004830 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004831 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004832 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004833 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004834
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004835}
4836
4837/// BuildClassMetaData - This routine defines that to-level meta-data
4838/// for the given ClassName for:
4839/// struct _class_t {
4840/// struct _class_t *isa;
4841/// struct _class_t * const superclass;
4842/// void *cache;
4843/// IMP *vtable;
4844/// struct class_ro_t *ro;
4845/// }
4846///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004847llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848 std::string &ClassName,
4849 llvm::Constant *IsAGV,
4850 llvm::Constant *SuperClassGV,
4851 llvm::Constant *ClassRoGV,
4852 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004853 std::vector<llvm::Constant*> Values(5);
4854 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004855 Values[1] = SuperClassGV;
4856 if (!Values[1])
4857 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004858 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4859 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4860 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004861 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004862 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004863 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4864 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004865 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004866 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004867 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004868 if (HiddenVisibility)
4869 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004870 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004871}
4872
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004873bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004874CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004875 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004876}
4877
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004878void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004879 uint32_t &InstanceStart,
4880 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004881 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004882 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004883
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004884 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004885 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004886
4887 // If there are no fields, the start is the same as the end.
4888 if (!RL.getFieldCount())
4889 InstanceStart = InstanceSize;
4890 else
4891 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004892}
4893
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004894void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4895 std::string ClassName = ID->getNameAsString();
4896 if (!ObjCEmptyCacheVar) {
4897 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004898 CGM.getModule(),
4899 ObjCTypes.CacheTy,
4900 false,
4901 llvm::GlobalValue::ExternalLinkage,
4902 0,
4903 "_objc_empty_cache");
4904
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004905 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004906 CGM.getModule(),
4907 ObjCTypes.ImpnfABITy,
4908 false,
4909 llvm::GlobalValue::ExternalLinkage,
4910 0,
4911 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004912 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004913 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004914 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004915 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004916 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004917 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004918 uint32_t InstanceSize = InstanceStart;
4919 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004920 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4921 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004922
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004923 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004924
4925 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004926 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004927 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004928 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004929 if (ID->getNumIvarInitializers())
4930 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004931 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004932 // class is root
4933 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004934 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004935 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004936 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004937 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004938 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4939 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4940 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004941 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004942 if (Root->hasAttr<WeakImportAttr>())
4943 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004944 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004945 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004946 ObjCMetaClassName +
4947 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004948 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004949 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4950 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004951 }
4952 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4953 InstanceStart,
4954 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004955 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004957 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4958 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004959 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004960
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004961 // Metadata for the class
4962 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004963 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004964 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004965 if (ID->getNumIvarInitializers())
4966 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004967
Douglas Gregor68584ed2009-06-18 16:11:24 +00004968 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004969 flags |= CLS_EXCEPTION;
4970
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004971 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004972 flags |= CLS_ROOT;
4973 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004974 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004975 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004976 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004977 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004978 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004979 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4980 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004981 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004982 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004983 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004984 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004985 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004986 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004987
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004988 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004989 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004990 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4991 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004992 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004993
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004994 // Determine if this class is also "non-lazy".
4995 if (ImplementationIsNonLazy(ID))
4996 DefinedNonLazyClasses.push_back(ClassMD);
4997
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004998 // Force the definition of the EHType if necessary.
4999 if (flags & CLS_EXCEPTION)
5000 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005001}
5002
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005003/// GenerateProtocolRef - This routine is called to generate code for
5004/// a protocol reference expression; as in:
5005/// @code
5006/// @protocol(Proto1);
5007/// @endcode
5008/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5009/// which will hold address of the protocol meta-data.
5010///
5011llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012 const ObjCProtocolDecl *PD) {
5013
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005014 // This routine is called for @protocol only. So, we must build definition
5015 // of protocol's meta-data (not a reference to it!)
5016 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005017 llvm::Constant *Init =
5018 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5019 ObjCTypes.ExternalProtocolPtrTy);
5020
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005021 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005022 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005023
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005024 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5025 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005026 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005027 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005028 CGM.getModule(),
5029 Init->getType(), false,
5030 llvm::GlobalValue::WeakAnyLinkage,
5031 Init,
5032 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005033 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5034 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005035 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005036 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005037}
5038
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005039/// GenerateCategory - Build metadata for a category implementation.
5040/// struct _category_t {
5041/// const char * const name;
5042/// struct _class_t *const cls;
5043/// const struct _method_list_t * const instance_methods;
5044/// const struct _method_list_t * const class_methods;
5045/// const struct _protocol_list_t * const protocols;
5046/// const struct _prop_list_t * const properties;
5047/// }
5048///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005049void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005050 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005051 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005052 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5053 "_$_" + OCD->getNameAsString());
5054 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005055 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005056
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005057 std::vector<llvm::Constant*> Values(6);
5058 Values[0] = GetClassName(OCD->getIdentifier());
5059 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005060 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005061 if (Interface->hasAttr<WeakImportAttr>())
5062 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5063
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005064 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005065 std::vector<llvm::Constant*> Methods;
5066 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005067 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005068 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005069
5070 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005071 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005072 // Instance methods should always be defined.
5073 Methods.push_back(GetMethodConstant(*i));
5074 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075
5076 Values[2] = EmitMethodList(MethodListName,
5077 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005078 Methods);
5079
5080 MethodListName = Prefix;
5081 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5082 OCD->getNameAsString();
5083 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005084 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005085 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005086 // Class methods should always be defined.
5087 Methods.push_back(GetMethodConstant(*i));
5088 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005089
5090 Values[3] = EmitMethodList(MethodListName,
5091 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005092 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005093 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005094 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005095 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005096 llvm::SmallString<256> ExtName;
5097 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5098 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005099 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005100 + Interface->getName() + "_$_"
5101 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005102 Category->protocol_begin(),
5103 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005104 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5105 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005106 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005107 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5108 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005109 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005110
5111 llvm::Constant *Init =
5112 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005113 Values);
5114 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005115 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005116 false,
5117 llvm::GlobalValue::InternalLinkage,
5118 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005119 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005120 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005121 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005122 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005123 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005124 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005125
5126 // Determine if this category is also "non-lazy".
5127 if (ImplementationIsNonLazy(OCD))
5128 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005129}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005130
5131/// GetMethodConstant - Return a struct objc_method constant for the
5132/// given method if it has been defined. The result is null if the
5133/// method has not been defined. The return value has type MethodPtrTy.
5134llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005135 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005136 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005137 if (!Fn)
5138 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005139
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005140 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005141 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005142 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005143 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005144 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005145 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005146 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005147}
5148
5149/// EmitMethodList - Build meta-data for method declarations
5150/// struct _method_list_t {
5151/// uint32_t entsize; // sizeof(struct _objc_method)
5152/// uint32_t method_count;
5153/// struct _objc_method method_list[method_count];
5154/// }
5155///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005156llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5157 const char *Section,
5158 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005159 // Return null for empty list.
5160 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005161 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005162
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005163 std::vector<llvm::Constant*> Values(3);
5164 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005165 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005166 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005167 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005168 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005169 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005170 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005171 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005172 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005173
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005174 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005175 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005176 llvm::GlobalValue::InternalLinkage,
5177 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005178 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005179 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005180 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005181 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005182 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005183 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005184 ObjCTypes.MethodListnfABIPtrTy);
5185}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005186
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005187/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5188/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005189llvm::GlobalVariable *
5190CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5191 const ObjCIvarDecl *Ivar) {
5192 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005193 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005194 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005195 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005196 CGM.getModule().getGlobalVariable(Name);
5197 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005198 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005199 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005200 false,
5201 llvm::GlobalValue::ExternalLinkage,
5202 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005203 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005204 return IvarOffsetGV;
5205}
5206
Daniel Dunbare83be122010-04-02 21:14:02 +00005207llvm::Constant *
5208CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5209 const ObjCIvarDecl *Ivar,
5210 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005211 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005213 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005214 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005215 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005216
Mike Stumpf5408fe2009-05-16 07:57:57 +00005217 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5218 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005219 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5220 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5221 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005222 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005223 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005224 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005225 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005226 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005227}
5228
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005229/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005230/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005231/// IvarListnfABIPtrTy.
5232/// struct _ivar_t {
5233/// unsigned long int *offset; // pointer to ivar offset location
5234/// char *name;
5235/// char *type;
5236/// uint32_t alignment;
5237/// uint32_t size;
5238/// }
5239/// struct _ivar_list_t {
5240/// uint32 entsize; // sizeof(struct _ivar_t)
5241/// uint32 count;
5242/// struct _iver_t list[count];
5243/// }
5244///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005245
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005246llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247 const ObjCImplementationDecl *ID) {
5248
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005249 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005250
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005251 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5252 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005254 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005255
Daniel Dunbar91636d62009-04-20 00:33:43 +00005256 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005257 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005258 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005259
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005260 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5261 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005262 // Ignore unnamed bit-fields.
5263 if (!IVD->getDeclName())
5264 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005265 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005266 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005267 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5268 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005269 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005270 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005271 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005272 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005273 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005274 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005275 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005276 // NOTE. Size of a bitfield does not match gcc's, because of the
5277 // way bitfields are treated special in each. But I am told that
5278 // 'size' for bitfield ivars is ignored by the runtime so it does
5279 // not matter. If it matters, there is enough info to get the
5280 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005281 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005282 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005283 }
5284 // Return null for empty list.
5285 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005286 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005287 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005288 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005289 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5290 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005291 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005292 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005293 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005294 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005295 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5296 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005297 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005298 llvm::GlobalValue::InternalLinkage,
5299 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005300 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005301 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005302 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005303 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304
Chris Lattnerad64e022009-07-17 23:57:13 +00005305 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005306 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005307}
5308
5309llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005310 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005311 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Fariborz Jahanianda320092009-01-29 19:24:30 +00005313 if (!Entry) {
5314 // We use the initializer as a marker of whether this is a forward
5315 // reference or not. At module finalization we add the empty
5316 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005317 Entry =
5318 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5319 llvm::GlobalValue::ExternalLinkage,
5320 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005321 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005322 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324
Fariborz Jahanianda320092009-01-29 19:24:30 +00005325 return Entry;
5326}
5327
5328/// GetOrEmitProtocol - Generate the protocol meta-data:
5329/// @code
5330/// struct _protocol_t {
5331/// id isa; // NULL
5332/// const char * const protocol_name;
5333/// const struct _protocol_list_t * protocol_list; // super protocols
5334/// const struct method_list_t * const instance_methods;
5335/// const struct method_list_t * const class_methods;
5336/// const struct method_list_t *optionalInstanceMethods;
5337/// const struct method_list_t *optionalClassMethods;
5338/// const struct _prop_list_t * properties;
5339/// const uint32_t size; // sizeof(struct _protocol_t)
5340/// const uint32_t flags; // = 0
5341/// }
5342/// @endcode
5343///
5344
5345llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005346 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005347 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005348
Fariborz Jahanianda320092009-01-29 19:24:30 +00005349 // Early exit if a defining object has already been generated.
5350 if (Entry && Entry->hasInitializer())
5351 return Entry;
5352
Fariborz Jahanianda320092009-01-29 19:24:30 +00005353 // Construct method lists.
5354 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5355 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005356 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005357 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005358 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005359 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005360 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5361 OptInstanceMethods.push_back(C);
5362 } else {
5363 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005364 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005365 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005366
5367 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005368 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005369 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005370 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005371 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5372 OptClassMethods.push_back(C);
5373 } else {
5374 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005375 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005377
Fariborz Jahanianda320092009-01-29 19:24:30 +00005378 std::vector<llvm::Constant*> Values(10);
5379 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005380 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005381 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005382 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5383 PD->protocol_begin(),
5384 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005385
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005386 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005387 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005388 "__DATA, __objc_const",
5389 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005390 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005391 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005392 "__DATA, __objc_const",
5393 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005394 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005395 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005396 "__DATA, __objc_const",
5397 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005398 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005399 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005400 "__DATA, __objc_const",
5401 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005402 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005403 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005405 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005406 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005407 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005408 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005409 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005410
Fariborz Jahanianda320092009-01-29 19:24:30 +00005411 if (Entry) {
5412 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005413 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005414 Entry->setInitializer(Init);
5415 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005416 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005417 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5418 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5419 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005420 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005421 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005423 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005424 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005425 CGM.AddUsedGlobal(Entry);
5426
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005427 // Use this protocol meta-data to build protocol list table in section
5428 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005429 llvm::GlobalVariable *PTGV =
5430 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5431 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5432 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005433 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005434 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005435 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005436 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005437 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 return Entry;
5439}
5440
5441/// EmitProtocolList - Generate protocol list meta-data:
5442/// @code
5443/// struct _protocol_list_t {
5444/// long protocol_count; // Note, this is 32/64 bit
5445/// struct _protocol_t[protocol_count];
5446/// }
5447/// @endcode
5448///
5449llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005450CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5451 ObjCProtocolDecl::protocol_iterator begin,
5452 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005453 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454
Fariborz Jahanianda320092009-01-29 19:24:30 +00005455 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005456 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005457 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005458
Daniel Dunbar948e2582009-02-15 07:36:20 +00005459 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005460 llvm::SmallString<256> TmpName;
5461 Name.toVector(TmpName);
5462 llvm::GlobalVariable *GV =
5463 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005464 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005465 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005466
Daniel Dunbar948e2582009-02-15 07:36:20 +00005467 for (; begin != end; ++begin)
5468 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5469
Fariborz Jahanianda320092009-01-29 19:24:30 +00005470 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005471 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005472 ObjCTypes.ProtocolnfABIPtrTy));
5473
Fariborz Jahanianda320092009-01-29 19:24:30 +00005474 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005475 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005476 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005478 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005479 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480 ProtocolRefs.size()),
5481 ProtocolRefs);
5482
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005483 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005484 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005485 llvm::GlobalValue::InternalLinkage,
5486 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005487 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005488 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005489 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005490 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005491 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005492 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005493 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005494}
5495
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005496/// GetMethodDescriptionConstant - This routine build following meta-data:
5497/// struct _objc_method {
5498/// SEL _cmd;
5499/// char *method_type;
5500/// char *_imp;
5501/// }
5502
5503llvm::Constant *
5504CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5505 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005506 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005507 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5508 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005509 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005510 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005511 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005512 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005513}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005514
5515/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5516/// This code gen. amounts to generating code for:
5517/// @code
5518/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5519/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005520///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005521LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005522 CodeGen::CodeGenFunction &CGF,
5523 QualType ObjectTy,
5524 llvm::Value *BaseValue,
5525 const ObjCIvarDecl *Ivar,
5526 unsigned CVRQualifiers) {
5527 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005528 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5529 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005530}
5531
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005532llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005533 CodeGen::CodeGenFunction &CGF,
5534 const ObjCInterfaceDecl *Interface,
5535 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005536 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005537}
5538
Fariborz Jahanian46551122009-02-04 00:22:57 +00005539CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005540 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005541 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005542 QualType ResultType,
5543 Selector Sel,
5544 llvm::Value *Receiver,
5545 QualType Arg0Ty,
5546 bool IsSuper,
5547 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005548 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5549 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005550 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005551 llvm::Value *Arg0 = Receiver;
5552 if (!IsSuper)
5553 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005554
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005555 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005556 // FIXME. This is too much work to get the ABI-specific result type needed to
5557 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005558 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005559 = Types.getFunctionInfo(ResultType, CallArgList(),
5560 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005561 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005562 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005563 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005564 if (IsSuper) {
5565 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5566 Name += "objc_msgSendSuper2_stret_fixup";
5567 } else {
5568 Fn = ObjCTypes.getMessageSendStretFixupFn();
5569 Name += "objc_msgSend_stret_fixup";
5570 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005571 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5572 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5573 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005574 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005575 if (IsSuper) {
5576 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5577 Name += "objc_msgSendSuper2_fixup";
5578 } else {
5579 Fn = ObjCTypes.getMessageSendFixupFn();
5580 Name += "objc_msgSend_fixup";
5581 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005582 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005583 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005584 Name += '_';
5585 std::string SelName(Sel.getAsString());
5586 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005587 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005588 if (SelName[i] == ':')
5589 SelName[i] = '_';
5590 Name += SelName;
5591 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5592 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005593 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005594 std::vector<llvm::Constant*> Values(2);
5595 Values[0] = Fn;
5596 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005597 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005598 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005599 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005600 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005601 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005602 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005603 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005604 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005605 }
5606 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005607
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005608 CallArgList ActualArgs;
5609 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005610 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005611 ObjCTypes.MessageRefCPtrTy));
5612 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005613 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005614 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005615 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5616 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005617 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005618 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005619 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005620 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005621}
5622
5623/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005624CodeGen::RValue
5625CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005626 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005627 QualType ResultType,
5628 Selector Sel,
5629 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005630 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005631 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005632 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005633 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005634 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5635 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005636 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005637 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005638 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639 Receiver, CGF.getContext().getObjCIdType(),
5640 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005641}
5642
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005643llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005644CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005645 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5646
Daniel Dunbardfff2302009-03-02 05:18:14 +00005647 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005649 false, llvm::GlobalValue::ExternalLinkage,
5650 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005651 }
5652
5653 return GV;
5654}
5655
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005656llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5657 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005658 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005660 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005661 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005662 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663 Entry =
5664 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005665 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005666 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005667 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005668 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005669 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005670 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005671 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005672 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005673 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005674
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005675 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005676}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005677
Daniel Dunbar11394522009-04-18 08:51:00 +00005678llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005679CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005680 const ObjCInterfaceDecl *ID) {
5681 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005682
Daniel Dunbar11394522009-04-18 08:51:00 +00005683 if (!Entry) {
5684 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5685 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005686 Entry =
5687 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005688 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005689 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005690 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005691 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005692 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005693 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005694 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005695 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005696 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005697
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005698 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005699}
5700
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005701/// EmitMetaClassRef - Return a Value * of the address of _class_t
5702/// meta-data
5703///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005704llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5705 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005706 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5707 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005708 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005710 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005711 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005713 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005714 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005715 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005716 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005717 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005718 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719 ObjCTypes.ClassnfABIPtrTy));
5720
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005721 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005722 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005724 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005725}
5726
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005727/// GetClass - Return a reference to the class for the given interface
5728/// decl.
5729llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5730 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005731 if (ID->hasAttr<WeakImportAttr>()) {
5732 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5733 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5734 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5735 }
5736
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005737 return EmitClassRef(Builder, ID);
5738}
5739
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005740/// Generates a message send where the super is the receiver. This is
5741/// a message send to self with special delivery semantics indicating
5742/// which class's method should be called.
5743CodeGen::RValue
5744CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005745 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005746 QualType ResultType,
5747 Selector Sel,
5748 const ObjCInterfaceDecl *Class,
5749 bool isCategoryImpl,
5750 llvm::Value *Receiver,
5751 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005752 const CodeGen::CallArgList &CallArgs,
5753 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 // ...
5755 // Create and init a super structure; this is a (receiver, class)
5756 // pair we will pass to objc_msgSendSuper.
5757 llvm::Value *ObjCSuper =
5758 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005760 llvm::Value *ReceiverAsObject =
5761 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5762 CGF.Builder.CreateStore(ReceiverAsObject,
5763 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005764
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005765 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005766 llvm::Value *Target;
5767 if (IsClassMessage) {
5768 if (isCategoryImpl) {
5769 // Message sent to "super' in a class method defined in
5770 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005771 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005772 Target = CGF.Builder.CreateStructGEP(Target, 0);
5773 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005774 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005775 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005776 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005777 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005778
Mike Stumpf5408fe2009-05-16 07:57:57 +00005779 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5780 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005781 const llvm::Type *ClassTy =
5782 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5783 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5784 CGF.Builder.CreateStore(Target,
5785 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005786
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005787 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005788 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5789 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005790 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005791 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005792 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005793 ObjCSuper, ObjCTypes.SuperPtrCTy,
5794 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005795}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005796
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005797llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005798 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005799 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005800
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005801 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005802 llvm::Constant *Casted =
5803 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5804 ObjCTypes.SelectorPtrTy);
5805 Entry =
5806 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5807 llvm::GlobalValue::InternalLinkage,
5808 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005809 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005810 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005811 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005812
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005813 if (lval)
5814 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005815 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005816}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005817/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005818/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005819///
5820void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005821 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005822 llvm::Value *dst,
5823 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005824 const llvm::Type * SrcTy = src->getType();
5825 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005826 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005827 assert(Size <= 8 && "does not support size > 8");
5828 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5829 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005830 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5831 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005832 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5833 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005834 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5835 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005836 return;
5837}
5838
5839/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5840/// objc_assign_strongCast (id src, id *dst)
5841///
5842void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005843 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005844 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005845 const llvm::Type * SrcTy = src->getType();
5846 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005847 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005848 assert(Size <= 8 && "does not support size > 8");
5849 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005851 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5852 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005853 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5854 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005855 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005856 src, dst, "weakassign");
5857 return;
5858}
5859
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005860void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005861 CodeGen::CodeGenFunction &CGF,
5862 llvm::Value *DestPtr,
5863 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005864 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005865 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5866 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005867 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005868 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005869 return;
5870}
5871
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005872/// EmitObjCWeakRead - Code gen for loading value of a __weak
5873/// object: objc_read_weak (id *src)
5874///
5875llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005876 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005877 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005878 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5880 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005881 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005882 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005883 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005884 return read_weak;
5885}
5886
5887/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5888/// objc_assign_weak (id src, id *dst)
5889///
5890void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005891 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005892 const llvm::Type * SrcTy = src->getType();
5893 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005894 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005895 assert(Size <= 8 && "does not support size > 8");
5896 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5897 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005898 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5899 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005900 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5901 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005902 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005903 src, dst, "weakassign");
5904 return;
5905}
5906
5907/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5908/// objc_assign_global (id src, id *dst)
5909///
5910void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005911 llvm::Value *src, llvm::Value *dst,
5912 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005913 const llvm::Type * SrcTy = src->getType();
5914 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005915 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005916 assert(Size <= 8 && "does not support size > 8");
5917 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5918 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005919 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5920 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005921 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5922 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005923 if (!threadlocal)
5924 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5925 src, dst, "globalassign");
5926 else
5927 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5928 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005929 return;
5930}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005931
John McCall740e8072010-07-21 00:41:47 +00005932namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005933 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005934 llvm::Value *SyncExitFn;
5935 llvm::Value *SyncArg;
5936 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5937 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5938
5939 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5940 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5941 }
5942 };
5943}
5944
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005945void
John McCallf1549f62010-07-06 01:34:17 +00005946CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5947 const ObjCAtSynchronizedStmt &S) {
5948 // Evaluate the lock operand. This should dominate the cleanup.
5949 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005950
John McCallf1549f62010-07-06 01:34:17 +00005951 // Acquire the lock.
5952 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5953 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5954 ->setDoesNotThrow();
5955
5956 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005957 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5958 ObjCTypes.getSyncExitFn(),
5959 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005960
John McCallf1549f62010-07-06 01:34:17 +00005961 // Emit the body of the statement.
5962 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005963
John McCallf1549f62010-07-06 01:34:17 +00005964 // Pop the lock-release cleanup.
5965 CGF.PopCleanupBlock();
5966}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005967
John McCallf1549f62010-07-06 01:34:17 +00005968namespace {
5969 struct CatchHandler {
5970 const VarDecl *Variable;
5971 const Stmt *Body;
5972 llvm::BasicBlock *Block;
5973 llvm::Value *TypeInfo;
5974 };
John McCall8e3f8612010-07-13 22:12:14 +00005975
John McCall1f0fca52010-07-21 07:22:38 +00005976 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005977 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5978 MightThrow(MightThrow), Fn(Fn) {}
5979 bool MightThrow;
5980 llvm::Value *Fn;
5981
5982 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5983 if (!MightThrow) {
5984 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5985 return;
5986 }
5987
5988 CGF.EmitCallOrInvoke(Fn, 0, 0);
5989 }
5990 };
John McCallf1549f62010-07-06 01:34:17 +00005991}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005992
John McCall5a180392010-07-24 00:37:23 +00005993llvm::Constant *
5994CGObjCNonFragileABIMac::GetEHType(QualType T) {
5995 // There's a particular fixed type info for 'id'.
5996 if (T->isObjCIdType() ||
5997 T->isObjCQualifiedIdType()) {
5998 llvm::Constant *IDEHType =
5999 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6000 if (!IDEHType)
6001 IDEHType =
6002 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6003 false,
6004 llvm::GlobalValue::ExternalLinkage,
6005 0, "OBJC_EHTYPE_id");
6006 return IDEHType;
6007 }
6008
6009 // All other types should be Objective-C interface pointer types.
6010 const ObjCObjectPointerType *PT =
6011 T->getAs<ObjCObjectPointerType>();
6012 assert(PT && "Invalid @catch type.");
6013 const ObjCInterfaceType *IT = PT->getInterfaceType();
6014 assert(IT && "Invalid @catch type.");
6015 return GetInterfaceEHType(IT->getDecl(), false);
6016}
6017
John McCallf1549f62010-07-06 01:34:17 +00006018void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6019 const ObjCAtTryStmt &S) {
6020 // Jump destination for falling out of catch bodies.
6021 CodeGenFunction::JumpDest Cont;
6022 if (S.getNumCatchStmts())
6023 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006024
John McCallf1549f62010-07-06 01:34:17 +00006025 CodeGenFunction::FinallyInfo FinallyInfo;
6026 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6027 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6028 ObjCTypes.getObjCBeginCatchFn(),
6029 ObjCTypes.getObjCEndCatchFn(),
6030 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006031
John McCallf1549f62010-07-06 01:34:17 +00006032 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006033
John McCallf1549f62010-07-06 01:34:17 +00006034 // Enter the catch, if there is one.
6035 if (S.getNumCatchStmts()) {
6036 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6037 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006038 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006039
John McCallf1549f62010-07-06 01:34:17 +00006040 Handlers.push_back(CatchHandler());
6041 CatchHandler &Handler = Handlers.back();
6042 Handler.Variable = CatchDecl;
6043 Handler.Body = CatchStmt->getCatchBody();
6044 Handler.Block = CGF.createBasicBlock("catch");
6045
6046 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006047 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006048 Handler.TypeInfo = 0; // catch-all
6049 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006050 break;
6051 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006052
John McCall5a180392010-07-24 00:37:23 +00006053 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006054 }
John McCallf1549f62010-07-06 01:34:17 +00006055
6056 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6057 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6058 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006059 }
John McCallf1549f62010-07-06 01:34:17 +00006060
6061 // Emit the try body.
6062 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006063
John McCallf1549f62010-07-06 01:34:17 +00006064 // Leave the try.
6065 if (S.getNumCatchStmts())
6066 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006067
John McCallf1549f62010-07-06 01:34:17 +00006068 // Remember where we were.
6069 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006070
John McCallf1549f62010-07-06 01:34:17 +00006071 // Emit the handlers.
6072 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6073 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006074
John McCallf1549f62010-07-06 01:34:17 +00006075 CGF.EmitBlock(Handler.Block);
6076 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006077
John McCallf1549f62010-07-06 01:34:17 +00006078 // Enter the catch.
6079 llvm::CallInst *Exn =
6080 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6081 "exn.adjusted");
6082 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006083
John McCallf1549f62010-07-06 01:34:17 +00006084 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006085 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006086 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6087 EndCatchMightThrow,
6088 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006089
John McCallf1549f62010-07-06 01:34:17 +00006090 // Bind the catch parameter if it exists.
6091 if (const VarDecl *CatchParam = Handler.Variable) {
6092 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6093 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006094
John McCallf1549f62010-07-06 01:34:17 +00006095 CGF.EmitLocalBlockVarDecl(*CatchParam);
6096 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006097 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006098
John McCallf1549f62010-07-06 01:34:17 +00006099 CGF.ObjCEHValueStack.push_back(Exn);
6100 CGF.EmitStmt(Handler.Body);
6101 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006102
John McCallf1549f62010-07-06 01:34:17 +00006103 // Leave the earlier cleanup.
6104 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006105
John McCallf1549f62010-07-06 01:34:17 +00006106 CGF.EmitBranchThroughCleanup(Cont);
6107 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006108
John McCallf1549f62010-07-06 01:34:17 +00006109 // Go back to the try-statement fallthrough.
6110 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006111
John McCallf1549f62010-07-06 01:34:17 +00006112 // Pop out of the normal cleanup on the finally.
6113 if (S.getFinallyStmt())
6114 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006115
John McCallff8e1152010-07-23 21:56:41 +00006116 if (Cont.isValid())
6117 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006118}
6119
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006120/// EmitThrowStmt - Generate code for a throw statement.
6121void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6122 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006123 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006124 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006125 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006126 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006127 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006128 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006129 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006130 "Unexpected rethrow outside @catch block.");
6131 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006132 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006133 }
6134
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006135 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006136 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6137 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6138 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006139 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006140 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006141 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006142 } else {
6143 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6144 ->setDoesNotReturn();
6145 CGF.Builder.CreateUnreachable();
6146 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006147
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006148 // Clear the insertion point to indicate we are in unreachable code.
6149 CGF.Builder.ClearInsertionPoint();
6150}
Daniel Dunbare588b992009-03-01 04:46:24 +00006151
John McCall5a180392010-07-24 00:37:23 +00006152llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006153CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006154 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006155 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006156
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006157 // If we don't need a definition, return the entry if found or check
6158 // if we use an external reference.
6159 if (!ForDefinition) {
6160 if (Entry)
6161 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006162
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006163 // If this type (or a super class) has the __objc_exception__
6164 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006165 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006166 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006167 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006168 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006169 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006170 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006171 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006172 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006173
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006174 // Otherwise we need to either make a new entry or fill in the
6175 // initializer.
6176 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006177 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006178 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006179 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006180 CGM.getModule().getGlobalVariable(VTableName);
6181 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006182 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6183 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006184 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006185 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006186
Chris Lattner77b89b82010-06-27 07:15:29 +00006187 llvm::Value *VTableIdx =
6188 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006189
6190 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006191 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006192 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006193 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006194 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006195 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006196
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006197 if (Entry) {
6198 Entry->setInitializer(Init);
6199 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006200 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006201 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006202 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006203 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006204 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006205 }
6206
Daniel Dunbar04d40782009-04-14 06:00:08 +00006207 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006208 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006209 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6210 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006211
6212 if (ForDefinition) {
6213 Entry->setSection("__DATA,__objc_const");
6214 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6215 } else {
6216 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6217 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006218
6219 return Entry;
6220}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006221
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006222/* *** */
6223
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006224CodeGen::CGObjCRuntime *
6225CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006226 return new CGObjCMac(CGM);
6227}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006228
6229CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006230CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006231 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006232}