blob: 47e303fb1eb68ff79507c6a7b36f0b73f2a06d24 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallf1549f62010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000027
John McCall87bb5822010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000043
Daniel Dunbar97776872009-04-22 07:32:20 +000044// Common CGObjCRuntime functions, these don't belong here, but they
45// don't belong in CGObjCRuntime either so we will live with it for
46// now.
47
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000053
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000054 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
55 // in here; it should never be necessary because that should be the lexical
56 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000057
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 // If we know have an implementation (and the ivar is in it) then
59 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 if (ID && ID->getClassInterface() == Container)
62 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
63 else
64 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000065
66 // Compute field index.
67 //
68 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
69 // implemented. This should be fixed to get the information from the layout
70 // directly.
71 unsigned Index = 0;
72 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
73 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
74 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
75 if (Ivar == Ivars[k])
76 break;
77 ++Index;
78 }
79 assert(Index != Ivars.size() && "Ivar is not inside container!");
80
Daniel Dunbar532d4da2009-05-03 13:15:50 +000081 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000103 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000109
John McCall0953e762009-09-24 19:53:00 +0000110 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
111 Quals.addCVRQualifiers(CVRQualifiers);
112
Daniel Dunbar56229f52010-04-05 16:20:33 +0000113 if (!Ivar->isBitField())
114 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000115
Daniel Dunbar56229f52010-04-05 16:20:33 +0000116 // We need to compute the bit offset for the bit-field, the offset is to the
117 // byte. Note, there is a subtle invariant here: we can only call this routine
118 // on non-synthesized ivars but we may be called for synthesized ivars.
119 // However, a synthesized ivar can never be a bit-field, so this is safe.
120 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
121 uint64_t BitFieldSize =
122 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000123
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000124 // Allocate a new CGBitFieldInfo object to describe this access.
125 //
126 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
127 // layout object. However, this is blocked on other cleanups to the
128 // Objective-C code, so for now we just live with allocating a bunch of these
129 // objects.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000130
Daniel Dunbarab970f92010-04-13 20:58:55 +0000131 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000132 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000133 AI.FieldIndex = 0;
134 AI.FieldByteOffset = 0;
135 AI.FieldBitStart = BitOffset;
136 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
137 AI.AccessAlignment = 0;
138 AI.TargetBitOffset = 0;
139 AI.TargetBitWidth = BitFieldSize;
140
Daniel Dunbar2df25692010-04-15 05:09:32 +0000141 CGBitFieldInfo *Info =
142 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
143 IvarTy->isSignedIntegerType());
144
Daniel Dunbar56229f52010-04-05 16:20:33 +0000145 // FIXME: We need to set a very conservative alignment on this, or make sure
146 // that the runtime is doing the right thing.
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000147 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000148}
149
150///
151
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000152namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000153
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000154typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000155
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000156// FIXME: We should find a nicer way to make the labels for metadata, string
157// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000158
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000159class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000160protected:
161 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000163private:
164 llvm::Constant *getMessageSendFn() const {
165 // id objc_msgSend (id, SEL, ...)
166 std::vector<const llvm::Type*> Params;
167 Params.push_back(ObjectPtrTy);
168 Params.push_back(SelectorPtrTy);
169 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000170 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
171 Params, true),
172 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000173 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000175 llvm::Constant *getMessageSendStretFn() const {
176 // id objc_msgSend_stret (id, SEL, ...)
177 std::vector<const llvm::Type*> Params;
178 Params.push_back(ObjectPtrTy);
179 Params.push_back(SelectorPtrTy);
180 return
Owen Anderson0032b272009-08-13 21:57:51 +0000181 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182 Params, true),
183 "objc_msgSend_stret");
184
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000186
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000187 llvm::Constant *getMessageSendFpretFn() const {
188 // FIXME: This should be long double on x86_64?
189 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
190 std::vector<const llvm::Type*> Params;
191 Params.push_back(ObjectPtrTy);
192 Params.push_back(SelectorPtrTy);
193 return
Owen Anderson0032b272009-08-13 21:57:51 +0000194 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
195 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000196 Params,
197 true),
198 "objc_msgSend_fpret");
199
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000200 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000201
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000202 llvm::Constant *getMessageSendSuperFn() const {
203 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
204 const char *SuperName = "objc_msgSendSuper";
205 std::vector<const llvm::Type*> Params;
206 Params.push_back(SuperPtrTy);
207 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000209 Params, true),
210 SuperName);
211 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000213 llvm::Constant *getMessageSendSuperFn2() const {
214 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
215 const char *SuperName = "objc_msgSendSuper2";
216 std::vector<const llvm::Type*> Params;
217 Params.push_back(SuperPtrTy);
218 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000219 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000220 Params, true),
221 SuperName);
222 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000223
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000224 llvm::Constant *getMessageSendSuperStretFn() const {
225 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
226 // SEL op, ...)
227 std::vector<const llvm::Type*> Params;
228 Params.push_back(Int8PtrTy);
229 Params.push_back(SuperPtrTy);
230 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000231 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000232 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000233 Params, true),
234 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000235 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000236
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000237 llvm::Constant *getMessageSendSuperStretFn2() const {
238 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
239 // SEL op, ...)
240 std::vector<const llvm::Type*> Params;
241 Params.push_back(Int8PtrTy);
242 Params.push_back(SuperPtrTy);
243 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000244 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000245 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000246 Params, true),
247 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000248 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000249
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000250 llvm::Constant *getMessageSendSuperFpretFn() const {
251 // There is no objc_msgSendSuper_fpret? How can that work?
252 return getMessageSendSuperFn();
253 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000254
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000255 llvm::Constant *getMessageSendSuperFpretFn2() const {
256 // There is no objc_msgSendSuper_fpret? How can that work?
257 return getMessageSendSuperFn2();
258 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000259
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260protected:
261 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000262
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000263public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000264 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000265 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000266
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000267 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
268 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000270 /// PtrObjectPtrTy - LLVM type for id *
271 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000273 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000274 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000275 /// ProtocolPtrTy - LLVM type for external protocol handles
276 /// (typeof(Protocol))
277 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000278
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000279 // SuperCTy - clang type for struct objc_super.
280 QualType SuperCTy;
281 // SuperPtrCTy - clang type for struct objc_super *.
282 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000283
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000284 /// SuperTy - LLVM type for struct objc_super.
285 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000286 /// SuperPtrTy - LLVM type for struct objc_super *.
287 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000288
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000289 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
290 /// in GCC parlance).
291 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000292
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000293 /// PropertyListTy - LLVM type for struct objc_property_list
294 /// (_prop_list_t in GCC parlance).
295 const llvm::StructType *PropertyListTy;
296 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
297 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000299 // MethodTy - LLVM type for struct objc_method.
300 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000301
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000302 /// CacheTy - LLVM type for struct objc_cache.
303 const llvm::Type *CacheTy;
304 /// CachePtrTy - LLVM type for struct objc_cache *.
305 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000306
Chris Lattner72db6c32009-04-22 02:44:54 +0000307 llvm::Constant *getGetPropertyFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000311 llvm::SmallVector<CanQualType,4> Params;
312 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
313 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000314 Params.push_back(IdType);
315 Params.push_back(SelType);
316 Params.push_back(Ctx.LongTy);
317 Params.push_back(Ctx.BoolTy);
318 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000319 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000320 FunctionType::ExtInfo()),
321 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000324
Chris Lattner72db6c32009-04-22 02:44:54 +0000325 llvm::Constant *getSetPropertyFn() {
326 CodeGen::CodeGenTypes &Types = CGM.getTypes();
327 ASTContext &Ctx = CGM.getContext();
328 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000329 llvm::SmallVector<CanQualType,6> Params;
330 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
331 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000332 Params.push_back(IdType);
333 Params.push_back(SelType);
334 Params.push_back(Ctx.LongTy);
335 Params.push_back(IdType);
336 Params.push_back(Ctx.BoolTy);
337 Params.push_back(Ctx.BoolTy);
338 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000339 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000340 FunctionType::ExtInfo()),
341 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000344
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000345
346 llvm::Constant *getCopyStructFn() {
347 CodeGen::CodeGenTypes &Types = CGM.getTypes();
348 ASTContext &Ctx = CGM.getContext();
349 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
350 llvm::SmallVector<CanQualType,5> Params;
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.VoidPtrTy);
353 Params.push_back(Ctx.LongTy);
354 Params.push_back(Ctx.BoolTy);
355 Params.push_back(Ctx.BoolTy);
356 const llvm::FunctionType *FTy =
357 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
358 FunctionType::ExtInfo()),
359 false);
360 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
361 }
362
Chris Lattner72db6c32009-04-22 02:44:54 +0000363 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
365 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000366 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000367 llvm::SmallVector<CanQualType,1> Params;
368 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000369 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000370 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000371 FunctionType::ExtInfo()),
372 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
374 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000375
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000376 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000377 llvm::Constant *getGcReadWeakFn() {
378 // id objc_read_weak (id *)
379 std::vector<const llvm::Type*> Args;
380 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000382 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000384 }
385
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000386 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000387 llvm::Constant *getGcAssignWeakFn() {
388 // id objc_assign_weak (id, id *)
389 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
390 Args.push_back(ObjectPtrTy->getPointerTo());
391 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000392 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000395
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000396 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000397 llvm::Constant *getGcAssignGlobalFn() {
398 // id objc_assign_global(id, id *)
399 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
400 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000401 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000402 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000403 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000405
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000406 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
407 llvm::Constant *getGcAssignThreadLocalFn() {
408 // id objc_assign_threadlocal(id src, id * dest)
409 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
410 Args.push_back(ObjectPtrTy->getPointerTo());
411 llvm::FunctionType *FTy =
412 llvm::FunctionType::get(ObjectPtrTy, Args, false);
413 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
414 }
415
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000416 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000418 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000419 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
420 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000421 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000422 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000423 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
425 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000426
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000427 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
428 llvm::Constant *GcMemmoveCollectableFn() {
429 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
430 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
431 Args.push_back(Int8PtrTy);
432 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
435 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000436
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000437 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000438 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000439 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000440 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
441 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000443 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
445 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000446
447 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 llvm::Constant *getExceptionThrowFn() {
449 // void objc_exception_throw(id)
450 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
451 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000452 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000453 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000455
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000456 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
457 llvm::Constant *getExceptionRethrowFn() {
458 // void objc_exception_rethrow(void)
459 std::vector<const llvm::Type*> Args;
460 llvm::FunctionType *FTy =
461 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
462 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
463 }
464
Daniel Dunbar1c566672009-02-24 01:43:46 +0000465 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000466 llvm::Constant *getSyncEnterFn() {
467 // void objc_sync_enter (id)
468 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
469 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000470 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000471 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000473
Daniel Dunbar1c566672009-02-24 01:43:46 +0000474 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000475 llvm::Constant *getSyncExitFn() {
476 // void objc_sync_exit (id)
477 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
478 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000479 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000480 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
481 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000482
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000486
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
489 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000490
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendStretFn(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
493 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000494
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000495 llvm::Constant *getSendStretFn2(bool IsSuper) const {
496 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
497 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000498
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000499 llvm::Constant *getSendFpretFn(bool IsSuper) const {
500 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
501 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000503 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
504 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
505 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000506
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000507 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
508 ~ObjCCommonTypesHelper(){}
509};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000510
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000511/// ObjCTypesHelper - Helper class that encapsulates lazy
512/// construction of varies types used during ObjC generation.
513class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000514public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000515 /// SymtabTy - LLVM type for struct objc_symtab.
516 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000517 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
518 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000519 /// ModuleTy - LLVM type for struct objc_module.
520 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000521
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000522 /// ProtocolTy - LLVM type for struct objc_protocol.
523 const llvm::StructType *ProtocolTy;
524 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
525 const llvm::Type *ProtocolPtrTy;
526 /// ProtocolExtensionTy - LLVM type for struct
527 /// objc_protocol_extension.
528 const llvm::StructType *ProtocolExtensionTy;
529 /// ProtocolExtensionTy - LLVM type for struct
530 /// objc_protocol_extension *.
531 const llvm::Type *ProtocolExtensionPtrTy;
532 /// MethodDescriptionTy - LLVM type for struct
533 /// objc_method_description.
534 const llvm::StructType *MethodDescriptionTy;
535 /// MethodDescriptionListTy - LLVM type for struct
536 /// objc_method_description_list.
537 const llvm::StructType *MethodDescriptionListTy;
538 /// MethodDescriptionListPtrTy - LLVM type for struct
539 /// objc_method_description_list *.
540 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000541 /// ProtocolListTy - LLVM type for struct objc_property_list.
542 const llvm::Type *ProtocolListTy;
543 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
544 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000545 /// CategoryTy - LLVM type for struct objc_category.
546 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000547 /// ClassTy - LLVM type for struct objc_class.
548 const llvm::StructType *ClassTy;
549 /// ClassPtrTy - LLVM type for struct objc_class *.
550 const llvm::Type *ClassPtrTy;
551 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
552 const llvm::StructType *ClassExtensionTy;
553 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
554 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000555 // IvarTy - LLVM type for struct objc_ivar.
556 const llvm::StructType *IvarTy;
557 /// IvarListTy - LLVM type for struct objc_ivar_list.
558 const llvm::Type *IvarListTy;
559 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
560 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000561 /// MethodListTy - LLVM type for struct objc_method_list.
562 const llvm::Type *MethodListTy;
563 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
564 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000565
Anders Carlsson124526b2008-09-09 10:10:21 +0000566 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
567 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Anders Carlsson124526b2008-09-09 10:10:21 +0000569 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionTryEnterFn() {
571 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000572 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000573 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000574 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575 Params, false),
576 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000578
579 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000580 llvm::Constant *getExceptionTryExitFn() {
581 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000582 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000583 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000584 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585 Params, false),
586 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000588
589 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000590 llvm::Constant *getExceptionExtractFn() {
591 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000592 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
593 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000594 Params, false),
595 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000596
Chris Lattner34b02a12009-04-22 02:26:14 +0000597 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Anders Carlsson124526b2008-09-09 10:10:21 +0000599 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000600 llvm::Constant *getExceptionMatchFn() {
601 std::vector<const llvm::Type*> Params;
602 Params.push_back(ClassPtrTy);
603 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000605 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606 Params, false),
607 "objc_exception_match");
608
Chris Lattner34b02a12009-04-22 02:26:14 +0000609 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610
Anders Carlsson124526b2008-09-09 10:10:21 +0000611 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000612 llvm::Constant *getSetJmpFn() {
613 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000614 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000615 return
Owen Anderson0032b272009-08-13 21:57:51 +0000616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000617 Params, false),
618 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Chris Lattner34b02a12009-04-22 02:26:14 +0000620 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000622public:
623 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000624 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000625};
626
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000627/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000628/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000629class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000630public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // MethodListnfABITy - LLVM for struct _method_list_t
633 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
636 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // ProtocolnfABITy = LLVM for struct _protocol_t
639 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Daniel Dunbar948e2582009-02-15 07:36:20 +0000641 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
642 const llvm::Type *ProtocolnfABIPtrTy;
643
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000644 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
645 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000647 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
648 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000650 // ClassnfABITy - LLVM for struct _class_t
651 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000652
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000653 // ClassnfABIPtrTy - LLVM for struct _class_t*
654 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000656 // IvarnfABITy - LLVM for struct _ivar_t
657 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000659 // IvarListnfABITy - LLVM for struct _ivar_list_t
660 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000662 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
663 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000665 // ClassRonfABITy - LLVM for struct _class_ro_t
666 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000668 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
669 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000671 // CategorynfABITy - LLVM for struct _category_t
672 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000673
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000674 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000675
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000676 // MessageRefTy - LLVM for:
677 // struct _message_ref_t {
678 // IMP messenger;
679 // SEL name;
680 // };
681 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000682 // MessageRefCTy - clang type for struct _message_ref_t
683 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000685 // MessageRefPtrTy - LLVM for struct _message_ref_t*
686 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000687 // MessageRefCPtrTy - clang type for struct _message_ref_t*
688 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Fariborz Jahanianef163782009-02-05 01:13:09 +0000690 // MessengerTy - Type of the messenger (shown as IMP above)
691 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000692
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000693 // SuperMessageRefTy - LLVM for:
694 // struct _super_message_ref_t {
695 // SUPER_IMP messenger;
696 // SEL name;
697 // };
698 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000700 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
701 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000702
Chris Lattner1c02f862009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendFixupFn() {
704 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
705 std::vector<const llvm::Type*> Params;
706 Params.push_back(ObjectPtrTy);
707 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 Params, true),
710 "objc_msgSend_fixup");
711 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Chris Lattner1c02f862009-04-22 02:53:24 +0000713 llvm::Constant *getMessageSendFpretFixupFn() {
714 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
715 std::vector<const llvm::Type*> Params;
716 Params.push_back(ObjectPtrTy);
717 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000719 Params, true),
720 "objc_msgSend_fpret_fixup");
721 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000722
Chris Lattner1c02f862009-04-22 02:53:24 +0000723 llvm::Constant *getMessageSendStretFixupFn() {
724 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
725 std::vector<const llvm::Type*> Params;
726 Params.push_back(ObjectPtrTy);
727 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000728 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000729 Params, true),
730 "objc_msgSend_stret_fixup");
731 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Chris Lattner1c02f862009-04-22 02:53:24 +0000733 llvm::Constant *getMessageSendIdFixupFn() {
734 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
735 std::vector<const llvm::Type*> Params;
736 Params.push_back(ObjectPtrTy);
737 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000738 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000739 Params, true),
740 "objc_msgSendId_fixup");
741 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742
Chris Lattner1c02f862009-04-22 02:53:24 +0000743 llvm::Constant *getMessageSendIdStretFixupFn() {
744 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
745 std::vector<const llvm::Type*> Params;
746 Params.push_back(ObjectPtrTy);
747 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000748 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000749 Params, true),
750 "objc_msgSendId_stret_fixup");
751 }
752 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000754 // struct _super_message_ref_t*, ...)
755 std::vector<const llvm::Type*> Params;
756 Params.push_back(SuperPtrTy);
757 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000758 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000759 Params, true),
760 "objc_msgSendSuper2_fixup");
761 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Chris Lattner1c02f862009-04-22 02:53:24 +0000763 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000765 // struct _super_message_ref_t*, ...)
766 std::vector<const llvm::Type*> Params;
767 Params.push_back(SuperPtrTy);
768 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000769 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000770 Params, true),
771 "objc_msgSendSuper2_stret_fixup");
772 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773
774
775
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000776 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
777 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000778 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000780 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000781 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000783 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000784 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000785
Chris Lattner8a569112009-04-22 02:15:23 +0000786 llvm::Constant *getUnwindResumeOrRethrowFn() {
787 std::vector<const llvm::Type*> Params;
788 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000789 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000790 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000792 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
793 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000794 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Chris Lattner8a569112009-04-22 02:15:23 +0000796 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000797 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000798 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000799 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000800
Chris Lattner8a569112009-04-22 02:15:23 +0000801 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Chris Lattner8a569112009-04-22 02:15:23 +0000803 llvm::Constant *getObjCBeginCatchFn() {
804 std::vector<const llvm::Type*> Params;
805 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000806 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000807 Params, false),
808 "objc_begin_catch");
809 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000810
811 const llvm::StructType *EHTypeTy;
812 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000813
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000814 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
815 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000816};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000817
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000818class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000819public:
820 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000821 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000822 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000823 unsigned ivar_bytepos;
824 unsigned ivar_size;
825 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000826 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000827
828 // Allow sorting based on byte pos.
829 bool operator<(const GC_IVAR &b) const {
830 return ivar_bytepos < b.ivar_bytepos;
831 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000832 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000833
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000834 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000835 public:
836 unsigned skip;
837 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000839 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000840 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000842protected:
843 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000844 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000845 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000846 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000848 // gc ivar layout bitmap calculation helper caches.
849 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
850 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000852 /// LazySymbols - Symbols to generate a lazy reference for. See
853 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000854 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000855
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000856 /// DefinedSymbols - External symbols which are defined by this
857 /// module. The symbols in this list and LazySymbols are used to add
858 /// special linker symbols which ensure that Objective-C modules are
859 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000860 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000861
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000862 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000863 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000864
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000865 /// MethodVarNames - uniqued method variable names.
866 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000867
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000868 /// DefinedCategoryNames - list of category names in form Class_Category.
869 llvm::SetVector<std::string> DefinedCategoryNames;
870
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000871 /// MethodVarTypes - uniqued method type signatures. We have to use
872 /// a StringMap here because have no other unique reference.
873 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000874
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000875 /// MethodDefinitions - map of methods which have been defined in
876 /// this translation unit.
877 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000878
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000879 /// PropertyNames - uniqued method variable names.
880 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 /// ClassReferences - uniqued class references.
883 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000885 /// SelectorReferences - uniqued selector references.
886 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000888 /// Protocols - Protocols for which an objc_protocol structure has
889 /// been emitted. Forward declarations are handled by creating an
890 /// empty structure whose initializer is filled in when/if defined.
891 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000893 /// DefinedProtocols - Protocols which have actually been
894 /// defined. We should not need this, see FIXME in GenerateProtocol.
895 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000896
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000897 /// DefinedClasses - List of defined classes.
898 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000899
900 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
901 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000903 /// DefinedCategories - List of defined categories.
904 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000905
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000906 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
907 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000909 /// GetNameForMethod - Return a name for the given method.
910 /// \param[out] NameOut - The return value.
911 void GetNameForMethod(const ObjCMethodDecl *OMD,
912 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000913 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000915 /// GetMethodVarName - Return a unique constant for the given
916 /// selector's name. The return value has type char *.
917 llvm::Constant *GetMethodVarName(Selector Sel);
918 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
919 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000921 /// GetMethodVarType - Return a unique constant for the given
922 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000924 // FIXME: This is a horrible name.
925 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000926 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000928 /// GetPropertyName - Return a unique constant for the given
929 /// name. The return value has type char *.
930 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000932 // FIXME: This can be dropped once string functions are unified.
933 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
934 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000935
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000936 /// GetClassName - Return a unique constant for the given selector's
937 /// name. The return value has type char *.
938 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000939
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000940 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
941
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000942 /// BuildIvarLayout - Builds ivar layout bitmap for the class
943 /// implementation for the __strong or __weak case.
944 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000945 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
946 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000947
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000948 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000949
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000950 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000951 unsigned int BytePos, bool ForStrongLayout,
952 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000953 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000954 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000955 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000956 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000957 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000958 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000959
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000960 /// GetIvarLayoutName - Returns a unique constant for the given
961 /// ivar layout bitmap.
962 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
963 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000964
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000965 /// EmitPropertyList - Emit the given property list. The return
966 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000967 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000968 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000969 const ObjCContainerDecl *OCD,
970 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000971
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000972 /// PushProtocolProperties - Push protocol's property on the input stack.
973 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
974 std::vector<llvm::Constant*> &Properties,
975 const Decl *Container,
976 const ObjCProtocolDecl *PROTO,
977 const ObjCCommonTypesHelper &ObjCTypes);
978
Fariborz Jahanianda320092009-01-29 19:24:30 +0000979 /// GetProtocolRef - Return a reference to the internal protocol
980 /// description, creating an empty one if it has not been
981 /// defined. The return value has type ProtocolPtrTy.
982 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000983
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000984 /// CreateMetadataVar - Create a global variable with internal
985 /// linkage for use by the Objective-C runtime.
986 ///
987 /// This is a convenience wrapper which not only creates the
988 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000989 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000990 ///
991 /// \param Name - The variable name.
992 /// \param Init - The variable initializer; this is also used to
993 /// define the type of the variable.
994 /// \param Section - The section the variable should go into, or 0.
995 /// \param Align - The alignment for the variable, or 0.
996 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000997 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000998 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000999 llvm::Constant *Init,
1000 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001001 unsigned Align,
1002 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001003
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001004 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001005 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001006 QualType ResultType,
1007 llvm::Value *Sel,
1008 llvm::Value *Arg0,
1009 QualType Arg0Ty,
1010 bool IsSuper,
1011 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001012 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001013 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001014
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001015 /// EmitImageInfo - Emit the image info marker used to encode some module
1016 /// level information.
1017 void EmitImageInfo();
1018
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001019public:
Owen Anderson69243822009-07-13 04:10:07 +00001020 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001021 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001022
David Chisnall0d13f6f2010-01-23 02:40:42 +00001023 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001024
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001025 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1026 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027
Fariborz Jahanianda320092009-01-29 19:24:30 +00001028 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001029
Fariborz Jahanianda320092009-01-29 19:24:30 +00001030 /// GetOrEmitProtocol - Get the protocol object for the given
1031 /// declaration, emitting it if necessary. The return value has type
1032 /// ProtocolPtrTy.
1033 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001034
Fariborz Jahanianda320092009-01-29 19:24:30 +00001035 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1036 /// object for the given declaration, emitting it if needed. These
1037 /// forward references will be filled in with empty bodies if no
1038 /// definition is seen. The return value has type ProtocolPtrTy.
1039 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001040 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
1041 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &);
1042
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001043};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001044
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001045class CGObjCMac : public CGObjCCommonMac {
1046private:
1047 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001048
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001049 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001050 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001051 void EmitModuleInfo();
1052
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001053 /// EmitModuleSymols - Emit module symbols, the list of defined
1054 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001055 llvm::Constant *EmitModuleSymbols();
1056
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001057 /// FinishModule - Write out global data structures at the end of
1058 /// processing a translation unit.
1059 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001060
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001061 /// EmitClassExtension - Generate the class extension structure used
1062 /// to store the weak ivar layout and properties. The return value
1063 /// has type ClassExtensionPtrTy.
1064 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1065
1066 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1067 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001068 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001069 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001070
1071 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1072 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001074 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001075 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001076 QualType ResultType,
1077 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001078 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001079 QualType Arg0Ty,
1080 bool IsSuper,
1081 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001082
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001083 /// EmitIvarList - Emit the ivar list for the given
1084 /// implementation. If ForClass is true the list of class ivars
1085 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1086 /// interface ivars will be emitted. The return value has type
1087 /// IvarListPtrTy.
1088 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001089 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001090
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001091 /// EmitMetaClass - Emit a forward reference to the class structure
1092 /// for the metaclass of the given interface. The return value has
1093 /// type ClassPtrTy.
1094 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1095
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001096 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001097 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001098 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1099 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001101
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001102 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001104 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001105
1106 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001107 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001108 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001109 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001110 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001111
1112 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001113 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001114 /// - TypeName: The name for the type containing the methods.
1115 /// - IsProtocol: True iff these methods are for a protocol.
1116 /// - ClassMethds: True iff these are class methods.
1117 /// - Required: When true, only "required" methods are
1118 /// listed. Similarly, when false only "optional" methods are
1119 /// listed. For classes this should always be true.
1120 /// - begin, end: The method list to output.
1121 ///
1122 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001123 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001124 const char *Section,
1125 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001126
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001127 /// GetOrEmitProtocol - Get the protocol object for the given
1128 /// declaration, emitting it if necessary. The return value has type
1129 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001130 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001131
1132 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1133 /// object for the given declaration, emitting it if needed. These
1134 /// forward references will be filled in with empty bodies if no
1135 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001136 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001137
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138 /// EmitProtocolExtension - Generate the protocol extension
1139 /// structure used to store optional instance and class methods, and
1140 /// protocol properties. The return value has type
1141 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001142 llvm::Constant *
1143 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1144 const ConstantVector &OptInstanceMethods,
1145 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146
1147 /// EmitProtocolList - Generate the list of referenced
1148 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001149 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001150 ObjCProtocolDecl::protocol_iterator begin,
1151 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001152
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001153 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1154 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001155 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1156 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001157
1158public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001159 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001160
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001161 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001162
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001163 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001164 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001165 QualType ResultType,
1166 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001167 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001168 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001169 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001170 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001171
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001172 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001173 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001174 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001175 QualType ResultType,
1176 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001177 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001178 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001179 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001180 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001181 const CallArgList &CallArgs,
1182 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001183
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001184 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001185 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001186
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001187 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1188 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001189
1190 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1191 /// untyped one.
1192 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1193 const ObjCMethodDecl *Method);
1194
John McCall5a180392010-07-24 00:37:23 +00001195 virtual llvm::Constant *GetEHType(QualType T);
1196
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001197 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001198
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001199 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001200
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001201 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001202 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001203
Chris Lattner74391b42009-03-22 21:03:39 +00001204 virtual llvm::Constant *GetPropertyGetFunction();
1205 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001206 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001207 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001208
John McCallf1549f62010-07-06 01:34:17 +00001209 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1210 const ObjCAtTryStmt &S);
1211 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1212 const ObjCAtSynchronizedStmt &S);
1213 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001214 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1215 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001216 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001217 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001218 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001219 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001220 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001221 llvm::Value *src, llvm::Value *dest,
1222 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001223 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001224 llvm::Value *src, llvm::Value *dest,
1225 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001226 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1227 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001228 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1229 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001230 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001231
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001232 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1233 QualType ObjectTy,
1234 llvm::Value *BaseValue,
1235 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001236 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001237 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001238 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001239 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001240};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001242class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001243private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001244 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001245 llvm::GlobalVariable* ObjCEmptyCacheVar;
1246 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001247
Daniel Dunbar11394522009-04-18 08:51:00 +00001248 /// SuperClassReferences - uniqued super class references.
1249 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001250
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001251 /// MetaClassReferences - uniqued meta class references.
1252 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001253
1254 /// EHTypeReferences - uniqued class ehtype references.
1255 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001257 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001258 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001259 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001260
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001261 /// DefinedMetaClasses - List of defined meta-classes.
1262 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1263
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001264 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001265 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001266 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001267
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001268 /// FinishNonFragileABIModule - Write out global data structures at the end of
1269 /// processing a translation unit.
1270 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001271
Daniel Dunbar463b8762009-05-15 21:48:48 +00001272 /// AddModuleClassList - Add the given list of class pointers to the
1273 /// module with the provided symbol and section names.
1274 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1275 const char *SymbolName,
1276 const char *SectionName);
1277
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001278 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1279 unsigned InstanceStart,
1280 unsigned InstanceSize,
1281 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001282 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001284 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001285 llvm::Constant *ClassRoGV,
1286 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001288 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001289
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001290 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001291
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001292 /// EmitMethodList - Emit the method list for the given
1293 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001294 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001295 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001296 const ConstantVector &Methods);
1297 /// EmitIvarList - Emit the ivar list for the given
1298 /// implementation. If ForClass is true the list of class ivars
1299 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1300 /// interface ivars will be emitted. The return value has type
1301 /// IvarListnfABIPtrTy.
1302 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001303
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001304 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001305 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001306 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001307
Fariborz Jahanianda320092009-01-29 19:24:30 +00001308 /// GetOrEmitProtocol - Get the protocol object for the given
1309 /// declaration, emitting it if necessary. The return value has type
1310 /// ProtocolPtrTy.
1311 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312
Fariborz Jahanianda320092009-01-29 19:24:30 +00001313 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1314 /// object for the given declaration, emitting it if needed. These
1315 /// forward references will be filled in with empty bodies if no
1316 /// definition is seen. The return value has type ProtocolPtrTy.
1317 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001318
Fariborz Jahanianda320092009-01-29 19:24:30 +00001319 /// EmitProtocolList - Generate the list of referenced
1320 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001321 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001322 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001323 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
Fariborz Jahanian46551122009-02-04 00:22:57 +00001325 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001326 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001327 QualType ResultType,
1328 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001329 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001330 QualType Arg0Ty,
1331 bool IsSuper,
1332 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001333
1334 /// GetClassGlobal - Return the global variable for the Objective-C
1335 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001336 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001337
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001338 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001339 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001340 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001341 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001342
Daniel Dunbar11394522009-04-18 08:51:00 +00001343 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1344 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001345 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1346 const ObjCInterfaceDecl *ID);
1347
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001348 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1349 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001350 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001351 const ObjCInterfaceDecl *ID);
1352
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001353 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1354 /// the given ivar.
1355 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001356 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001357 const ObjCInterfaceDecl *ID,
1358 const ObjCIvarDecl *Ivar);
1359
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001360 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1361 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001362 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1363 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001364
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001365 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001366 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001367 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001368 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001369
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001370 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001371 return "OBJC_METACLASS_$_";
1372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001373
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001374 const char *getClassSymbolPrefix() const {
1375 return "OBJC_CLASS_$_";
1376 }
1377
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001378 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001379 uint32_t &InstanceStart,
1380 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001381
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001382 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001383 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001384 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1385 return CGM.getContext().Selectors.getSelector(0, &II);
1386 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001387
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001388 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001389 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1390 return CGM.getContext().Selectors.getSelector(1, &II);
1391 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001392
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001393 /// ImplementationIsNonLazy - Check whether the given category or
1394 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001395 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001396
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001397public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001398 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001399 // FIXME. All stubs for now!
1400 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001401
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001402 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001403 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001404 QualType ResultType,
1405 Selector Sel,
1406 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001407 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001408 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001409 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001410
1411 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001412 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001413 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001414 QualType ResultType,
1415 Selector Sel,
1416 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001417 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 llvm::Value *Receiver,
1419 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001420 const CallArgList &CallArgs,
1421 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001422
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001423 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001424 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001425
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001426 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1427 bool lvalue = false)
1428 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001429
1430 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1431 /// untyped one.
1432 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1433 const ObjCMethodDecl *Method)
1434 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001435
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001436 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001437
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001438 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001439 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001440 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001441
John McCall5a180392010-07-24 00:37:23 +00001442 virtual llvm::Constant *GetEHType(QualType T);
1443
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001444 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001445 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001446 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001447 virtual llvm::Constant *GetPropertySetFunction() {
1448 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001449 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001450
1451 virtual llvm::Constant *GetCopyStructFunction() {
1452 return ObjCTypes.getCopyStructFn();
1453 }
1454
Chris Lattner74391b42009-03-22 21:03:39 +00001455 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001456 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001457 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001458
John McCallf1549f62010-07-06 01:34:17 +00001459 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1460 const ObjCAtTryStmt &S);
1461 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1462 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001463 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001464 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001465 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001466 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001467 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001468 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001469 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001470 llvm::Value *src, llvm::Value *dest,
1471 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001472 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001473 llvm::Value *src, llvm::Value *dest,
1474 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001475 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001476 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001477 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1478 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001479 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001480 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1481 QualType ObjectTy,
1482 llvm::Value *BaseValue,
1483 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001484 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001485 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001486 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001487 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001488};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001489
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001490} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001491
1492/* *** Helper Functions *** */
1493
1494/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001495static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001496 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001497 unsigned idx0,
1498 unsigned idx1) {
1499 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001500 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1501 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001502 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001503 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001504}
1505
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001506/// hasObjCExceptionAttribute - Return true if this class or any super
1507/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001508static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001509 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001510 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001511 return true;
1512 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001513 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001514 return false;
1515}
1516
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001517/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001518
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001519CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001520 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001521 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001522 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001523}
1524
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001525/// GetClass - Return a reference to the class for the given interface
1526/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001527llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001528 const ObjCInterfaceDecl *ID) {
1529 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001530}
1531
1532/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001533llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1534 bool lval) {
1535 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001536}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001537llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001538 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001539 return EmitSelector(Builder, Method->getSelector());
1540}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001541
John McCall5a180392010-07-24 00:37:23 +00001542llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1543 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1544 return 0;
1545}
1546
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001547/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001548/*
1549 struct __builtin_CFString {
1550 const int *isa; // point to __CFConstantStringClassReference
1551 int flags;
1552 const char *str;
1553 long length;
1554 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001555*/
1556
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001557/// or Generate a constant NSString object.
1558/*
1559 struct __builtin_NSString {
1560 const int *isa; // point to __NSConstantStringClassReference
1561 const char *str;
1562 unsigned int length;
1563 };
1564*/
1565
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001566llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001567 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001568 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1569 CGM.GetAddrOfConstantCFString(SL) :
1570 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001571}
1572
1573/// Generates a message send where the super is the receiver. This is
1574/// a message send to self with special delivery semantics indicating
1575/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001576CodeGen::RValue
1577CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001578 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001579 QualType ResultType,
1580 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001581 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001582 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001583 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001584 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001585 const CodeGen::CallArgList &CallArgs,
1586 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001587 // Create and init a super structure; this is a (receiver, class)
1588 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001589 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001590 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001591 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001592 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001593 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001594 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001595
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001596 // If this is a class message the metaclass is passed as the target.
1597 llvm::Value *Target;
1598 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001599 if (isCategoryImpl) {
1600 // Message sent to 'super' in a class method defined in a category
1601 // implementation requires an odd treatment.
1602 // If we are in a class method, we must retrieve the
1603 // _metaclass_ for the current class, pointed at by
1604 // the class's "isa" pointer. The following assumes that
1605 // isa" is the first ivar in a class (which it must be).
1606 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1607 Target = CGF.Builder.CreateStructGEP(Target, 0);
1608 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001609 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001610 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1611 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1612 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1613 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001614 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001615 }
1616 else if (isCategoryImpl)
1617 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1618 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001619 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1620 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1621 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001622 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001623 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1624 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001625 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001626 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001627 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001628 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001629 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001630 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001631 EmitSelector(CGF.Builder, Sel),
1632 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001633 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001634}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001635
1636/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001637CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001638 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001639 QualType ResultType,
1640 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001641 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001642 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001643 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001644 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001645 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001646 EmitSelector(CGF.Builder, Sel),
1647 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001648 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001649}
1650
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001651CodeGen::RValue
1652CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001653 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001654 QualType ResultType,
1655 llvm::Value *Sel,
1656 llvm::Value *Arg0,
1657 QualType Arg0Ty,
1658 bool IsSuper,
1659 const CallArgList &CallArgs,
1660 const ObjCMethodDecl *Method,
1661 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001662 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001663 if (!IsSuper)
1664 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001665 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001666 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001667 CGF.getContext().getObjCSelType()));
1668 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001669
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001670 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001671 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001672 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001673 const llvm::FunctionType *FTy =
1674 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001675
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001676 if (Method)
1677 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1678 CGM.getContext().getCanonicalType(ResultType) &&
1679 "Result type mismatch!");
1680
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001681 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001682 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001683 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001684 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001685 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1686 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1687 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001688 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001689 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001690 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001691 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001692 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001693 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001694}
1695
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001696static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1697 if (FQT.isObjCGCStrong())
1698 return Qualifiers::Strong;
1699
1700 if (FQT.isObjCGCWeak())
1701 return Qualifiers::Weak;
1702
1703 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1704 return Qualifiers::Strong;
1705
1706 if (const PointerType *PT = FQT->getAs<PointerType>())
1707 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1708
1709 return Qualifiers::GCNone;
1710}
1711
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001712llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001713 const llvm::SmallVectorImpl<const BlockDeclRefExpr *> &DeclRefs) {
1714 llvm::Constant *NullPtr =
1715 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanian5af12352010-08-05 15:52:12 +00001716 if ((CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ||
1717 DeclRefs.empty())
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001718 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001719 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001720 SkipIvars.clear();
1721 IvarsInfo.clear();
1722 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1723 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1724
1725 for (size_t i = 0; i < DeclRefs.size(); ++i) {
1726 const BlockDeclRefExpr *BDRE = DeclRefs[i];
1727 const ValueDecl *VD = BDRE->getDecl();
1728 CharUnits Offset = CGF.BlockDecls[VD];
1729 uint64_t FieldOffset = Offset.getQuantity();
1730 QualType Ty = VD->getType();
1731 assert(!Ty->isArrayType() &&
1732 "Array block variable should have been caught");
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001733 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001734 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1735 FieldOffset,
1736 true,
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001737 hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001738 continue;
1739 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001740
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001741 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1742 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1743 // __block variables are passed by their descriptior address. So, size
1744 // must reflect this.
1745 if (BDRE->isByRef())
1746 FieldSize = WordSizeInBits;
1747 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1748 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1749 FieldSize / WordSizeInBits));
1750 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1751 SkipIvars.push_back(GC_IVAR(FieldOffset,
1752 FieldSize / ByteSizeInBits));
1753 }
1754
1755 if (IvarsInfo.empty())
1756 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001757 // Sort on byte position in case we encounterred a union nested in
1758 // block variable type's aggregate type.
1759 if (hasUnion && !IvarsInfo.empty())
1760 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1761 if (hasUnion && !SkipIvars.empty())
1762 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001763
1764 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001765 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001766 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1767 printf("\n block variable layout for block: ");
1768 const unsigned char *s = (unsigned char*)BitMap.c_str();
1769 for (unsigned i = 0; i < BitMap.size(); i++)
1770 if (!(s[i] & 0xf0))
1771 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1772 else
1773 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1774 printf("\n");
1775 }
1776
1777 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001778}
1779
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001780llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001781 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001782 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001783 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001784 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1785
Owen Anderson3c4972d2009-07-29 18:54:39 +00001786 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001787 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001788}
1789
Fariborz Jahanianda320092009-01-29 19:24:30 +00001790void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001791 // FIXME: We shouldn't need this, the protocol decl should contain enough
1792 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001793 DefinedProtocols.insert(PD->getIdentifier());
1794
1795 // If we have generated a forward reference to this protocol, emit
1796 // it now. Otherwise do nothing, the protocol objects are lazily
1797 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001798 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001799 GetOrEmitProtocol(PD);
1800}
1801
Fariborz Jahanianda320092009-01-29 19:24:30 +00001802llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001803 if (DefinedProtocols.count(PD->getIdentifier()))
1804 return GetOrEmitProtocol(PD);
1805 return GetOrEmitProtocolRef(PD);
1806}
1807
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001808/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001809// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1810struct _objc_protocol {
1811struct _objc_protocol_extension *isa;
1812char *protocol_name;
1813struct _objc_protocol_list *protocol_list;
1814struct _objc__method_prototype_list *instance_methods;
1815struct _objc__method_prototype_list *class_methods
1816};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001817
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001818See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001819*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001820llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1821 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1822
1823 // Early exit if a defining object has already been generated.
1824 if (Entry && Entry->hasInitializer())
1825 return Entry;
1826
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001827 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001828 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001829 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1830
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001831 // Construct method lists.
1832 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1833 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001834 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001835 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001836 ObjCMethodDecl *MD = *i;
1837 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1838 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1839 OptInstanceMethods.push_back(C);
1840 } else {
1841 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 }
1844
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001845 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001846 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001847 ObjCMethodDecl *MD = *i;
1848 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1849 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1850 OptClassMethods.push_back(C);
1851 } else {
1852 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001853 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001854 }
1855
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001857 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001858 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001860 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001861 PD->protocol_begin(),
1862 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001864 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001865 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1866 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001867 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001868 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001869 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1870 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001871 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001872 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001873
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001874 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001875 // Already created, fix the linkage and update the initializer.
1876 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001877 Entry->setInitializer(Init);
1878 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001879 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001880 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001881 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001882 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001883 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001885 // FIXME: Is this necessary? Why only for protocol?
1886 Entry->setAlignment(4);
1887 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001888 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001889
1890 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001891}
1892
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001893llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1895
1896 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001897 // We use the initializer as a marker of whether this is a forward
1898 // reference or not. At module finalization we add the empty
1899 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001900 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001901 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001902 llvm::GlobalValue::ExternalLinkage,
1903 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001904 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001905 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906 // FIXME: Is this necessary? Why only for protocol?
1907 Entry->setAlignment(4);
1908 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001909
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001910 return Entry;
1911}
1912
1913/*
1914 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001915 uint32_t size;
1916 struct objc_method_description_list *optional_instance_methods;
1917 struct objc_method_description_list *optional_class_methods;
1918 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919 };
1920*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001921llvm::Constant *
1922CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1923 const ConstantVector &OptInstanceMethods,
1924 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001926 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001927 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001928 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001929 Values[1] =
1930 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001931 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001932 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1933 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001934 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001935 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001936 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1937 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001938 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001939 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001940
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001941 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001942 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001943 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001944 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001946 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001947 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001948
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001949 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001950 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001952 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001953}
1954
1955/*
1956 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001957 struct objc_protocol_list *next;
1958 long count;
1959 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001960 };
1961*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001962llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001963CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001964 ObjCProtocolDecl::protocol_iterator begin,
1965 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001966 std::vector<llvm::Constant*> ProtocolRefs;
1967
Daniel Dunbardbc93372008-08-21 21:57:41 +00001968 for (; begin != end; ++begin)
1969 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001970
1971 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001972 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001973 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001974
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001975 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001976 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001977
1978 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001979 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001980 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001981 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001982 ProtocolRefs.size() - 1);
1983 Values[2] =
1984 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1985 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001986 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001987
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001988 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001989 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001990 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001991 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001992 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001993}
1994
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001995void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1996 std::vector<llvm::Constant*> &Properties,
1997 const Decl *Container,
1998 const ObjCProtocolDecl *PROTO,
1999 const ObjCCommonTypesHelper &ObjCTypes) {
2000 std::vector<llvm::Constant*> Prop(2);
2001 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2002 E = PROTO->protocol_end(); P != E; ++P)
2003 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2004 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2005 E = PROTO->prop_end(); I != E; ++I) {
2006 const ObjCPropertyDecl *PD = *I;
2007 if (!PropertySet.insert(PD->getIdentifier()))
2008 continue;
2009 Prop[0] = GetPropertyName(PD->getIdentifier());
2010 Prop[1] = GetPropertyTypeString(PD, Container);
2011 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2012 }
2013}
2014
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002015/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002016 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002017 const char * const name;
2018 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002019 };
2020
2021 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002022 uint32_t entsize; // sizeof (struct _objc_property)
2023 uint32_t prop_count;
2024 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002025 };
2026*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002027llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002028 const Decl *Container,
2029 const ObjCContainerDecl *OCD,
2030 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002031 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002032 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002033 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2034 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002035 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002036 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002037 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002038 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002039 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002040 Prop));
2041 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002042 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002043 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
2044 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002045 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2046 ObjCTypes);
2047 }
2048 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2049 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2050 E = CD->protocol_end(); P != E; ++P)
2051 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2052 ObjCTypes);
2053 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002054
2055 // Return null for empty list.
2056 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002057 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002058
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002059 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002060 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002061 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002062 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2063 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002064 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002065 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002066 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002067 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002068
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002069 llvm::GlobalVariable *GV =
2070 CreateMetadataVar(Name, Init,
2071 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002072 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002073 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002074 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002075 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002076}
2077
2078/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002079 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002080 int count;
2081 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002082 };
2083*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002084llvm::Constant *
2085CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2086 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002087 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002088 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2089 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002090 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002091 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002092 Desc);
2093}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002094
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002095llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002096 const char *Section,
2097 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002098 // Return null for empty list.
2099 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002100 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002101
2102 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002103 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002105 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002106 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002107 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002108
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002109 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002110 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002111 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002112}
2113
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002114/*
2115 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002116 char *category_name;
2117 char *class_name;
2118 struct _objc_method_list *instance_methods;
2119 struct _objc_method_list *class_methods;
2120 struct _objc_protocol_list *protocols;
2121 uint32_t size; // <rdar://4585769>
2122 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002123 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002124*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002125void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002126 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002127
Mike Stumpf5408fe2009-05-16 07:57:57 +00002128 // FIXME: This is poor design, the OCD should have a pointer to the category
2129 // decl. Additionally, note that Category can be null for the @implementation
2130 // w/o an @interface case. Sema should just create one for us as it does for
2131 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002132 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002133 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002134 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002135
2136 llvm::SmallString<256> ExtName;
2137 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2138 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002139
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002140 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002141 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002142 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002143 // Instance methods should always be defined.
2144 InstanceMethods.push_back(GetMethodConstant(*i));
2145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002146 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002147 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002148 // Class methods should always be defined.
2149 ClassMethods.push_back(GetMethodConstant(*i));
2150 }
2151
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002152 std::vector<llvm::Constant*> Values(7);
2153 Values[0] = GetClassName(OCD->getIdentifier());
2154 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002155 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002156 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002157 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002158 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002159 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002160 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002161 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002162 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002163 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002164 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002165 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002166 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002167 Category->protocol_begin(),
2168 Category->protocol_end());
2169 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002170 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002171 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002172 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002173
2174 // If there is no category @interface then there can be no properties.
2175 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002176 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002177 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002178 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002179 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002180 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002181
Owen Anderson08e25242009-07-27 22:29:56 +00002182 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002183 Values);
2184
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002185 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002186 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002187 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002188 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002189 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002190 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002191}
2192
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193// FIXME: Get from somewhere?
2194enum ClassFlags {
2195 eClassFlags_Factory = 0x00001,
2196 eClassFlags_Meta = 0x00002,
2197 // <rdr://5142207>
2198 eClassFlags_HasCXXStructors = 0x02000,
2199 eClassFlags_Hidden = 0x20000,
2200 eClassFlags_ABI2_Hidden = 0x00010,
2201 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2202};
2203
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204/*
2205 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002206 Class isa;
2207 Class super_class;
2208 const char *name;
2209 long version;
2210 long info;
2211 long instance_size;
2212 struct _objc_ivar_list *ivars;
2213 struct _objc_method_list *methods;
2214 struct _objc_cache *cache;
2215 struct _objc_protocol_list *protocols;
2216 // Objective-C 1.0 extensions (<rdr://4585769>)
2217 const char *ivar_layout;
2218 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 };
2220
2221 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002222*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002224 DefinedSymbols.insert(ID->getIdentifier());
2225
Chris Lattner8ec03f52008-11-24 03:54:41 +00002226 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002228 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002229 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002230 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002231 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002232 Interface->protocol_begin(),
2233 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002235 if (ID->getNumIvarInitializers())
2236 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002237 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002238 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002239
2240 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002241 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002242 Flags |= eClassFlags_Hidden;
2243
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002244 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002245 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002246 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002247 // Instance methods should always be defined.
2248 InstanceMethods.push_back(GetMethodConstant(*i));
2249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002250 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002251 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002252 // Class methods should always be defined.
2253 ClassMethods.push_back(GetMethodConstant(*i));
2254 }
2255
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002256 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002257 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002258 ObjCPropertyImplDecl *PID = *i;
2259
2260 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2261 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2262
2263 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2264 if (llvm::Constant *C = GetMethodConstant(MD))
2265 InstanceMethods.push_back(C);
2266 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2267 if (llvm::Constant *C = GetMethodConstant(MD))
2268 InstanceMethods.push_back(C);
2269 }
2270 }
2271
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002272 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002273 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002275 // Record a reference to the super class.
2276 LazySymbols.insert(Super->getIdentifier());
2277
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002278 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002279 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 ObjCTypes.ClassPtrTy);
2281 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002282 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 }
2284 Values[ 2] = GetClassName(ID->getIdentifier());
2285 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002286 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2287 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2288 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002289 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002290 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002291 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002292 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002293 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002294 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002295 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002297 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002298 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002299 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002301 std::string Name("\01L_OBJC_CLASS_");
2302 Name += ClassName;
2303 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2304 // Check for a forward reference.
2305 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2306 if (GV) {
2307 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2308 "Forward metaclass reference has incorrect type.");
2309 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2310 GV->setInitializer(Init);
2311 GV->setSection(Section);
2312 GV->setAlignment(4);
2313 CGM.AddUsedGlobal(GV);
2314 }
2315 else
2316 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 DefinedClasses.push_back(GV);
2318}
2319
2320llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2321 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002322 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002324 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325
Daniel Dunbar04d40782009-04-14 06:00:08 +00002326 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002328
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329 std::vector<llvm::Constant*> Values(12);
2330 // The isa for the metaclass is the root of the hierarchy.
2331 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2332 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2333 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002334 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002335 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002337 // The super class for the metaclass is emitted as the name of the
2338 // super class. The runtime fixes this up to point to the
2339 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002340 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002342 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343 ObjCTypes.ClassPtrTy);
2344 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002345 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 }
2347 Values[ 2] = GetClassName(ID->getIdentifier());
2348 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002349 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2350 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2351 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002352 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002353 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002354 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002355 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002356 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002358 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002359 Values[ 9] = Protocols;
2360 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002361 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002362 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002363 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002364 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002365 Values);
2366
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002367 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002368 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002369
2370 // Check for a forward reference.
2371 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2372 if (GV) {
2373 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2374 "Forward metaclass reference has incorrect type.");
2375 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2376 GV->setInitializer(Init);
2377 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002378 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002379 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002380 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002381 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002383 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002384 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002385
2386 return GV;
2387}
2388
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002389llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002390 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002391
Mike Stumpf5408fe2009-05-16 07:57:57 +00002392 // FIXME: Should we look these up somewhere other than the module. Its a bit
2393 // silly since we only generate these while processing an implementation, so
2394 // exactly one pointer would work if know when we entered/exitted an
2395 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002396
2397 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002398 // Previously, metaclass with internal linkage may have been defined.
2399 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002400 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2401 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002402 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2403 "Forward metaclass reference has incorrect type.");
2404 return GV;
2405 } else {
2406 // Generate as an external reference to keep a consistent
2407 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002408 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002409 llvm::GlobalValue::ExternalLinkage,
2410 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002411 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002412 }
2413}
2414
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002415llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2416 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2417
2418 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2419 true)) {
2420 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2421 "Forward class metadata reference has incorrect type.");
2422 return GV;
2423 } else {
2424 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2425 llvm::GlobalValue::ExternalLinkage,
2426 0,
2427 Name);
2428 }
2429}
2430
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431/*
2432 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433 uint32_t size;
2434 const char *weak_ivar_layout;
2435 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002436 };
2437*/
2438llvm::Constant *
2439CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002441 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442
2443 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002444 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002445 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002446 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002447 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448
2449 // Return null if no extension bits are used.
2450 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002451 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002453 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002454 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002455 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002456 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002457 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458}
2459
2460/*
2461 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002462 char *ivar_name;
2463 char *ivar_type;
2464 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002465 };
2466
2467 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468 int ivar_count;
2469 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002470 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002471*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002472llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002473 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002474 std::vector<llvm::Constant*> Ivars, Ivar(3);
2475
2476 // When emitting the root class GCC emits ivar entries for the
2477 // actual class structure. It is not clear if we need to follow this
2478 // behavior; for now lets try and get away with not doing it. If so,
2479 // the cleanest solution would be to make up an ObjCInterfaceDecl
2480 // for the class.
2481 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002482 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002483
2484 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002485 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002486
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002487 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002488 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002489
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002490 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2491 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002492 // Ignore unnamed bit-fields.
2493 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002494 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002495 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2496 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002498 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002499 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 }
2501
2502 // Return null for empty list.
2503 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002504 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002505
2506 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002507 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002508 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002509 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002510 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002511 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002512
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002513 llvm::GlobalVariable *GV;
2514 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002515 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002516 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002517 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002518 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002519 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002520 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002521 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002522 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002523}
2524
2525/*
2526 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002527 SEL method_name;
2528 char *method_types;
2529 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002531
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002532 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002533 struct objc_method_list *obsolete;
2534 int count;
2535 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002536 };
2537*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002538
2539/// GetMethodConstant - Return a struct objc_method constant for the
2540/// given method if it has been defined. The result is null if the
2541/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002542llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002543 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002544 if (!Fn)
2545 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002546
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002547 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002548 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002549 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002550 ObjCTypes.SelectorPtrTy);
2551 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002552 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002553 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002554}
2555
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002556llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002557 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002558 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002559 // Return null for empty list.
2560 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002561 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002562
2563 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002564 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002565 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002566 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002567 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002568 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002569 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002570
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002571 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002572 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002573 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002574}
2575
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002576llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002577 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002578 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002579 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002580
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002581 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002582 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002583 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002584 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002585 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002586 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002587 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002588 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002589 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002590
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002591 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002592}
2593
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002594llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002595CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002596 llvm::Constant *Init,
2597 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002598 unsigned Align,
2599 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002600 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002601 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002602 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002603 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002604 if (Section)
2605 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002606 if (Align)
2607 GV->setAlignment(Align);
2608 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002609 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002610 return GV;
2611}
2612
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002613llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002614 // Abuse this interface function as a place to finalize.
2615 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002616 return NULL;
2617}
2618
Chris Lattner74391b42009-03-22 21:03:39 +00002619llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002620 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002621}
2622
Chris Lattner74391b42009-03-22 21:03:39 +00002623llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002624 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002625}
2626
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002627llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2628 return ObjCTypes.getCopyStructFn();
2629}
2630
Chris Lattner74391b42009-03-22 21:03:39 +00002631llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002632 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002633}
2634
John McCallf1549f62010-07-06 01:34:17 +00002635void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2636 return EmitTryOrSynchronizedStmt(CGF, S);
2637}
2638
2639void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2640 const ObjCAtSynchronizedStmt &S) {
2641 return EmitTryOrSynchronizedStmt(CGF, S);
2642}
2643
John McCallcc505292010-07-21 06:59:36 +00002644namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002645 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002646 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002647 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002648 llvm::Value *CallTryExitVar;
2649 llvm::Value *ExceptionData;
2650 ObjCTypesHelper &ObjCTypes;
2651 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002652 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002653 llvm::Value *CallTryExitVar,
2654 llvm::Value *ExceptionData,
2655 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002656 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002657 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2658
2659 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2660 // Check whether we need to call objc_exception_try_exit.
2661 // In optimized code, this branch will always be folded.
2662 llvm::BasicBlock *FinallyCallExit =
2663 CGF.createBasicBlock("finally.call_exit");
2664 llvm::BasicBlock *FinallyNoCallExit =
2665 CGF.createBasicBlock("finally.no_call_exit");
2666 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2667 FinallyCallExit, FinallyNoCallExit);
2668
2669 CGF.EmitBlock(FinallyCallExit);
2670 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2671 ->setDoesNotThrow();
2672
2673 CGF.EmitBlock(FinallyNoCallExit);
2674
2675 if (isa<ObjCAtTryStmt>(S)) {
2676 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002677 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2678 // Save the current cleanup destination in case there's
2679 // control flow inside the finally statement.
2680 llvm::Value *CurCleanupDest =
2681 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2682
John McCallcc505292010-07-21 06:59:36 +00002683 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2684
John McCalld96a8e72010-08-11 00:16:14 +00002685 if (CGF.HaveInsertPoint()) {
2686 CGF.Builder.CreateStore(CurCleanupDest,
2687 CGF.getNormalCleanupDestSlot());
2688 } else {
2689 // Currently, the end of the cleanup must always exist.
2690 CGF.EnsureInsertPoint();
2691 }
2692 }
John McCallcc505292010-07-21 06:59:36 +00002693 } else {
2694 // Emit objc_sync_exit(expr); as finally's sole statement for
2695 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002696 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002697 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2698 ->setDoesNotThrow();
2699 }
2700 }
2701 };
John McCall87bb5822010-07-31 23:20:56 +00002702
2703 class FragileHazards {
2704 CodeGenFunction &CGF;
2705 llvm::SmallVector<llvm::Value*, 20> Locals;
2706 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2707
2708 llvm::InlineAsm *ReadHazard;
2709 llvm::InlineAsm *WriteHazard;
2710
2711 llvm::FunctionType *GetAsmFnType();
2712
2713 void collectLocals();
2714 void emitReadHazard(CGBuilderTy &Builder);
2715
2716 public:
2717 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002718
John McCall87bb5822010-07-31 23:20:56 +00002719 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002720 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002721 };
2722}
2723
2724/// Create the fragile-ABI read and write hazards based on the current
2725/// state of the function, which is presumed to be immediately prior
2726/// to a @try block. These hazards are used to maintain correct
2727/// semantics in the face of optimization and the fragile ABI's
2728/// cavalier use of setjmp/longjmp.
2729FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2730 collectLocals();
2731
2732 if (Locals.empty()) return;
2733
2734 // Collect all the blocks in the function.
2735 for (llvm::Function::iterator
2736 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2737 BlocksBeforeTry.insert(&*I);
2738
2739 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2740
2741 // Create a read hazard for the allocas. This inhibits dead-store
2742 // optimizations and forces the values to memory. This hazard is
2743 // inserted before any 'throwing' calls in the protected scope to
2744 // reflect the possibility that the variables might be read from the
2745 // catch block if the call throws.
2746 {
2747 std::string Constraint;
2748 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2749 if (I) Constraint += ',';
2750 Constraint += "*m";
2751 }
2752
2753 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2754 }
2755
2756 // Create a write hazard for the allocas. This inhibits folding
2757 // loads across the hazard. This hazard is inserted at the
2758 // beginning of the catch path to reflect the possibility that the
2759 // variables might have been written within the protected scope.
2760 {
2761 std::string Constraint;
2762 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2763 if (I) Constraint += ',';
2764 Constraint += "=*m";
2765 }
2766
2767 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2768 }
2769}
2770
2771/// Emit a write hazard at the current location.
2772void FragileHazards::emitWriteHazard() {
2773 if (Locals.empty()) return;
2774
2775 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2776 ->setDoesNotThrow();
2777}
2778
John McCall87bb5822010-07-31 23:20:56 +00002779void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2780 assert(!Locals.empty());
2781 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2782 ->setDoesNotThrow();
2783}
2784
2785/// Emit read hazards in all the protected blocks, i.e. all the blocks
2786/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002787void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002788 if (Locals.empty()) return;
2789
2790 CGBuilderTy Builder(CGF.getLLVMContext());
2791
2792 // Iterate through all blocks, skipping those prior to the try.
2793 for (llvm::Function::iterator
2794 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2795 llvm::BasicBlock &BB = *FI;
2796 if (BlocksBeforeTry.count(&BB)) continue;
2797
2798 // Walk through all the calls in the block.
2799 for (llvm::BasicBlock::iterator
2800 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2801 llvm::Instruction &I = *BI;
2802
2803 // Ignore instructions that aren't non-intrinsic calls.
2804 // These are the only calls that can possibly call longjmp.
2805 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2806 if (isa<llvm::IntrinsicInst>(I))
2807 continue;
2808
2809 // Ignore call sites marked nounwind. This may be questionable,
2810 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2811 llvm::CallSite CS(&I);
2812 if (CS.doesNotThrow()) continue;
2813
John McCall0b251722010-08-04 05:59:32 +00002814 // Insert a read hazard before the call. This will ensure that
2815 // any writes to the locals are performed before making the
2816 // call. If the call throws, then this is sufficient to
2817 // guarantee correctness as long as it doesn't also write to any
2818 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002819 Builder.SetInsertPoint(&BB, BI);
2820 emitReadHazard(Builder);
2821 }
2822 }
2823}
2824
2825static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2826 if (V) S.insert(V);
2827}
2828
2829void FragileHazards::collectLocals() {
2830 // Compute a set of allocas to ignore.
2831 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2832 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2833 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2834 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2835
2836 // Collect all the allocas currently in the function. This is
2837 // probably way too aggressive.
2838 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2839 for (llvm::BasicBlock::iterator
2840 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2841 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2842 Locals.push_back(&*I);
2843}
2844
2845llvm::FunctionType *FragileHazards::GetAsmFnType() {
2846 std::vector<const llvm::Type *> Tys(Locals.size());
2847 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2848 Tys[I] = Locals[I]->getType();
2849 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002850}
2851
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002852/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002853
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002854 Objective-C setjmp-longjmp (sjlj) Exception Handling
2855 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002856
John McCallf1549f62010-07-06 01:34:17 +00002857 A catch buffer is a setjmp buffer plus:
2858 - a pointer to the exception that was caught
2859 - a pointer to the previous exception data buffer
2860 - two pointers of reserved storage
2861 Therefore catch buffers form a stack, with a pointer to the top
2862 of the stack kept in thread-local storage.
2863
2864 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2865 objc_exception_try_exit pops the given catch buffer, which is
2866 required to be the top of the EH stack.
2867 objc_exception_throw pops the top of the EH stack, writes the
2868 thrown exception into the appropriate field, and longjmps
2869 to the setjmp buffer. It crashes the process (with a printf
2870 and an abort()) if there are no catch buffers on the stack.
2871 objc_exception_extract just reads the exception pointer out of the
2872 catch buffer.
2873
2874 There's no reason an implementation couldn't use a light-weight
2875 setjmp here --- something like __builtin_setjmp, but API-compatible
2876 with the heavyweight setjmp. This will be more important if we ever
2877 want to implement correct ObjC/C++ exception interactions for the
2878 fragile ABI.
2879
2880 Note that for this use of setjmp/longjmp to be correct, we may need
2881 to mark some local variables volatile: if a non-volatile local
2882 variable is modified between the setjmp and the longjmp, it has
2883 indeterminate value. For the purposes of LLVM IR, it may be
2884 sufficient to make loads and stores within the @try (to variables
2885 declared outside the @try) volatile. This is necessary for
2886 optimized correctness, but is not currently being done; this is
2887 being tracked as rdar://problem/8160285
2888
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002889 The basic framework for a @try-catch-finally is as follows:
2890 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002891 objc_exception_data d;
2892 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002893 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002894
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002895 objc_exception_try_enter(&d);
2896 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002897 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002898 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002899 // exception path
2900 id _caught = objc_exception_extract(&d);
2901
2902 // enter new try scope for handlers
2903 if (!setjmp(d.jmp_buf)) {
2904 ... match exception and execute catch blocks ...
2905
2906 // fell off end, rethrow.
2907 _rethrow = _caught;
2908 ... jump-through-finally to finally_rethrow ...
2909 } else {
2910 // exception in catch block
2911 _rethrow = objc_exception_extract(&d);
2912 _call_try_exit = false;
2913 ... jump-through-finally to finally_rethrow ...
2914 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002915 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002916 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002917
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002918 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002919 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002920 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002921
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002922 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002923 ... dispatch to finally destination ...
2924
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002925 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002926 objc_exception_throw(_rethrow);
2927
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002928 finally_end:
2929 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002930
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002931 This framework differs slightly from the one gcc uses, in that gcc
2932 uses _rethrow to determine if objc_exception_try_exit should be called
2933 and if the object should be rethrown. This breaks in the face of
2934 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002935
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002936 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002937
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002938 - If there are no catch blocks, then we avoid emitting the second
2939 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002940
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002941 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2942 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002943
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002944 - FIXME: If there is no @finally block we can do a few more
2945 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002946
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002947 Rethrows and Jumps-Through-Finally
2948 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002949
John McCallf1549f62010-07-06 01:34:17 +00002950 '@throw;' is supported by pushing the currently-caught exception
2951 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002952
John McCallf1549f62010-07-06 01:34:17 +00002953 Branches through the @finally block are handled with an ordinary
2954 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2955 exceptions are not compatible with C++ exceptions, and this is
2956 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002957
John McCallf1549f62010-07-06 01:34:17 +00002958 @synchronized(expr) { stmt; } is emitted as if it were:
2959 id synch_value = expr;
2960 objc_sync_enter(synch_value);
2961 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002962*/
2963
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002964void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2965 const Stmt &S) {
2966 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002967
2968 // A destination for the fall-through edges of the catch handlers to
2969 // jump to.
2970 CodeGenFunction::JumpDest FinallyEnd =
2971 CGF.getJumpDestInCurrentScope("finally.end");
2972
2973 // A destination for the rethrow edge of the catch handlers to jump
2974 // to.
2975 CodeGenFunction::JumpDest FinallyRethrow =
2976 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002977
Daniel Dunbar1c566672009-02-24 01:43:46 +00002978 // For @synchronized, call objc_sync_enter(sync.expr). The
2979 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002980 // @synchronized. We can't avoid a temp here because we need the
2981 // value to be preserved. If the backend ever does liveness
2982 // correctly after setjmp, this will be unnecessary.
2983 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002984 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002985 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002986 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2987 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002988 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2989 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002990
2991 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2992 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002993 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002994
John McCall0b251722010-08-04 05:59:32 +00002995 // Allocate memory for the setjmp buffer. This needs to be kept
2996 // live throughout the try and catch blocks.
2997 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2998 "exceptiondata.ptr");
2999
John McCall87bb5822010-07-31 23:20:56 +00003000 // Create the fragile hazards. Note that this will not capture any
3001 // of the allocas required for exception processing, but will
3002 // capture the current basic block (which extends all the way to the
3003 // setjmp call) as "before the @try".
3004 FragileHazards Hazards(CGF);
3005
John McCallf1549f62010-07-06 01:34:17 +00003006 // Create a flag indicating whether the cleanup needs to call
3007 // objc_exception_try_exit. This is true except when
3008 // - no catches match and we're branching through the cleanup
3009 // just to rethrow the exception, or
3010 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003011 // The setjmp-safety rule here is that we should always store to this
3012 // variable in a place that dominates the branch through the cleanup
3013 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003014 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003015 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003016
John McCallf1549f62010-07-06 01:34:17 +00003017 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003018 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003019 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003020 CallTryExitVar,
3021 ExceptionData,
3022 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003023
3024 // Enter a try block:
3025 // - Call objc_exception_try_enter to push ExceptionData on top of
3026 // the EH stack.
3027 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3028 ->setDoesNotThrow();
3029
3030 // - Call setjmp on the exception data buffer.
3031 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3032 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3033 llvm::Value *SetJmpBuffer =
3034 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3035 llvm::CallInst *SetJmpResult =
3036 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3037 SetJmpResult->setDoesNotThrow();
3038
3039 // If setjmp returned 0, enter the protected block; otherwise,
3040 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003041 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3042 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003043 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003044 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3045 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003046
John McCallf1549f62010-07-06 01:34:17 +00003047 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003048 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003049 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003050 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003051 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003052
3053 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003054
John McCallf1549f62010-07-06 01:34:17 +00003055 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003056 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003057
John McCall87bb5822010-07-31 23:20:56 +00003058 // Don't optimize loads of the in-scope locals across this point.
3059 Hazards.emitWriteHazard();
3060
John McCallf1549f62010-07-06 01:34:17 +00003061 // For a @synchronized (or a @try with no catches), just branch
3062 // through the cleanup to the rethrow block.
3063 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3064 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003065 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003066 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003067
3068 // Otherwise, we have to match against the caught exceptions.
3069 } else {
John McCall0b251722010-08-04 05:59:32 +00003070 // Retrieve the exception object. We may emit multiple blocks but
3071 // nothing can cross this so the value is already in SSA form.
3072 llvm::CallInst *Caught =
3073 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3074 ExceptionData, "caught");
3075 Caught->setDoesNotThrow();
3076
John McCallf1549f62010-07-06 01:34:17 +00003077 // Push the exception to rethrow onto the EH value stack for the
3078 // benefit of any @throws in the handlers.
3079 CGF.ObjCEHValueStack.push_back(Caught);
3080
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003081 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003082
John McCall0b251722010-08-04 05:59:32 +00003083 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003084
John McCall0b251722010-08-04 05:59:32 +00003085 llvm::BasicBlock *CatchBlock = 0;
3086 llvm::BasicBlock *CatchHandler = 0;
3087 if (HasFinally) {
3088 // Enter a new exception try block (in case a @catch block
3089 // throws an exception).
3090 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3091 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003092
John McCall0b251722010-08-04 05:59:32 +00003093 llvm::CallInst *SetJmpResult =
3094 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3095 "setjmp.result");
3096 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003097
John McCall0b251722010-08-04 05:59:32 +00003098 llvm::Value *Threw =
3099 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3100
3101 CatchBlock = CGF.createBasicBlock("catch");
3102 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3103 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3104
3105 CGF.EmitBlock(CatchBlock);
3106 }
3107
3108 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003109
Daniel Dunbar55e40722008-09-27 07:03:52 +00003110 // Handle catch list. As a special case we check if everything is
3111 // matched and avoid generating code for falling off the end if
3112 // so.
3113 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003114 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3115 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003116
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003117 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003118 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003119
Anders Carlsson80f25672008-09-09 17:59:25 +00003120 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003121 if (!CatchParam) {
3122 AllMatched = true;
3123 } else {
John McCall183700f2009-09-21 23:43:11 +00003124 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003125
John McCallf1549f62010-07-06 01:34:17 +00003126 // catch(id e) always matches under this ABI, since only
3127 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003128 // FIXME: For the time being we also match id<X>; this should
3129 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003130 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003131 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003132 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003133
John McCallf1549f62010-07-06 01:34:17 +00003134 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003135 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003136 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3137
Anders Carlssondde0a942008-09-11 09:15:33 +00003138 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00003139 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003140 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003141
3142 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003143 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003144 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003145
Anders Carlssondde0a942008-09-11 09:15:33 +00003146 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003147
3148 // The scope of the catch variable ends right here.
3149 CatchVarCleanups.ForceCleanup();
3150
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003151 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003152 break;
3153 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003154
Steve Naroff14108da2009-07-10 23:34:53 +00003155 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003156 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003157
3158 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003159 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3160 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003161
3162 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003163 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003164
John McCallf1549f62010-07-06 01:34:17 +00003165 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003166 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3167 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003168 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003169
John McCallf1549f62010-07-06 01:34:17 +00003170 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3171 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003172
3173 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003174 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003175
Anders Carlsson80f25672008-09-09 17:59:25 +00003176 // Emit the @catch block.
3177 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003178
3179 // Collect any cleanups for the catch variable. The scope lasts until
3180 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003181 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003182
Steve Naroff7ba138a2009-03-03 19:52:17 +00003183 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003184 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003185
John McCallf1549f62010-07-06 01:34:17 +00003186 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003187 llvm::Value *Tmp =
3188 CGF.Builder.CreateBitCast(Caught,
3189 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003190 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003191 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003192
Anders Carlssondde0a942008-09-11 09:15:33 +00003193 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003194
3195 // We're done with the catch variable.
3196 CatchVarCleanups.ForceCleanup();
3197
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003198 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003199
Anders Carlsson80f25672008-09-09 17:59:25 +00003200 CGF.EmitBlock(NextCatchBlock);
3201 }
3202
John McCallf1549f62010-07-06 01:34:17 +00003203 CGF.ObjCEHValueStack.pop_back();
3204
John McCall0b251722010-08-04 05:59:32 +00003205 // If nothing wanted anything to do with the caught exception,
3206 // kill the extract call.
3207 if (Caught->use_empty())
3208 Caught->eraseFromParent();
3209
3210 if (!AllMatched)
3211 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3212
3213 if (HasFinally) {
3214 // Emit the exception handler for the @catch blocks.
3215 CGF.EmitBlock(CatchHandler);
3216
3217 // In theory we might now need a write hazard, but actually it's
3218 // unnecessary because there's no local-accessing code between
3219 // the try's write hazard and here.
3220 //Hazards.emitWriteHazard();
3221
3222 // Don't pop the catch handler; the throw already did.
3223 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003224 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003225 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003226 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003227
John McCall87bb5822010-07-31 23:20:56 +00003228 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003229 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003230
John McCallf1549f62010-07-06 01:34:17 +00003231 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003232 CGF.Builder.restoreIP(TryFallthroughIP);
3233 if (CGF.HaveInsertPoint())
3234 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003235 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003236 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003237
John McCallf1549f62010-07-06 01:34:17 +00003238 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003239 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003240 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003241 if (CGF.HaveInsertPoint()) {
John McCall0b251722010-08-04 05:59:32 +00003242 // Just look in the buffer for the exception to throw.
3243 llvm::CallInst *Caught =
3244 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3245 ExceptionData);
3246 Caught->setDoesNotThrow();
3247
3248 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), Caught)
John McCallf1549f62010-07-06 01:34:17 +00003249 ->setDoesNotThrow();
3250 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003251 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003252
John McCall87bb5822010-07-31 23:20:56 +00003253 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003254}
3255
3256void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003257 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003258 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003259
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003260 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3261 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003262 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003263 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3264 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003265 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003266 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003267 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003268 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269
John McCallf1549f62010-07-06 01:34:17 +00003270 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3271 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003272 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003273
3274 // Clear the insertion point to indicate we are in unreachable code.
3275 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003276}
3277
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003278/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003279/// object: objc_read_weak (id *src)
3280///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003281llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003282 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003283 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3285 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3286 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003287 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003288 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003289 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003290 return read_weak;
3291}
3292
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003293/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3294/// objc_assign_weak (id src, id *dst)
3295///
3296void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003297 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003298 const llvm::Type * SrcTy = src->getType();
3299 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003300 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003301 assert(Size <= 8 && "does not support size > 8");
3302 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003303 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003304 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3305 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003306 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3307 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003308 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003309 src, dst, "weakassign");
3310 return;
3311}
3312
Fariborz Jahanian58626502008-11-19 00:59:10 +00003313/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3314/// objc_assign_global (id src, id *dst)
3315///
3316void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003317 llvm::Value *src, llvm::Value *dst,
3318 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003319 const llvm::Type * SrcTy = src->getType();
3320 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003321 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003322 assert(Size <= 8 && "does not support size > 8");
3323 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003324 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003325 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3326 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003327 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3328 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003329 if (!threadlocal)
3330 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3331 src, dst, "globalassign");
3332 else
3333 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3334 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003335 return;
3336}
3337
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003338/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003339/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003340///
3341void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003342 llvm::Value *src, llvm::Value *dst,
3343 llvm::Value *ivarOffset) {
3344 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003345 const llvm::Type * SrcTy = src->getType();
3346 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003347 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003348 assert(Size <= 8 && "does not support size > 8");
3349 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003350 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003351 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3352 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003353 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3354 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003355 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3356 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003357 return;
3358}
3359
Fariborz Jahanian58626502008-11-19 00:59:10 +00003360/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3361/// objc_assign_strongCast (id src, id *dst)
3362///
3363void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003364 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003365 const llvm::Type * SrcTy = src->getType();
3366 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003367 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003368 assert(Size <= 8 && "does not support size > 8");
3369 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003370 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003371 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3372 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003373 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3374 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003375 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003376 src, dst, "weakassign");
3377 return;
3378}
3379
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003380void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003381 llvm::Value *DestPtr,
3382 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003383 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003384 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3385 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003386 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003387 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003388 return;
3389}
3390
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003391/// EmitObjCValueForIvar - Code Gen for ivar reference.
3392///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003393LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3394 QualType ObjectTy,
3395 llvm::Value *BaseValue,
3396 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003397 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003398 const ObjCInterfaceDecl *ID =
3399 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003400 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3401 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003402}
3403
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003404llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003405 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003406 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003407 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003408 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003409 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3410 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003411}
3412
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003413/* *** Private Interface *** */
3414
3415/// EmitImageInfo - Emit the image info marker used to encode some module
3416/// level information.
3417///
3418/// See: <rdr://4810609&4810587&4810587>
3419/// struct IMAGE_INFO {
3420/// unsigned version;
3421/// unsigned flags;
3422/// };
3423enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003424 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003425 eImageInfo_GarbageCollected = (1 << 1),
3426 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003427 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3428
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003429 // A flag indicating that the module has no instances of a @synthesize of a
3430 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003431 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003432};
3433
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003434void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003435 unsigned version = 0; // Version is unused?
3436 unsigned flags = 0;
3437
3438 // FIXME: Fix and continue?
3439 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3440 flags |= eImageInfo_GarbageCollected;
3441 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3442 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003443
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003444 // We never allow @synthesize of a superclass property.
3445 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003446
Chris Lattner77b89b82010-06-27 07:15:29 +00003447 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3448
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003449 // Emitted as int[2];
3450 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003451 llvm::ConstantInt::get(Int32Ty, version),
3452 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003453 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003454 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003455
3456 const char *Section;
3457 if (ObjCABI == 1)
3458 Section = "__OBJC, __image_info,regular";
3459 else
3460 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003461 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003462 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003463 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003464 Section,
3465 0,
3466 true);
3467 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003468}
3469
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003470
3471// struct objc_module {
3472// unsigned long version;
3473// unsigned long size;
3474// const char *name;
3475// Symtab symtab;
3476// };
3477
3478// FIXME: Get from somewhere
3479static const int ModuleVersion = 7;
3480
3481void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003482 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003483
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003484 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003485 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3486 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003487 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003488 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003489 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003490 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003491 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003492 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003493 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003494}
3495
3496llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003497 unsigned NumClasses = DefinedClasses.size();
3498 unsigned NumCategories = DefinedCategories.size();
3499
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003500 // Return null if no symbols were defined.
3501 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003502 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003503
3504 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003505 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003506 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003507 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3508 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003509
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003510 // The runtime expects exactly the list of defined classes followed
3511 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003512 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003513 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003514 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003515 ObjCTypes.Int8PtrTy);
3516 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003517 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003518 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003519 ObjCTypes.Int8PtrTy);
3520
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003521 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003522 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003523 NumClasses + NumCategories),
3524 Symbols);
3525
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003526 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003527
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003528 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003529 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3530 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003531 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003532 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003533}
3534
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003535llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003536 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003537 LazySymbols.insert(ID->getIdentifier());
3538
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003539 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003540
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003541 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003542 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003543 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003544 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003545 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003546 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3547 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003548 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003549 }
3550
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003551 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003552}
3553
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003554llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3555 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003556 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003557
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003558 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003559 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003560 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003561 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003562 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003563 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3564 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003565 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003566 }
3567
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003568 if (lvalue)
3569 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003570 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003571}
3572
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003573llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003574 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003575
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003576 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003577 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003578 llvm::ConstantArray::get(VMContext,
3579 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003580 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003581 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003582
Owen Andersona1cf15f2009-07-14 23:10:40 +00003583 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003584}
3585
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003586llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3587 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3588 I = MethodDefinitions.find(MD);
3589 if (I != MethodDefinitions.end())
3590 return I->second;
3591
3592 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3593 // MD isn't emitted yet because it comes from PCH.
3594 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3595 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3596 return MethodDefinitions[MD];
3597 }
3598
3599 return NULL;
3600}
3601
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003602/// GetIvarLayoutName - Returns a unique constant for the given
3603/// ivar layout bitmap.
3604llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003605 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003606 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003607}
3608
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003609void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003610 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003611 bool ForStrongLayout,
3612 bool &HasUnion) {
3613 const RecordDecl *RD = RT->getDecl();
3614 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003615 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003616 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003617 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003618 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003619
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003620 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3621 ForStrongLayout, HasUnion);
3622}
3623
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003624void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003625 const llvm::StructLayout *Layout,
3626 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003627 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003628 unsigned int BytePos, bool ForStrongLayout,
3629 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003630 bool IsUnion = (RD && RD->isUnion());
3631 uint64_t MaxUnionIvarSize = 0;
3632 uint64_t MaxSkippedUnionIvarSize = 0;
3633 FieldDecl *MaxField = 0;
3634 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003635 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003636 uint64_t MaxFieldOffset = 0;
3637 uint64_t MaxSkippedFieldOffset = 0;
3638 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003639
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003640 if (RecFields.empty())
3641 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003642 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3643 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3644
Chris Lattnerf1690852009-03-31 08:48:01 +00003645 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003646 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003647 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003648 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003649 // Note that 'i' here is actually the field index inside RD of Field,
3650 // although this dependency is hidden.
3651 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3652 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003653 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003654 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003655
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003656 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003657 if (!Field->getIdentifier() || Field->isBitField()) {
3658 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003659 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003660 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003661 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003662
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003663 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003664 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003665 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003666 if (FQT->isUnionType())
3667 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003668
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003669 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003670 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003671 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003672 continue;
3673 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674
Chris Lattnerf1690852009-03-31 08:48:01 +00003675 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003677 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003678 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003679 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003680 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003681 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3682 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003683 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003684 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003685 FQT = CArray->getElementType();
3686 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687
3688 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003689 "layout for array of unions not supported");
3690 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003691 int OldIndex = IvarsInfo.size() - 1;
3692 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003693
Ted Kremenek6217b802009-07-29 21:53:49 +00003694 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003695 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003696 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003697
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003698 // Replicate layout information for each array element. Note that
3699 // one element is already done.
3700 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003701 for (int FirstIndex = IvarsInfo.size() - 1,
3702 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003703 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003704 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3705 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3706 IvarsInfo[i].ivar_size));
3707 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3708 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3709 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003710 }
3711 continue;
3712 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003713 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003714 // At this point, we are done with Record/Union and array there of.
3715 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003716 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003717
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003718 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003719 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3720 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003721 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003722 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003723 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003724 MaxUnionIvarSize = UnionIvarSize;
3725 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003726 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003727 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003728 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003729 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003730 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003731 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003732 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003733 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3734 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003735 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003736 // FIXME: Why the asymmetry? We divide by word size in bits on other
3737 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003738 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003739 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003740 MaxSkippedUnionIvarSize = UnionIvarSize;
3741 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003742 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003743 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003744 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003745 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003746 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003747 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003748 }
3749 }
3750 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003751
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003752 if (LastFieldBitfield) {
3753 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003754 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3755 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003756 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003757 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003758 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003759 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3760 + ((BitFieldSize % ByteSizeInBits) != 0);
3761 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003762 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003763
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003764 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003765 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003766 MaxUnionIvarSize));
3767 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003768 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003769 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003770}
3771
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003772/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3773/// the computations and returning the layout bitmap (for ivar or blocks) in
3774/// the given argument BitMap string container. Routine reads
3775/// two containers, IvarsInfo and SkipIvars which are assumed to be
3776/// filled already by the caller.
3777llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003778 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003779 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003780
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003781 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003782 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003783 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003784 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003785 if (IvarsInfo[0].ivar_bytepos == 0) {
3786 WordsToSkip = 0;
3787 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003788 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3790 WordsToScan = IvarsInfo[0].ivar_size;
3791 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003792 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003793 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003794 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003795 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003796 // consecutive 'scanned' object pointers.
3797 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003798 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003799 // Skip over 'gc'able object pointer which lay over each other.
3800 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3801 continue;
3802 // Must skip over 1 or more words. We save current skip/scan values
3803 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003804 SKIP_SCAN SkScan;
3805 SkScan.skip = WordsToSkip;
3806 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003807 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003808
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003809 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003810 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3811 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003812 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003813 WordsToSkip = 0;
3814 WordsToScan = IvarsInfo[i].ivar_size;
3815 }
3816 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003817 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003818 SKIP_SCAN SkScan;
3819 SkScan.skip = WordsToSkip;
3820 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003821 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003822 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003823
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003824 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003825 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003826 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003827 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003828 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003829 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003830 IvarsInfo[LastIndex].ivar_bytepos +
3831 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003832 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003833 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003834 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003835 SKIP_SCAN SkScan;
3836 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3837 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003838 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003839 }
3840 }
3841 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3842 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003843 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003844 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003845 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3846 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3847 // 0xM0 followed by 0x0N detected.
3848 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3849 for (int j = i+1; j < SkipScan; j++)
3850 SkipScanIvars[j] = SkipScanIvars[j+1];
3851 --SkipScan;
3852 }
3853 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003854
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003855 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003856 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003857 unsigned char byte;
3858 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3859 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3860 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3861 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003862
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003863 // first skip big.
3864 for (unsigned int ix = 0; ix < skip_big; ix++)
3865 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003866
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003867 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003868 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003869 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003870 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003871 byte |= 0xf;
3872 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003873 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003874 byte |= scan_small;
3875 scan_small = 0;
3876 }
3877 BitMap += byte;
3878 }
3879 // next scan big
3880 for (unsigned int ix = 0; ix < scan_big; ix++)
3881 BitMap += (unsigned char)(0x0f);
3882 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003883 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003884 byte = scan_small;
3885 BitMap += byte;
3886 }
3887 }
3888 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003889 unsigned char zero = 0;
3890 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003891
3892 llvm::GlobalVariable * Entry =
3893 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3894 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3895 "__TEXT,__cstring,cstring_literals",
3896 1, true);
3897 return getConstantGEP(VMContext, Entry, 0, 0);
3898}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003899
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003900/// BuildIvarLayout - Builds ivar layout bitmap for the class
3901/// implementation for the __strong or __weak case.
3902/// The layout map displays which words in ivar list must be skipped
3903/// and which must be scanned by GC (see below). String is built of bytes.
3904/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3905/// of words to skip and right nibble is count of words to scan. So, each
3906/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3907/// represented by a 0x00 byte which also ends the string.
3908/// 1. when ForStrongLayout is true, following ivars are scanned:
3909/// - id, Class
3910/// - object *
3911/// - __strong anything
3912///
3913/// 2. When ForStrongLayout is false, following ivars are scanned:
3914/// - __weak anything
3915///
3916llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3917 const ObjCImplementationDecl *OMD,
3918 bool ForStrongLayout) {
3919 bool hasUnion = false;
3920
3921 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3922 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3923 return llvm::Constant::getNullValue(PtrTy);
3924
3925 llvm::SmallVector<FieldDecl*, 32> RecFields;
3926 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3927 CGM.getContext().CollectObjCIvars(OI, RecFields);
3928
3929 // Add this implementations synthesized ivars.
3930 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3931 CGM.getContext().CollectNonClassIvars(OI, Ivars);
3932 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3933 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3934
3935 if (RecFields.empty())
3936 return llvm::Constant::getNullValue(PtrTy);
3937
3938 SkipIvars.clear();
3939 IvarsInfo.clear();
3940
3941 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3942 if (IvarsInfo.empty())
3943 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003944 // Sort on byte position in case we encounterred a union nested in
3945 // the ivar list.
3946 if (hasUnion && !IvarsInfo.empty())
3947 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3948 if (hasUnion && !SkipIvars.empty())
3949 std::sort(SkipIvars.begin(), SkipIvars.end());
3950
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003951 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003952 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003953
3954 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003956 ForStrongLayout ? "strong" : "weak",
3957 OMD->getClassInterface()->getNameAsCString());
3958 const unsigned char *s = (unsigned char*)BitMap.c_str();
3959 for (unsigned i = 0; i < BitMap.size(); i++)
3960 if (!(s[i] & 0xf0))
3961 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3962 else
3963 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3964 printf("\n");
3965 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003966 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003967}
3968
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003969llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003970 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3971
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003972 // FIXME: Avoid std::string copying.
3973 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003974 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003975 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003976 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003977 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003978
Owen Andersona1cf15f2009-07-14 23:10:40 +00003979 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003980}
3981
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003982// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003983llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003984 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3985}
3986
3987// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003988llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003989 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3990}
3991
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003992llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003993 std::string TypeStr;
3994 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3995
3996 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003997
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003998 if (!Entry)
3999 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004000 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004001 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004002 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004003
Owen Andersona1cf15f2009-07-14 23:10:40 +00004004 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004005}
4006
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004007llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004008 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00004009 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4010 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00004011
4012 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4013
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004014 if (!Entry)
4015 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004016 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004017 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004018 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004019
Owen Andersona1cf15f2009-07-14 23:10:40 +00004020 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004021}
4022
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004023// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004024llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004025 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004026
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004027 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004028 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004029 llvm::ConstantArray::get(VMContext,
4030 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004031 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004032 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004033
Owen Andersona1cf15f2009-07-14 23:10:40 +00004034 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004035}
4036
4037// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004038// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004039llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004040CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4041 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004042 std::string TypeStr;
4043 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004044 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4045}
4046
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004047void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004048 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004049 llvm::SmallVectorImpl<char> &Name) {
4050 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004051 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004052 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4053 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004054 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004055 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004056 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004057 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004058}
4059
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004060void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004061 EmitModuleInfo();
4062
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004063 // Emit the dummy bodies for any protocols which were referenced but
4064 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004065 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004066 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4067 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004068 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004069
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004070 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004071 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004072 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004073 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004074 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004075 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004076 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004077 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004078 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004079 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004080 }
4081
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004082 // Add assembler directives to add lazy undefined symbol references
4083 // for classes which are referenced but not defined. This is
4084 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004085 //
4086 // FIXME: It would be nice if we had an LLVM construct for this.
4087 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4088 llvm::SmallString<256> Asm;
4089 Asm += CGM.getModule().getModuleInlineAsm();
4090 if (!Asm.empty() && Asm.back() != '\n')
4091 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004092
Daniel Dunbar33063492009-09-07 00:20:42 +00004093 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004094 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4095 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004096 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4097 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004098 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004099 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004100 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004101 }
4102
4103 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4104 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4105 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4106 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004107
Daniel Dunbar33063492009-09-07 00:20:42 +00004108 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004109 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004110}
4111
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004112CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004113 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004114 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004115 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004116 ObjCABI = 2;
4117}
4118
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004119/* *** */
4120
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004121ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004122 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004123 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4124 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004125
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004126 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004127 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004128 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004129 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004130 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004132 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004133 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004134 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004135
Mike Stumpf5408fe2009-05-16 07:57:57 +00004136 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4137 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004138 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004139 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004140
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004141 // I'm not sure I like this. The implicit coordination is a bit
4142 // gross. We should solve this in a reasonable fashion because this
4143 // is a pretty common task (match some runtime data structure with
4144 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004145
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004146 // FIXME: This is leaked.
4147 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004148
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004149 // struct _objc_super {
4150 // id self;
4151 // Class cls;
4152 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004153 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004154 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004155 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004156 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004157 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004158 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004159 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004160 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004161 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004162
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004163 SuperCTy = Ctx.getTagDeclType(RD);
4164 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004165
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004166 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4168
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004169 // struct _prop_t {
4170 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004172 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004173 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004175 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004177 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004178 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004179 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004180 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004181 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004182 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004183 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004184 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004185 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004186 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004187 PropertyListTy);
4188 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004189 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004190
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004191 // struct _objc_method {
4192 // SEL _cmd;
4193 // char *method_type;
4194 // char *_imp;
4195 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004196 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004197 Int8PtrTy,
4198 Int8PtrTy,
4199 NULL);
4200 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004202 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004203 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004204 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004205 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004206}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004207
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004208ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004209 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004210 // struct _objc_method_description {
4211 // SEL name;
4212 // char *types;
4213 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004215 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004216 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004217 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004218 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004219 MethodDescriptionTy);
4220
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004221 // struct _objc_method_description_list {
4222 // int count;
4223 // struct _objc_method_description[1];
4224 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004226 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004227 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004228 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004231
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004232 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004233 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004234 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004235
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004236 // Protocol description structures
4237
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004238 // struct _objc_protocol_extension {
4239 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4240 // struct _objc_method_description_list *optional_instance_methods;
4241 // struct _objc_method_description_list *optional_class_methods;
4242 // struct _objc_property_list *instance_properties;
4243 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004244 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004245 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004246 MethodDescriptionListPtrTy,
4247 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004248 PropertyListPtrTy,
4249 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004250 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004251 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004252
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004253 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004254 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004255
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004256 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004257
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004258 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4259 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004260
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004261 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004262 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004263 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004264 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004265 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004266 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004267 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4268
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004269 // struct _objc_protocol {
4270 // struct _objc_protocol_extension *isa;
4271 // char *protocol_name;
4272 // struct _objc_protocol **_objc_protocol_list;
4273 // struct _objc_method_description_list *instance_methods;
4274 // struct _objc_method_description_list *class_methods;
4275 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004276 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004277 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004278 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004279 MethodDescriptionListPtrTy,
4280 MethodDescriptionListPtrTy,
4281 NULL);
4282 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4283
4284 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004285 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004286 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004287 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004288 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004289
4290 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004291 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004292 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004293
4294 // Class description structures
4295
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004296 // struct _objc_ivar {
4297 // char *ivar_name;
4298 // char *ivar_type;
4299 // int ivar_offset;
4300 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004301 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004302 Int8PtrTy,
4303 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004304 NULL);
4305 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4306
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004307 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004308 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004309 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004310 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004311
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004312 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004313 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004314 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004315 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004317 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004318 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004319 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004320 Int8PtrTy,
4321 PropertyListPtrTy,
4322 NULL);
4323 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004324 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004325
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004326 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004327
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004328 // struct _objc_class {
4329 // Class isa;
4330 // Class super_class;
4331 // char *name;
4332 // long version;
4333 // long info;
4334 // long instance_size;
4335 // struct _objc_ivar_list *ivars;
4336 // struct _objc_method_list *methods;
4337 // struct _objc_cache *cache;
4338 // struct _objc_protocol_list *protocols;
4339 // char *ivar_layout;
4340 // struct _objc_class_ext *ext;
4341 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004342 T = llvm::StructType::get(VMContext,
4343 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004344 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004345 Int8PtrTy,
4346 LongTy,
4347 LongTy,
4348 LongTy,
4349 IvarListPtrTy,
4350 MethodListPtrTy,
4351 CachePtrTy,
4352 ProtocolListPtrTy,
4353 Int8PtrTy,
4354 ClassExtensionPtrTy,
4355 NULL);
4356 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004358 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4359 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004360 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004361
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004362 // struct _objc_category {
4363 // char *category_name;
4364 // char *class_name;
4365 // struct _objc_method_list *instance_method;
4366 // struct _objc_method_list *class_method;
4367 // uint32_t size; // sizeof(struct _objc_category)
4368 // struct _objc_property_list *instance_properties;// category's @property
4369 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004370 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004371 Int8PtrTy,
4372 MethodListPtrTy,
4373 MethodListPtrTy,
4374 ProtocolListPtrTy,
4375 IntTy,
4376 PropertyListPtrTy,
4377 NULL);
4378 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4379
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004380 // Global metadata structures
4381
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004382 // struct _objc_symtab {
4383 // long sel_ref_cnt;
4384 // SEL *refs;
4385 // short cls_def_cnt;
4386 // short cat_def_cnt;
4387 // char *defs[cls_def_cnt + cat_def_cnt];
4388 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004389 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004390 SelectorPtrTy,
4391 ShortTy,
4392 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004393 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004394 NULL);
4395 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004396 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004397
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004398 // struct _objc_module {
4399 // long version;
4400 // long size; // sizeof(struct _objc_module)
4401 // char *name;
4402 // struct _objc_symtab* symtab;
4403 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004404 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004405 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004406 LongTy,
4407 Int8PtrTy,
4408 SymtabPtrTy,
4409 NULL);
4410 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004411
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Mike Stumpf5408fe2009-05-16 07:57:57 +00004413 // FIXME: This is the size of the setjmp buffer and should be target
4414 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004415 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004416
Anders Carlsson124526b2008-09-09 10:10:21 +00004417 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004418 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004419 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004420
4421 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004422 llvm::StructType::get(VMContext,
4423 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4424 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004425 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004426 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004427 ExceptionDataTy);
4428
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004429}
4430
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004431ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004432 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004433 // struct _method_list_t {
4434 // uint32_t entsize; // sizeof(struct _objc_method)
4435 // uint32_t method_count;
4436 // struct _objc_method method_list[method_count];
4437 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004438 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004439 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004440 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004441 NULL);
4442 CGM.getModule().addTypeName("struct.__method_list_t",
4443 MethodListnfABITy);
4444 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004445 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004446
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004447 // struct _protocol_t {
4448 // id isa; // NULL
4449 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004450 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004451 // const struct method_list_t * const instance_methods;
4452 // const struct method_list_t * const class_methods;
4453 // const struct method_list_t *optionalInstanceMethods;
4454 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004455 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004456 // const uint32_t size; // sizeof(struct _protocol_t)
4457 // const uint32_t flags; // = 0
4458 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004459
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004460 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004461 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004462
Owen Anderson47a434f2009-08-05 23:18:46 +00004463 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004464 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004465 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004466 ProtocolListTyHolder),
4467 MethodListnfABIPtrTy,
4468 MethodListnfABIPtrTy,
4469 MethodListnfABIPtrTy,
4470 MethodListnfABIPtrTy,
4471 PropertyListPtrTy,
4472 IntTy,
4473 IntTy,
4474 NULL);
4475 CGM.getModule().addTypeName("struct._protocol_t",
4476 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004477
4478 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004479 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004480
Fariborz Jahanianda320092009-01-29 19:24:30 +00004481 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004482 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004483 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004484 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004485 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004486 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004487 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004488 NULL);
4489 CGM.getModule().addTypeName("struct._objc_protocol_list",
4490 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004491 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492 ProtocolListnfABITy);
4493
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004494 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004495 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004497 // struct _ivar_t {
4498 // unsigned long int *offset; // pointer to ivar offset location
4499 // char *name;
4500 // char *type;
4501 // uint32_t alignment;
4502 // uint32_t size;
4503 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004504 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004505 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004506 Int8PtrTy,
4507 Int8PtrTy,
4508 IntTy,
4509 IntTy,
4510 NULL);
4511 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004512
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004513 // struct _ivar_list_t {
4514 // uint32 entsize; // sizeof(struct _ivar_t)
4515 // uint32 count;
4516 // struct _iver_t list[count];
4517 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004518 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004519 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004520 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004521 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004522 NULL);
4523 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524
Owen Anderson96e0fc72009-07-29 22:16:19 +00004525 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004526
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004527 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004528 // uint32_t const flags;
4529 // uint32_t const instanceStart;
4530 // uint32_t const instanceSize;
4531 // uint32_t const reserved; // only when building for 64bit targets
4532 // const uint8_t * const ivarLayout;
4533 // const char *const name;
4534 // const struct _method_list_t * const baseMethods;
4535 // const struct _objc_protocol_list *const baseProtocols;
4536 // const struct _ivar_list_t *const ivars;
4537 // const uint8_t * const weakIvarLayout;
4538 // const struct _prop_list_t * const properties;
4539 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004541 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004542 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004543 IntTy,
4544 IntTy,
4545 Int8PtrTy,
4546 Int8PtrTy,
4547 MethodListnfABIPtrTy,
4548 ProtocolListnfABIPtrTy,
4549 IvarListnfABIPtrTy,
4550 Int8PtrTy,
4551 PropertyListPtrTy,
4552 NULL);
4553 CGM.getModule().addTypeName("struct._class_ro_t",
4554 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004555
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004556 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4557 std::vector<const llvm::Type*> Params;
4558 Params.push_back(ObjectPtrTy);
4559 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004560 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004561 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4562
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004563 // struct _class_t {
4564 // struct _class_t *isa;
4565 // struct _class_t * const superclass;
4566 // void *cache;
4567 // IMP *vtable;
4568 // struct class_ro_t *ro;
4569 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004570
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004571 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004572 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004573 llvm::StructType::get(VMContext,
4574 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575 llvm::PointerType::getUnqual(ClassTyHolder),
4576 CachePtrTy,
4577 llvm::PointerType::getUnqual(ImpnfABITy),
4578 llvm::PointerType::getUnqual(ClassRonfABITy),
4579 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004580 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4581
4582 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004583 ClassnfABITy);
4584
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004585 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004586 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004588 // struct _category_t {
4589 // const char * const name;
4590 // struct _class_t *const cls;
4591 // const struct _method_list_t * const instance_methods;
4592 // const struct _method_list_t * const class_methods;
4593 // const struct _protocol_list_t * const protocols;
4594 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004595 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004596 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004597 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004598 MethodListnfABIPtrTy,
4599 MethodListnfABIPtrTy,
4600 ProtocolListnfABIPtrTy,
4601 PropertyListPtrTy,
4602 NULL);
4603 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004605 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004606 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4607 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004609 // MessageRefTy - LLVM for:
4610 // struct _message_ref_t {
4611 // IMP messenger;
4612 // SEL name;
4613 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004614
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004615 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004616 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004617 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004618 SourceLocation(),
4619 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004620 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004621 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004622 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004623 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004624 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004626 MessageRefCTy = Ctx.getTagDeclType(RD);
4627 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4628 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004630 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004631 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004632
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004633 // SuperMessageRefTy - LLVM for:
4634 // struct _super_message_ref_t {
4635 // SUPER_IMP messenger;
4636 // SEL name;
4637 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004638 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004639 SelectorPtrTy,
4640 NULL);
4641 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004643 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4645
Daniel Dunbare588b992009-03-01 04:46:24 +00004646
4647 // struct objc_typeinfo {
4648 // const void** vtable; // objc_ehtype_vtable + 2
4649 // const char* name; // c++ typeinfo string
4650 // Class cls;
4651 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004652 EHTypeTy = llvm::StructType::get(VMContext,
4653 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004654 Int8PtrTy,
4655 ClassnfABIPtrTy,
4656 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004657 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004658 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004659}
4660
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004661llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004662 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004664 return NULL;
4665}
4666
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004667void CGObjCNonFragileABIMac::AddModuleClassList(const
4668 std::vector<llvm::GlobalValue*>
4669 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004670 const char *SymbolName,
4671 const char *SectionName) {
4672 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673
Daniel Dunbar463b8762009-05-15 21:48:48 +00004674 if (!NumClasses)
4675 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004676
Daniel Dunbar463b8762009-05-15 21:48:48 +00004677 std::vector<llvm::Constant*> Symbols(NumClasses);
4678 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004679 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004680 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004681 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004682 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004683 NumClasses),
4684 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004685
Daniel Dunbar463b8762009-05-15 21:48:48 +00004686 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004687 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004688 llvm::GlobalValue::InternalLinkage,
4689 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004690 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004691 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004692 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004693 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004694}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004695
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004696void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4697 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004698
Daniel Dunbar463b8762009-05-15 21:48:48 +00004699 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004700 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004702 "\01L_OBJC_LABEL_CLASS_$",
4703 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004704
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004705 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4706 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4707 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4708 continue;
4709 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004710 }
4711
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004712 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4713 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4714 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4715 continue;
4716 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4717 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004718
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004719 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004720 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4721 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004722
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004723 // Build list of all implemented category addresses in array
4724 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004725 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004726 "\01L_OBJC_LABEL_CATEGORY_$",
4727 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004728 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004729 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4730 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004731
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004732 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004733}
4734
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004735/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004736/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004738/// message dispatch call for all the rest.
4739///
4740bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004741 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4742 default:
4743 assert(0 && "Invalid dispatch method!");
4744 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004745 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004746 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004747 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004748 case CodeGenOptions::Mixed:
4749 break;
4750 }
4751
4752 // If so, see whether this selector is in the white-list of things which must
4753 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004754 if (NonLegacyDispatchMethods.empty()) {
4755 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4756 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4757 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4758 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4759 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4760 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4761 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4762 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4763 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4764 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004765
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004766 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4767 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4768 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4769 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4770 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4771 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4772 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4773 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004774 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004775 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4777 &CGM.getContext().Idents.get("objects"),
4778 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004779 };
4780 NonLegacyDispatchMethods.insert(
4781 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004782 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004783
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004784 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004785}
4786
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004787// Metadata flags
4788enum MetaDataDlags {
4789 CLS = 0x0,
4790 CLS_META = 0x1,
4791 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004792 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004793 CLS_EXCEPTION = 0x20
4794};
4795/// BuildClassRoTInitializer - generate meta-data for:
4796/// struct _class_ro_t {
4797/// uint32_t const flags;
4798/// uint32_t const instanceStart;
4799/// uint32_t const instanceSize;
4800/// uint32_t const reserved; // only when building for 64bit targets
4801/// const uint8_t * const ivarLayout;
4802/// const char *const name;
4803/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004804/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004805/// const struct _ivar_list_t *const ivars;
4806/// const uint8_t * const weakIvarLayout;
4807/// const struct _prop_list_t * const properties;
4808/// }
4809///
4810llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004811 unsigned flags,
4812 unsigned InstanceStart,
4813 unsigned InstanceSize,
4814 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004815 std::string ClassName = ID->getNameAsString();
4816 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004817 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4818 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4819 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004820 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004821 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4822 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004823 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004824 // const struct _method_list_t * const baseMethods;
4825 std::vector<llvm::Constant*> Methods;
4826 std::string MethodListName("\01l_OBJC_$_");
4827 if (flags & CLS_META) {
4828 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004829 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004830 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004831 // Class methods should always be defined.
4832 Methods.push_back(GetMethodConstant(*i));
4833 }
4834 } else {
4835 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004836 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004837 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004838 // Instance methods should always be defined.
4839 Methods.push_back(GetMethodConstant(*i));
4840 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004841 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004842 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004843 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004845 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4846 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004847
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004848 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4849 if (llvm::Constant *C = GetMethodConstant(MD))
4850 Methods.push_back(C);
4851 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4852 if (llvm::Constant *C = GetMethodConstant(MD))
4853 Methods.push_back(C);
4854 }
4855 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004856 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004857 Values[ 5] = EmitMethodList(MethodListName,
4858 "__DATA, __objc_const", Methods);
4859
Fariborz Jahanianda320092009-01-29 19:24:30 +00004860 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4861 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004863 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004864 OID->protocol_begin(),
4865 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004866
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004867 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004868 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004869 else
4870 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004871 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4872 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004873 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004874 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004875 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004876 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4877 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004878 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004879 Values);
4880 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004881 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4882 llvm::GlobalValue::InternalLinkage,
4883 Init,
4884 (flags & CLS_META) ?
4885 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4886 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004887 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004888 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004889 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004890 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004891
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004892}
4893
4894/// BuildClassMetaData - This routine defines that to-level meta-data
4895/// for the given ClassName for:
4896/// struct _class_t {
4897/// struct _class_t *isa;
4898/// struct _class_t * const superclass;
4899/// void *cache;
4900/// IMP *vtable;
4901/// struct class_ro_t *ro;
4902/// }
4903///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004904llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004905 std::string &ClassName,
4906 llvm::Constant *IsAGV,
4907 llvm::Constant *SuperClassGV,
4908 llvm::Constant *ClassRoGV,
4909 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004910 std::vector<llvm::Constant*> Values(5);
4911 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004912 Values[1] = SuperClassGV;
4913 if (!Values[1])
4914 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004915 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4916 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4917 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004918 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004919 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004920 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4921 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004922 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004923 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004924 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004925 if (HiddenVisibility)
4926 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004927 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004928}
4929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004930bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004931CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004932 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004933}
4934
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004935void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004936 uint32_t &InstanceStart,
4937 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004938 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004939 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004940
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004941 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004942 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004943
4944 // If there are no fields, the start is the same as the end.
4945 if (!RL.getFieldCount())
4946 InstanceStart = InstanceSize;
4947 else
4948 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004949}
4950
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004951void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4952 std::string ClassName = ID->getNameAsString();
4953 if (!ObjCEmptyCacheVar) {
4954 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004955 CGM.getModule(),
4956 ObjCTypes.CacheTy,
4957 false,
4958 llvm::GlobalValue::ExternalLinkage,
4959 0,
4960 "_objc_empty_cache");
4961
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004962 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004963 CGM.getModule(),
4964 ObjCTypes.ImpnfABITy,
4965 false,
4966 llvm::GlobalValue::ExternalLinkage,
4967 0,
4968 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004969 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004970 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004971 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004972 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004973 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004974 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004975 uint32_t InstanceSize = InstanceStart;
4976 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004977 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4978 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004979
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004980 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004981
4982 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004983 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004984 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004985 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004986 if (ID->getNumIvarInitializers())
4987 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004988 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004989 // class is root
4990 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004991 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004992 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004993 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004994 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004995 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4996 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4997 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004998 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004999 if (Root->hasAttr<WeakImportAttr>())
5000 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005001 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005002 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005003 ObjCMetaClassName +
5004 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005005 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005006 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5007 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005008 }
5009 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5010 InstanceStart,
5011 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005012 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005014 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5015 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005016 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005017
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005018 // Metadata for the class
5019 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005020 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005021 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005022 if (ID->getNumIvarInitializers())
5023 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005024
Douglas Gregor68584ed2009-06-18 16:11:24 +00005025 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005026 flags |= CLS_EXCEPTION;
5027
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005028 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005029 flags |= CLS_ROOT;
5030 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005031 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005032 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005033 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005034 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005035 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005036 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5037 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005038 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005039 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005040 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005041 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005043 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005044
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005045 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005046 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005047 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5048 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005049 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005050
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005051 // Determine if this class is also "non-lazy".
5052 if (ImplementationIsNonLazy(ID))
5053 DefinedNonLazyClasses.push_back(ClassMD);
5054
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005055 // Force the definition of the EHType if necessary.
5056 if (flags & CLS_EXCEPTION)
5057 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005058}
5059
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005060/// GenerateProtocolRef - This routine is called to generate code for
5061/// a protocol reference expression; as in:
5062/// @code
5063/// @protocol(Proto1);
5064/// @endcode
5065/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5066/// which will hold address of the protocol meta-data.
5067///
5068llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005069 const ObjCProtocolDecl *PD) {
5070
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005071 // This routine is called for @protocol only. So, we must build definition
5072 // of protocol's meta-data (not a reference to it!)
5073 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005074 llvm::Constant *Init =
5075 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5076 ObjCTypes.ExternalProtocolPtrTy);
5077
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005078 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
5079 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005080
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005081 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5082 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005083 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005084 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085 CGM.getModule(),
5086 Init->getType(), false,
5087 llvm::GlobalValue::WeakAnyLinkage,
5088 Init,
5089 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005090 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5091 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005092 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005093 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005094}
5095
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005096/// GenerateCategory - Build metadata for a category implementation.
5097/// struct _category_t {
5098/// const char * const name;
5099/// struct _class_t *const cls;
5100/// const struct _method_list_t * const instance_methods;
5101/// const struct _method_list_t * const class_methods;
5102/// const struct _protocol_list_t * const protocols;
5103/// const struct _prop_list_t * const properties;
5104/// }
5105///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005106void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005107 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005108 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5110 "_$_" + OCD->getNameAsString());
5111 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005112 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005113
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005114 std::vector<llvm::Constant*> Values(6);
5115 Values[0] = GetClassName(OCD->getIdentifier());
5116 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005117 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005118 if (Interface->hasAttr<WeakImportAttr>())
5119 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5120
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005121 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005122 std::vector<llvm::Constant*> Methods;
5123 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005124 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005125 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005126
5127 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005128 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005129 // Instance methods should always be defined.
5130 Methods.push_back(GetMethodConstant(*i));
5131 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005132
5133 Values[2] = EmitMethodList(MethodListName,
5134 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005135 Methods);
5136
5137 MethodListName = Prefix;
5138 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5139 OCD->getNameAsString();
5140 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005141 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005142 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005143 // Class methods should always be defined.
5144 Methods.push_back(GetMethodConstant(*i));
5145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146
5147 Values[3] = EmitMethodList(MethodListName,
5148 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005149 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005151 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005152 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005153 llvm::SmallString<256> ExtName;
5154 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5155 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005156 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005157 + Interface->getName() + "_$_"
5158 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005159 Category->protocol_begin(),
5160 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005161 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5162 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005163 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005164 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5165 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005166 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005167
5168 llvm::Constant *Init =
5169 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005170 Values);
5171 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005172 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005173 false,
5174 llvm::GlobalValue::InternalLinkage,
5175 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005176 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005177 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005178 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005179 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005180 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005181 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005182
5183 // Determine if this category is also "non-lazy".
5184 if (ImplementationIsNonLazy(OCD))
5185 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005186}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005187
5188/// GetMethodConstant - Return a struct objc_method constant for the
5189/// given method if it has been defined. The result is null if the
5190/// method has not been defined. The return value has type MethodPtrTy.
5191llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005193 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005194 if (!Fn)
5195 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005196
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005197 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005198 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005199 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005200 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005201 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005202 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005203 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005204}
5205
5206/// EmitMethodList - Build meta-data for method declarations
5207/// struct _method_list_t {
5208/// uint32_t entsize; // sizeof(struct _objc_method)
5209/// uint32_t method_count;
5210/// struct _objc_method method_list[method_count];
5211/// }
5212///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005213llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5214 const char *Section,
5215 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005216 // Return null for empty list.
5217 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005218 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005219
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005220 std::vector<llvm::Constant*> Values(3);
5221 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005222 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005223 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005224 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005225 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005226 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005227 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005228 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005229 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005231 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005232 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005233 llvm::GlobalValue::InternalLinkage,
5234 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005235 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005236 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005237 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005238 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005239 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005240 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005241 ObjCTypes.MethodListnfABIPtrTy);
5242}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005243
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005244/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5245/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005246llvm::GlobalVariable *
5247CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5248 const ObjCIvarDecl *Ivar) {
5249 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005250 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005251 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005253 CGM.getModule().getGlobalVariable(Name);
5254 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005255 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005256 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005257 false,
5258 llvm::GlobalValue::ExternalLinkage,
5259 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005260 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005261 return IvarOffsetGV;
5262}
5263
Daniel Dunbare83be122010-04-02 21:14:02 +00005264llvm::Constant *
5265CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5266 const ObjCIvarDecl *Ivar,
5267 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005268 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005269 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005270 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005271 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005272 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005273
Mike Stumpf5408fe2009-05-16 07:57:57 +00005274 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5275 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005276 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5277 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5278 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005279 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005280 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005281 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005282 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005283 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005284}
5285
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005286/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005287/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005288/// IvarListnfABIPtrTy.
5289/// struct _ivar_t {
5290/// unsigned long int *offset; // pointer to ivar offset location
5291/// char *name;
5292/// char *type;
5293/// uint32_t alignment;
5294/// uint32_t size;
5295/// }
5296/// struct _ivar_list_t {
5297/// uint32 entsize; // sizeof(struct _ivar_t)
5298/// uint32 count;
5299/// struct _iver_t list[count];
5300/// }
5301///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005302
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005303llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304 const ObjCImplementationDecl *ID) {
5305
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005306 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005307
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005308 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5309 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005310
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005311 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Daniel Dunbar91636d62009-04-20 00:33:43 +00005313 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005314 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005315 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005317 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5318 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005319 // Ignore unnamed bit-fields.
5320 if (!IVD->getDeclName())
5321 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005323 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005324 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5325 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005326 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005327 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005328 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005329 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005330 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005331 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005332 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005333 // NOTE. Size of a bitfield does not match gcc's, because of the
5334 // way bitfields are treated special in each. But I am told that
5335 // 'size' for bitfield ivars is ignored by the runtime so it does
5336 // not matter. If it matters, there is enough info to get the
5337 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005338 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005339 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005340 }
5341 // Return null for empty list.
5342 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005343 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005344 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005345 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005346 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5347 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005348 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005349 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005350 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005351 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005352 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5353 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005354 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005355 llvm::GlobalValue::InternalLinkage,
5356 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005357 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005358 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005359 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005360 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005361
Chris Lattnerad64e022009-07-17 23:57:13 +00005362 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005363 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005364}
5365
5366llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005368 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369
Fariborz Jahanianda320092009-01-29 19:24:30 +00005370 if (!Entry) {
5371 // We use the initializer as a marker of whether this is a forward
5372 // reference or not. At module finalization we add the empty
5373 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374 Entry =
5375 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5376 llvm::GlobalValue::ExternalLinkage,
5377 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005378 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005381
Fariborz Jahanianda320092009-01-29 19:24:30 +00005382 return Entry;
5383}
5384
5385/// GetOrEmitProtocol - Generate the protocol meta-data:
5386/// @code
5387/// struct _protocol_t {
5388/// id isa; // NULL
5389/// const char * const protocol_name;
5390/// const struct _protocol_list_t * protocol_list; // super protocols
5391/// const struct method_list_t * const instance_methods;
5392/// const struct method_list_t * const class_methods;
5393/// const struct method_list_t *optionalInstanceMethods;
5394/// const struct method_list_t *optionalClassMethods;
5395/// const struct _prop_list_t * properties;
5396/// const uint32_t size; // sizeof(struct _protocol_t)
5397/// const uint32_t flags; // = 0
5398/// }
5399/// @endcode
5400///
5401
5402llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005405
Fariborz Jahanianda320092009-01-29 19:24:30 +00005406 // Early exit if a defining object has already been generated.
5407 if (Entry && Entry->hasInitializer())
5408 return Entry;
5409
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 // Construct method lists.
5411 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5412 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005413 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005414 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005416 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005417 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5418 OptInstanceMethods.push_back(C);
5419 } else {
5420 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005421 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005423
5424 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005425 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005426 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005427 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005428 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5429 OptClassMethods.push_back(C);
5430 } else {
5431 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005433 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005434
Fariborz Jahanianda320092009-01-29 19:24:30 +00005435 std::vector<llvm::Constant*> Values(10);
5436 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005437 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005439 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5440 PD->protocol_begin(),
5441 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005442
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005443 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005444 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005445 "__DATA, __objc_const",
5446 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005447 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005448 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005449 "__DATA, __objc_const",
5450 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005451 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005452 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005453 "__DATA, __objc_const",
5454 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005456 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 "__DATA, __objc_const",
5458 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005459 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005460 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005462 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005463 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005464 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005465 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005466 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467
Fariborz Jahanianda320092009-01-29 19:24:30 +00005468 if (Entry) {
5469 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005470 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005471 Entry->setInitializer(Init);
5472 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005474 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5475 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5476 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005477 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005478 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005479 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005480 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005481 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005482 CGM.AddUsedGlobal(Entry);
5483
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005484 // Use this protocol meta-data to build protocol list table in section
5485 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005486 llvm::GlobalVariable *PTGV =
5487 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5488 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5489 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005490 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005491 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005492 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005493 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005494 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005495 return Entry;
5496}
5497
5498/// EmitProtocolList - Generate protocol list meta-data:
5499/// @code
5500/// struct _protocol_list_t {
5501/// long protocol_count; // Note, this is 32/64 bit
5502/// struct _protocol_t[protocol_count];
5503/// }
5504/// @endcode
5505///
5506llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005507CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5508 ObjCProtocolDecl::protocol_iterator begin,
5509 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005510 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Fariborz Jahanianda320092009-01-29 19:24:30 +00005512 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005513 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005514 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005515
Daniel Dunbar948e2582009-02-15 07:36:20 +00005516 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005517 llvm::SmallString<256> TmpName;
5518 Name.toVector(TmpName);
5519 llvm::GlobalVariable *GV =
5520 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005521 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005522 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005523
Daniel Dunbar948e2582009-02-15 07:36:20 +00005524 for (; begin != end; ++begin)
5525 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5526
Fariborz Jahanianda320092009-01-29 19:24:30 +00005527 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005528 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005529 ObjCTypes.ProtocolnfABIPtrTy));
5530
Fariborz Jahanianda320092009-01-29 19:24:30 +00005531 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005532 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005533 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005534 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005535 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005536 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005537 ProtocolRefs.size()),
5538 ProtocolRefs);
5539
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005540 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005541 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005542 llvm::GlobalValue::InternalLinkage,
5543 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005544 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005545 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005546 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005547 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005548 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005549 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005550 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005551}
5552
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005553/// GetMethodDescriptionConstant - This routine build following meta-data:
5554/// struct _objc_method {
5555/// SEL _cmd;
5556/// char *method_type;
5557/// char *_imp;
5558/// }
5559
5560llvm::Constant *
5561CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5562 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005563 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005564 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5565 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005566 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005567 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005568 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005569 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005570}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005571
5572/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5573/// This code gen. amounts to generating code for:
5574/// @code
5575/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5576/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005577///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005578LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005579 CodeGen::CodeGenFunction &CGF,
5580 QualType ObjectTy,
5581 llvm::Value *BaseValue,
5582 const ObjCIvarDecl *Ivar,
5583 unsigned CVRQualifiers) {
5584 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005585 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5586 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005587}
5588
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005589llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005590 CodeGen::CodeGenFunction &CGF,
5591 const ObjCInterfaceDecl *Interface,
5592 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005593 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005594}
5595
Fariborz Jahanian46551122009-02-04 00:22:57 +00005596CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005597 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005598 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005599 QualType ResultType,
5600 Selector Sel,
5601 llvm::Value *Receiver,
5602 QualType Arg0Ty,
5603 bool IsSuper,
5604 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005605 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5606 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005607 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005608 llvm::Value *Arg0 = Receiver;
5609 if (!IsSuper)
5610 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005612 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005613 // FIXME. This is too much work to get the ABI-specific result type needed to
5614 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005615 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005616 = Types.getFunctionInfo(ResultType, CallArgList(),
5617 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005618 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005619 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005620 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005621#if 0
5622 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005623 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005624 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005625 // FIXME. Is there a better way of getting these names.
5626 // They are available in RuntimeFunctions vector pair.
5627 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005628 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005629#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005630 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005631 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005632 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005633 } else {
5634 Fn = ObjCTypes.getMessageSendStretFixupFn();
5635 Name += "objc_msgSend_stret_fixup";
5636 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005637 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5638 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5639 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005640 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005641#if 0
5642// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005643 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005644 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005645 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005646 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005647#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005649 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005650 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005651 } else {
5652 Fn = ObjCTypes.getMessageSendFixupFn();
5653 Name += "objc_msgSend_fixup";
5654 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005655 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005656 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005657 Name += '_';
5658 std::string SelName(Sel.getAsString());
5659 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005660 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005661 if (SelName[i] == ':')
5662 SelName[i] = '_';
5663 Name += SelName;
5664 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5665 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005666 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005667 std::vector<llvm::Constant*> Values(2);
5668 Values[0] = Fn;
5669 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005670 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005671 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005672 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005673 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005674 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005675 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005676 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005677 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005678 }
5679 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005681 CallArgList ActualArgs;
5682 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005683 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005684 ObjCTypes.MessageRefCPtrTy));
5685 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005686 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005687 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005688 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5689 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005690 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005691 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005692 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005693 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005694}
5695
5696/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005697CodeGen::RValue
5698CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005699 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005700 QualType ResultType,
5701 Selector Sel,
5702 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005703 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005704 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005705 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005706 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005707 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5708 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005710 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005711 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712 Receiver, CGF.getContext().getObjCIdType(),
5713 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005714}
5715
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005716llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005717CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005718 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5719
Daniel Dunbardfff2302009-03-02 05:18:14 +00005720 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005721 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005722 false, llvm::GlobalValue::ExternalLinkage,
5723 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005724 }
5725
5726 return GV;
5727}
5728
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005729llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5730 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005731 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005733 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005734 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005735 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005736 Entry =
5737 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005738 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005739 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005740 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005741 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005742 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005743 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005744 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005745 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005746 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005747
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005748 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005749}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005750
Daniel Dunbar11394522009-04-18 08:51:00 +00005751llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005753 const ObjCInterfaceDecl *ID) {
5754 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005755
Daniel Dunbar11394522009-04-18 08:51:00 +00005756 if (!Entry) {
5757 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5758 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759 Entry =
5760 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005761 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005762 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005763 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005764 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005765 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005766 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005767 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005768 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005769 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005770
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005771 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005772}
5773
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005774/// EmitMetaClassRef - Return a Value * of the address of _class_t
5775/// meta-data
5776///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005777llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5778 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005779 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5780 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005781 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005782
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005783 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005784 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005785 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005786 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005787 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005789 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005790 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005791 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005792 ObjCTypes.ClassnfABIPtrTy));
5793
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005794 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005795 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005796
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005797 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005798}
5799
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005800/// GetClass - Return a reference to the class for the given interface
5801/// decl.
5802llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5803 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005804 if (ID->hasAttr<WeakImportAttr>()) {
5805 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5806 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5807 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5808 }
5809
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005810 return EmitClassRef(Builder, ID);
5811}
5812
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005813/// Generates a message send where the super is the receiver. This is
5814/// a message send to self with special delivery semantics indicating
5815/// which class's method should be called.
5816CodeGen::RValue
5817CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005818 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005819 QualType ResultType,
5820 Selector Sel,
5821 const ObjCInterfaceDecl *Class,
5822 bool isCategoryImpl,
5823 llvm::Value *Receiver,
5824 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005825 const CodeGen::CallArgList &CallArgs,
5826 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005827 // ...
5828 // Create and init a super structure; this is a (receiver, class)
5829 // pair we will pass to objc_msgSendSuper.
5830 llvm::Value *ObjCSuper =
5831 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005833 llvm::Value *ReceiverAsObject =
5834 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5835 CGF.Builder.CreateStore(ReceiverAsObject,
5836 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005837
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005838 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005839 llvm::Value *Target;
5840 if (IsClassMessage) {
5841 if (isCategoryImpl) {
5842 // Message sent to "super' in a class method defined in
5843 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005844 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005845 Target = CGF.Builder.CreateStructGEP(Target, 0);
5846 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005847 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005848 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005849 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005850 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005851
Mike Stumpf5408fe2009-05-16 07:57:57 +00005852 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5853 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005854 const llvm::Type *ClassTy =
5855 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5856 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5857 CGF.Builder.CreateStore(Target,
5858 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005859
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005860 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005861 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5862 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005863 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005864 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005865 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005866 ObjCSuper, ObjCTypes.SuperPtrCTy,
5867 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005868}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005869
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005871 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005872 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005873
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005874 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005875 llvm::Constant *Casted =
5876 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5877 ObjCTypes.SelectorPtrTy);
5878 Entry =
5879 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5880 llvm::GlobalValue::InternalLinkage,
5881 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005882 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005883 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005884 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005885
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005886 if (lval)
5887 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005888 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005889}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005890/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005891/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005892///
5893void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005894 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005895 llvm::Value *dst,
5896 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005897 const llvm::Type * SrcTy = src->getType();
5898 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005899 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005900 assert(Size <= 8 && "does not support size > 8");
5901 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5902 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005903 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5904 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005905 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5906 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005907 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5908 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005909 return;
5910}
5911
5912/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5913/// objc_assign_strongCast (id src, id *dst)
5914///
5915void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005916 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005917 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005918 const llvm::Type * SrcTy = src->getType();
5919 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005920 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005921 assert(Size <= 8 && "does not support size > 8");
5922 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005923 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005924 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5925 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005926 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5927 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005928 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005929 src, dst, "weakassign");
5930 return;
5931}
5932
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005933void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005934 CodeGen::CodeGenFunction &CGF,
5935 llvm::Value *DestPtr,
5936 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005937 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005938 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5939 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005940 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005941 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005942 return;
5943}
5944
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005945/// EmitObjCWeakRead - Code gen for loading value of a __weak
5946/// object: objc_read_weak (id *src)
5947///
5948llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005950 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005951 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005952 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5953 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005954 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005955 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005956 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005957 return read_weak;
5958}
5959
5960/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5961/// objc_assign_weak (id src, id *dst)
5962///
5963void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005964 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005965 const llvm::Type * SrcTy = src->getType();
5966 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005967 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005968 assert(Size <= 8 && "does not support size > 8");
5969 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5970 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005971 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5972 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005973 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5974 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005975 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005976 src, dst, "weakassign");
5977 return;
5978}
5979
5980/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5981/// objc_assign_global (id src, id *dst)
5982///
5983void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005984 llvm::Value *src, llvm::Value *dst,
5985 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005986 const llvm::Type * SrcTy = src->getType();
5987 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005988 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005989 assert(Size <= 8 && "does not support size > 8");
5990 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5991 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005992 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5993 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005994 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5995 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005996 if (!threadlocal)
5997 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5998 src, dst, "globalassign");
5999 else
6000 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6001 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006002 return;
6003}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006004
John McCall740e8072010-07-21 00:41:47 +00006005namespace {
John McCall1f0fca52010-07-21 07:22:38 +00006006 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00006007 llvm::Value *SyncExitFn;
6008 llvm::Value *SyncArg;
6009 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
6010 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
6011
6012 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
6013 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
6014 }
6015 };
6016}
6017
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006018void
John McCallf1549f62010-07-06 01:34:17 +00006019CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6020 const ObjCAtSynchronizedStmt &S) {
6021 // Evaluate the lock operand. This should dominate the cleanup.
6022 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006023
John McCallf1549f62010-07-06 01:34:17 +00006024 // Acquire the lock.
6025 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6026 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6027 ->setDoesNotThrow();
6028
6029 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006030 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6031 ObjCTypes.getSyncExitFn(),
6032 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006033
John McCallf1549f62010-07-06 01:34:17 +00006034 // Emit the body of the statement.
6035 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006036
John McCallf1549f62010-07-06 01:34:17 +00006037 // Pop the lock-release cleanup.
6038 CGF.PopCleanupBlock();
6039}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006040
John McCallf1549f62010-07-06 01:34:17 +00006041namespace {
6042 struct CatchHandler {
6043 const VarDecl *Variable;
6044 const Stmt *Body;
6045 llvm::BasicBlock *Block;
6046 llvm::Value *TypeInfo;
6047 };
John McCall8e3f8612010-07-13 22:12:14 +00006048
John McCall1f0fca52010-07-21 07:22:38 +00006049 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006050 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6051 MightThrow(MightThrow), Fn(Fn) {}
6052 bool MightThrow;
6053 llvm::Value *Fn;
6054
6055 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6056 if (!MightThrow) {
6057 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6058 return;
6059 }
6060
6061 CGF.EmitCallOrInvoke(Fn, 0, 0);
6062 }
6063 };
John McCallf1549f62010-07-06 01:34:17 +00006064}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006065
John McCall5a180392010-07-24 00:37:23 +00006066llvm::Constant *
6067CGObjCNonFragileABIMac::GetEHType(QualType T) {
6068 // There's a particular fixed type info for 'id'.
6069 if (T->isObjCIdType() ||
6070 T->isObjCQualifiedIdType()) {
6071 llvm::Constant *IDEHType =
6072 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6073 if (!IDEHType)
6074 IDEHType =
6075 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6076 false,
6077 llvm::GlobalValue::ExternalLinkage,
6078 0, "OBJC_EHTYPE_id");
6079 return IDEHType;
6080 }
6081
6082 // All other types should be Objective-C interface pointer types.
6083 const ObjCObjectPointerType *PT =
6084 T->getAs<ObjCObjectPointerType>();
6085 assert(PT && "Invalid @catch type.");
6086 const ObjCInterfaceType *IT = PT->getInterfaceType();
6087 assert(IT && "Invalid @catch type.");
6088 return GetInterfaceEHType(IT->getDecl(), false);
6089}
6090
John McCallf1549f62010-07-06 01:34:17 +00006091void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6092 const ObjCAtTryStmt &S) {
6093 // Jump destination for falling out of catch bodies.
6094 CodeGenFunction::JumpDest Cont;
6095 if (S.getNumCatchStmts())
6096 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006097
John McCallf1549f62010-07-06 01:34:17 +00006098 CodeGenFunction::FinallyInfo FinallyInfo;
6099 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6100 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6101 ObjCTypes.getObjCBeginCatchFn(),
6102 ObjCTypes.getObjCEndCatchFn(),
6103 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006104
John McCallf1549f62010-07-06 01:34:17 +00006105 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006106
John McCallf1549f62010-07-06 01:34:17 +00006107 // Enter the catch, if there is one.
6108 if (S.getNumCatchStmts()) {
6109 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6110 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006111 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006112
John McCallf1549f62010-07-06 01:34:17 +00006113 Handlers.push_back(CatchHandler());
6114 CatchHandler &Handler = Handlers.back();
6115 Handler.Variable = CatchDecl;
6116 Handler.Body = CatchStmt->getCatchBody();
6117 Handler.Block = CGF.createBasicBlock("catch");
6118
6119 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006120 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006121 Handler.TypeInfo = 0; // catch-all
6122 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006123 break;
6124 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006125
John McCall5a180392010-07-24 00:37:23 +00006126 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006127 }
John McCallf1549f62010-07-06 01:34:17 +00006128
6129 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6130 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6131 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006132 }
John McCallf1549f62010-07-06 01:34:17 +00006133
6134 // Emit the try body.
6135 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006136
John McCallf1549f62010-07-06 01:34:17 +00006137 // Leave the try.
6138 if (S.getNumCatchStmts())
6139 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006140
John McCallf1549f62010-07-06 01:34:17 +00006141 // Remember where we were.
6142 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006143
John McCallf1549f62010-07-06 01:34:17 +00006144 // Emit the handlers.
6145 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6146 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006147
John McCallf1549f62010-07-06 01:34:17 +00006148 CGF.EmitBlock(Handler.Block);
6149 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006150
John McCallf1549f62010-07-06 01:34:17 +00006151 // Enter the catch.
6152 llvm::CallInst *Exn =
6153 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6154 "exn.adjusted");
6155 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006156
John McCallf1549f62010-07-06 01:34:17 +00006157 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006158 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006159 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6160 EndCatchMightThrow,
6161 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006162
John McCallf1549f62010-07-06 01:34:17 +00006163 // Bind the catch parameter if it exists.
6164 if (const VarDecl *CatchParam = Handler.Variable) {
6165 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6166 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006167
John McCallf1549f62010-07-06 01:34:17 +00006168 CGF.EmitLocalBlockVarDecl(*CatchParam);
6169 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006170 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006171
John McCallf1549f62010-07-06 01:34:17 +00006172 CGF.ObjCEHValueStack.push_back(Exn);
6173 CGF.EmitStmt(Handler.Body);
6174 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006175
John McCallf1549f62010-07-06 01:34:17 +00006176 // Leave the earlier cleanup.
6177 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006178
John McCallf1549f62010-07-06 01:34:17 +00006179 CGF.EmitBranchThroughCleanup(Cont);
6180 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006181
John McCallf1549f62010-07-06 01:34:17 +00006182 // Go back to the try-statement fallthrough.
6183 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006184
John McCallf1549f62010-07-06 01:34:17 +00006185 // Pop out of the normal cleanup on the finally.
6186 if (S.getFinallyStmt())
6187 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006188
John McCallff8e1152010-07-23 21:56:41 +00006189 if (Cont.isValid())
6190 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006191}
6192
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006193/// EmitThrowStmt - Generate code for a throw statement.
6194void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6195 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006196 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006197 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006198 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006199 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006200 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006201 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006202 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006203 "Unexpected rethrow outside @catch block.");
6204 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006205 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006206 }
6207
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006208 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006209 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
6210 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
6211 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00006212 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00006213 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006214 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00006215 } else {
6216 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
6217 ->setDoesNotReturn();
6218 CGF.Builder.CreateUnreachable();
6219 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006220
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006221 // Clear the insertion point to indicate we are in unreachable code.
6222 CGF.Builder.ClearInsertionPoint();
6223}
Daniel Dunbare588b992009-03-01 04:46:24 +00006224
John McCall5a180392010-07-24 00:37:23 +00006225llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006226CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006227 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006228 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006229
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006230 // If we don't need a definition, return the entry if found or check
6231 // if we use an external reference.
6232 if (!ForDefinition) {
6233 if (Entry)
6234 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006235
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006236 // If this type (or a super class) has the __objc_exception__
6237 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006238 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006239 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006240 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006241 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006242 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006243 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006244 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006245 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006246
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006247 // Otherwise we need to either make a new entry or fill in the
6248 // initializer.
6249 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006250 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006251 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006252 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006253 CGM.getModule().getGlobalVariable(VTableName);
6254 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006255 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6256 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006257 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006258 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006259
Chris Lattner77b89b82010-06-27 07:15:29 +00006260 llvm::Value *VTableIdx =
6261 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006262
6263 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006264 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006265 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006266 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006267 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006268 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006269
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006270 if (Entry) {
6271 Entry->setInitializer(Init);
6272 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006273 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006274 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006275 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006276 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006277 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006278 }
6279
Daniel Dunbar04d40782009-04-14 06:00:08 +00006280 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006281 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006282 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6283 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006284
6285 if (ForDefinition) {
6286 Entry->setSection("__DATA,__objc_const");
6287 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6288 } else {
6289 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6290 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006291
6292 return Entry;
6293}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006294
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006295/* *** */
6296
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006297CodeGen::CGObjCRuntime *
6298CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006299 return new CGObjCMac(CGM);
6300}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006301
6302CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006303CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006304 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006305}